第四篇(续):熔断监控Hystrix Dashboard和熔断聚合监控Hystrix Turbine
Hystrix Dashboard:在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
本篇基于第一篇的项目
创建service-client-dashboard
改造service-client,在pom文件中引入依赖
<!--Eureka Client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--Web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--actuator监控监控依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--hystrix dashboard--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
编写配置文件application.yml
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
在启动类添加@EnableHystrixDashboard注解, 开启HystrixDashboard
@SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableHystrixDashboard public class EurekaClientHystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientHystrixDashboardApplication.class, args); } @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
修改controller层TestEurekaClient类
@RestController public class TestEurekaClient { @Value("${server.port}") String port; @GetMapping("/test") @HystrixCommand(fallbackMethod = "hiError") public String home(@RequestParam(value = "name", defaultValue = "niuben") String name) { return "hi " + name + " ,现在的端口是:" + port; } public String hiError(String name) { return "hi, "+name+", error!"; } }
依次启动eureka-server 和service-client
浏览器,打开http://localhost:8762/actuator/hystrix.stream(在请求http://localhost:8762/actuator/hystrix.stream之前,需要随便请求一个接口,例如:http://localhost:8762/test,否则会一直ping ping ping)
在打开localhost:8762/hystrix,可以看见以下界面
依次输入http://localhost:8762/actuator/hystrix.stream 、2000 、test,出现以下界面
在另一个窗口请求http://localhost:8762/test?name=test,试试看
熔断聚合监控Hystrix Turbine
当我们有很多个服务的时候,这就需要聚合所以服务的Hystrix Dashboard的数据了
使用上面的工程
创建service-client-turbine模块
pom文件如下
<!--Eureka Client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--Web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--actuator监控监控依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--hystrix dashboard--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!--hystrix turbine--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
编写配置文件application.yml
server: port: 8764 spring: application: name: eureka-client-turbine eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*" turbine: app-config: eureka-client,eureka-clientTwo aggregator: clusterConfig: default clusterNameExpression: new String("default") combine-host: true instanceUrlSuffix: default: actuator/hystrix.stream
在启动类开启注解@EnableTurbine
@SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableHystrixDashboard @EnableTurbine public class EurekaClientHystrixTurbineApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientHystrixTurbineApplication.class, args); } @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
再复制service-client-turbine工程,命名为service-client-turbine-two(记得修改端口号)
依次启动eureka-server、service-client、service-client-turbine-two、service-client-turbine工程
浏览器打开:http://localhost:8763/hystrix;输入监控流http://localhost:8764/turbine.stream,点击monitor stream 进入页面,请求http://localhost:8762/test?name=ee,http://localhost:8763/test?name=ee
就可以看到这个页面聚合了2个service的hystrix dashbord数据了