微服务熔断限流Hystrix之Dashboard
程序员文章站
2022-04-09 09:01:32
简介 Hystrix Dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard可以直观地看到各Hystrix Command的请求响应时间,请求成功率等数据。 快速上手 工程说明 | 工程名 | 端口 | 作用 | | : | : | : : | | eu ......
简介
hystrix dashboard是一款针对hystrix进行实时监控的工具,通过hystrix dashboard可以直观地看到各hystrix command的请求响应时间,请求成功率等数据。
快速上手
工程说明
工程名 | 端口 | 作用 |
---|---|---|
eureka-server | 8761 | 注册中心 |
service-hi | 8762 | 服务提供者 |
service-consumer | 8763 | 服务消费者 |
核心代码
eureka-server 工程
pom.xml
<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>
application.yml
server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultzone: http://${eureka.instance.hostname}:/${server.port}/eureka/ spring: application: name: eureka-server
启动类
@springbootapplication @enableeurekaserver public class eurekaserverapplication { public static void main(string[] args) { springapplication.run( eurekaserverapplication.class, args ); } }
service-hi 工程
pom.xml
<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-client</artifactid> </dependency>
application.yml
server: port: 8762 spring: application: name: service-hi eureka: client: service-url: defaultzone: http://localhost:8761/eureka/
hellocontroller
@restcontroller public class hellocontroller { @getmapping("/hi") public string hi() { return "hello ~"; } @getmapping("/hey") public string hey() { return "hey ~"; } @getmapping("/oh") public string oh() { return "ah ~"; } @getmapping("/ah") public string ah() { //模拟接口1/3的概率超时 random rand = new random(); int randomnum = rand.nextint(3) + 1; if (3 == randomnum) { try { thread.sleep( 3000 ); } catch (interruptedexception e) { e.printstacktrace(); } } return "来了老弟~"; } }
启动类
@springbootapplication @enableeurekaclient public class servicehiapplication { public static void main(string[] args) { springapplication.run( servicehiapplication.class, args ); } }
service-consumer 工程
pom.xml
<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-client</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-ribbon</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</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-hystrix-dashboard</artifactid> </dependency>
application.yml
server: port: 8763 tomcat: uri-encoding: utf-8 max-threads: 1000 max-connections: 20000 spring: application: name: service-consumer eureka: client: service-url: defaultzone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
helloservice
@service public class helloservice { @autowired private resttemplate resttemplate; /** * 简单用法 */ @hystrixcommand public string hiservice() { return resttemplate.getforobject("http://service-hi/hi" , string.class); } /** * 定制超时 */ @hystrixcommand(commandproperties = { @hystrixproperty(name = "execution.isolation.thread.timeoutinmilliseconds", value = "30000") }) public string heyservice() { return resttemplate.getforobject("http://service-hi/hey" , string.class); } /** * 定制降级方法 */ @hystrixcommand(fallbackmethod = "getfallback") public string ahservice() { return resttemplate.getforobject("http://service-hi/ah" , string.class); } /** * 定制线程池隔离策略 */ @hystrixcommand(fallbackmethod = "getfallback", threadpoolkey = "studentservicethreadpool", threadpoolproperties = { @hystrixproperty(name="coresize", value="30"), @hystrixproperty(name="maxqueuesize", value="50") } ) public string ohservice() { return resttemplate.getforobject("http://service-hi/oh" , string.class); } public string getfallback() { return "oh , sorry , error !"; } }
hellocontroller
@restcontroller public class hellocontroller { @autowired private helloservice helloservice; @getmapping("/hi") public string hi() { return helloservice.hiservice(); } @getmapping("/hey") public string hey() { return helloservice.heyservice(); } @getmapping("/oh") public string oh() { return helloservice.ohservice(); } @getmapping("/ah") public string ah() { return helloservice.ahservice(); } }
启动类
@springbootapplication @enableeurekaclient @enablehystrixdashboard @enablehystrix @enablecircuitbreaker public class serviceconsumerapplication { public static void main(string[] args) { springapplication.run( serviceconsumerapplication.class, args ); } @loadbalanced @bean public resttemplate resttemplate() { return new resttemplate(); } }
hystrix dashboard 的使用
json格式监控信息
先访问http://localhost:8762/hi
再打开http://localhost:8763/actuator/hystrix.stream,可以看到一些具体的数据:
hystrix仪表盘监控信息
单纯的查看json数据,很难分析出结果,所以,要在hystrix仪表盘中来查看这一段json,在hystrix仪表盘中输入监控地址进行监控:
打开仪表盘地址:http://localhost:8762/hystrix
在界面依次输入:http://localhost:8763/actuator/hystrix.stream 、2000 、service-consumer;点确定。
hystrix仪表盘指标含义
测试
编一个测试脚本curl.sh
while true; do curl "http://localhost:8763/hi"; curl "http://localhost:8763/hey"; curl "http://localhost:8763/oh"; curl "http://localhost:8763/ah"; done
执行测试脚本,查看hystrix仪表盘:
可以看出 ahservice 因为模拟了1/3的概率超时,所以监控中呈现了30%左右的错误百分比。
源码
https://github.com/gf-huanchupk/springcloudlearning/tree/master/chapter17
欢迎扫码或微信搜索公众号《程序员果果》关注我,关注有惊喜~
上一篇: 网页转换成图片
下一篇: Java8 中的 Optional
推荐阅读
-
微服务熔断限流Hystrix之Dashboard
-
Springcloud hystrix服务熔断和dashboard如何实现
-
SpringCloud之熔断监控Hystrix Dashboard的实现
-
微服务熔断限流Hystrix之流聚合
-
SpringCloud之Hystrix【服务降级、服务熔断、服务限流】
-
spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)
-
SpringCloud之熔断监控Hystrix Dashboard的实现
-
Springcloud hystrix服务熔断和dashboard如何实现
-
微服务熔断限流Hystrix之流聚合
-
微服务熔断限流Hystrix之Dashboard