springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)

springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本),第1张

概述本文基于方志朋先生的博客实现:https://blog.csdn.net/forezp/article/details/70233227 一、准本工作 1、工具:Idea,JDK1.8,Maven3.5 2、创建四个model,名字分别为eurserver(服务注册中心),service-hi(实现断路监控),service-lucy(实现断路监控),service-turbine(断路器聚合监控

本文基于方志朋先生的博客实现:https://blog.csdn.net/forezp/article/details/70233227

一、准本工作

1、工具:IDea,JDK1.8,Maven3.5

2、创建四个model,名字分别为eurserver(服务注册中心),service-hi(实现断路监控),service-lucy(实现断路监控),service-turbine(断路器聚合监控)

二、创建与实现

1、创建服务注册中心eurserver,右键工程,new ==> model,然后选择Spring Initializr,next。

然后填写model名字,next,选择Cloud discovery,Eureka Server,next ==>finish

启动一个服务注册中心,主需要一个注解@EnableEurekaServer,这个注解放在springboot的启动工程里。

@SpringBootApplication@EnableEurekaServerpublic class EurserverApplication {    public static voID main(String[] args) {        SpringApplication.run(EurserverApplication.class,args);    }}

填写配置文件:

server:  port: 8088  //端口号eureka:  instance:    hostname: localhost //主机地址  clIEnt:    register-with-eureka: false //注册中心告诉自己不能向自己注册自己,默认为true    fetch-registry: false        service-url:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

 pom文件:

<?xml version="1.0" enCoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupID>com.rrg</groupID>    <artifactID>eurserver</artifactID>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>eurserver</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-starter-parent</artifactID>        <version>2.0.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <propertIEs>        <project.build.sourceEnCoding>UTF-8</project.build.sourceEnCoding>        <project.reporting.outputEnCoding>UTF-8</project.reporting.outputEnCoding>        <java.version>1.8</java.version>        <spring-cloud.version>Finchley.RC2</spring-cloud.version>    </propertIEs>    <dependencIEs>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-eureka-server</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-test</artifactID>            <scope>test</scope>        </dependency>    </dependencIEs>    <dependencyManagement>        <dependencIEs>            <dependency>                <groupID>org.springframework.cloud</groupID>                <artifactID>spring-cloud-dependencIEs</artifactID>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencIEs>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupID>org.springframework.boot</groupID>                <artifactID>spring-boot-maven-plugin</artifactID>            </plugin>        </plugins>    </build>    <repositorIEs>        <repository>            <ID>spring-milestones</ID>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositorIEs></project>

项目列表:

 

 启动项目,访问:localhost:8088

可以看到,没有任何服务向注册中心注册。

2、创建一个service-hi

选择依赖包:

pom文件:

<?xml version="1.0" enCoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupID>com.rrg</groupID>    <artifactID>service-hi</artifactID>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>service-hi</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-starter-parent</artifactID>        <version>2.0.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <propertIEs>        <project.build.sourceEnCoding>UTF-8</project.build.sourceEnCoding>        <project.reporting.outputEnCoding>UTF-8</project.reporting.outputEnCoding>        <java.version>1.8</java.version>        <spring-cloud.version>Finchley.RC2</spring-cloud.version>    </propertIEs>    <dependencIEs>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-actuator</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-web</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-eureka-server</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-hystrix</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-hystrix-dashboard</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-test</artifactID>            <scope>test</scope>        </dependency>    </dependencIEs>    <dependencyManagement>        <dependencIEs>            <dependency>                <groupID>org.springframework.cloud</groupID>                <artifactID>spring-cloud-dependencIEs</artifactID>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencIEs>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupID>org.springframework.boot</groupID>                <artifactID>spring-boot-maven-plugin</artifactID>            </plugin>        </plugins>    </build>    <repositorIEs>        <repository>            <ID>spring-milestones</ID>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositorIEs></project>

配置文件:

server.port=8085
# 向服务中心注册
eureka.clIEnt.service-url.defaultZone=http://localhost:8088/eureka/ spring.application.name=service-hi

启动类:

