spring boot2.0 spring cloud Hystrix熔断器 Turbine和Hystrix Dashboard熔断器监控
服务雪崩效应配置fallback 服务降级处理:
测试环境:
开启eureka注册中心,启动merber服务和order服务并注册到eureka ,order用fegin方式通过eureka进行rpc远程调用,这时关闭merber,模拟无法访问情况。order调用时会发生错误。
pom.xml
1 2 3 4 5 6 7 8 9 10 |
<!--eureka 客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--Hystrix包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> |
启动类
@EnableHystrix
控制器
1 2 3 4 5 6 7 8 |
@HystrixCommand(fallbackMethod = "fallback") //添加注解 若该方法rpc远程访问报错,则调用fallback方法,给客户端返回友好提示 @RequestMapping("/ordertomerber") public String OrderToMerber(String name){ return merber.GetMerber(name).toString(); } public String fallback(String name){ return "Error"+name; } |
熔断器:
Turbine 可以多个熔断器监控
Hystrix Dashboard 只能单个
Hystrix Dashboard配置:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> |
application.properties添加
1 2 3 4 |
##设置hystrix端口 management.server.port=9001 ##开启所有路由 management.endpoints.web.exposure.include= '*' |
控制器添加设置fallback降级处理
启动类
@EnableHystrix
@EnableHystrixDashboard
测试
2002 服务的端口号
9001服务设置的hystrix的端口号
访问http://localhost:2002/hystrix ##注意:端口号为具体服务对应的端口号而不是Eureka Server的端口号
依次填写http://localhost:9001/actuator/hystrix.stream、2000、Amn(随意填写),点击 moniter
该页面显示了熔断器的谷中数据指标,这些数据指标含义如图
细节地址:
访问http://localhost:2002/ordertomerber,出错时,调用fallback的方法后
error null
————-
访问http://lcoalshost:9001/actuator/hystrix.stream
浏览器会显示熔断器的数据指标
===============================
Turbine配置
监控merber 和order
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> |
启动文件
@EnableHystrix
@EnableHystrixDashboard
@EnableEurekaClient
application.properties添加
1 2 3 |
#每个服务设置不同的端口号 management.server.port=9002(9001) management.endpoints.web.exposure.exclude='*' |
order和merber控制器都必须设置一个fallback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//Merber @HystrixCommand(fallbackMethod = "fallback") @RequestMapping("/MerberInfo") public String MerberInfo(){ return "name is "+env.getProperty("spring.application.name")+",port:"+env.getProperty("server.port"); } public String fallback(){ return "error"; } ################# //Order @HystrixCommand(fallbackMethod = "fallback") @RequestMapping("/ordertomerber") public String OrderToMerber(String name) { System.out.println(Thread.currentThread().getName()); return merber.GetMerber(name).toString(); }
public String fallback(String name) { return "Error" + name; } |
新建Turbine服务
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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> <!--Turbine--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
application.properties
1 2 3 4 5 6 7 8 9 10 |
spring.application.name=turbine server.port=3001 eureka.client.service-url.defaultZone=http://localhost:1001/eureka/
management.endpoints.web.exposure.exclude='*' #需要监控的服务集群名 turbine.aggregator.cluster-config=default #需要监控的服务名 turbine.app-config=order,merber turbine.cluster-name-expression=new String("default") |
测试
依次启动euerka(1001) order(2002,9001) merber(2000,9002) turbin(3001)
访问http://localhost:2000/hystrix(或者http://localhost:2002/hystrix)
依次填写http://localhost:3001/turbine.stream ;;2000 ;;Amn
即可显示图像化的熔断器状态
如果一直为loading或者不显示,尝试依次访问一下每个服务(设置过fallback)对应的的路由
http://localhost:2002/ordertomerber
http://localhost:2000/MerberInfo
===================================================
代码地址