package com.rrg;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsstreamServlet;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.cloud.netflix.eureka.EnableEurekaClIEnt;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapPing;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClIEnt// 断路器@EnableHystrix// 断路器仪表盘@EnableHystrixDashboard@RestControllerpublic class ServiceHiApplication {    public static voID main(String[] args) {        SpringApplication.run(ServiceHiApplication.class,args);    }    // 这个是2.02要添加的,不然仪表盘不显示    @Bean    public ServletRegistrationBean getServlet(){        HystrixMetricsstreamServlet streamServlet = new HystrixMetricsstreamServlet();        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);        registrationBean.setLoadOnStartup(1);        registrationBean.addUrlMapPings("/hystrix.stream");        registrationBean.setname("HystrixMetricsstreamServlet");        registrationBean.setname("HystrixMetricsstreamServlet");        return registrationBean;    }    @Value("${server.port}")    private String port;    @RequestMapPing("/hi")    @HystrixCommand(fallbackMethod = "hIErr")    public String hi(@RequestParam String name){        return "hi," + name + "I am a port:" + port;    }    public String hIErr(@RequestParam String name){        return "sorry," + name + ",have a error";    }}

启动service-hi,访问localhost:8085

然后输入:http://localhost:8085/hystrix.streammiya,点击Monitor Stream

在访问之前,先访问local host:8085/hi?name=rose,这样那个仪表盘才显示。

 到此,已经实现了单个断路器的监控,但是如果要监控多个服务的话,就需要turbine来实现

3、创建一个service-trubine,service-lucy的创建和service-hi一摸一样,就不用多写了。

pom文件 :

<?xml version="1.0" enCoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupID>com.rrg</groupID>    <artifactID>service-turbine</artifactID>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>service-turbine</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupID>org.springframework.boot</groupID>        <artifactID>spring-boot-starter-parent</artifactID>        <version>2.0.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <propertIEs>        <project.build.sourceEnCoding>UTF-8</project.build.sourceEnCoding>        <project.reporting.outputEnCoding>UTF-8</project.reporting.outputEnCoding>        <java.version>1.8</java.version>        <spring-cloud.version>Finchley.RC2</spring-cloud.version>    </propertIEs>    <dependencIEs>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-actuator</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-turbine</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.cloud</groupID>            <artifactID>spring-cloud-starter-netflix-hystrix-dashboard</artifactID>        </dependency>        <dependency>            <groupID>org.springframework.boot</groupID>            <artifactID>spring-boot-starter-test</artifactID>            <scope>test</scope>        </dependency>    </dependencIEs>    <dependencyManagement>        <dependencIEs>            <dependency>                <groupID>org.springframework.cloud</groupID>                <artifactID>spring-cloud-dependencIEs</artifactID>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencIEs>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupID>org.springframework.boot</groupID>                <artifactID>spring-boot-maven-plugin</artifactID>            </plugin>        </plugins>    </build>    <repositorIEs>        <repository>            <ID>spring-milestones</ID>            <name>Spring Milestones</name>            <url>https://repo.spring.io/milestone</url>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositorIEs></project>

配置文件:

server.port=8087spring.application.name=service-turbine# 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问turbine.aggregator.cluster-config=default#表明监控哪些服务turbine.app-config=service-hi,service-lucyturbine.cluster-name-Expression=new String("default")
#这里和service-hi启动类里的
registrationBean.addUrlMapPings("/hystrix.stream")一致,原因待探索
turbine.instanceUrlSuffix=/hystrix.stream
eureka.clIEnt.service
-url.defaultZone=http://localhost:8088/eureka/

启动类:

@SpringBootApplication// @Enableturbine注解包含了@EnablediscoveryClIEnt注解@Enableturbine@EnableHystrixDashboardpublic class ServiceturbineApplication {    public static voID main(String[] args) {        SpringApplication.run(ServiceturbineApplication.class,args);    }}

然后分别启动四个项目:访问localhost:8088

都已注册。访问local host:8087,输入http://localhost:8087/turbine.stream点击进去。

 

 

可以看到,已经成功的对service-hi和service-lucy聚合监控。

总结

以上是内存溢出为你收集整理的springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)全部内容,希望文章能够帮你解决springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存