我们上一篇博客讲解了Hystrix Dashboard的功能及其实现,了解到可以给每个服务添加一个后台监控界面.
但是对我们来说,查看单个的Hystrix Dashboard数据并没有多大的价值,要想看整个系统的Hystrix Dashboard数据,我们可以使用Hystrix Turbine组件.Hystrix Turbine将每个服务的Hystrix Dashboard数据进行了整合.Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了.
二. Hystrix Turbine的实现我们在上一篇博客代码的基础之上进行改造.
因为我们需要多个服务的Dashboard数据,所以需要再创建一个服务,取名为eureka-ribbon2,它的基本配置等同于eureka-ribbon,但是该服务中的Controller不同.
1. eureka-ribbon2代码改造我们创建一个新的模块,绝大多数代码复制eureka-ribbon模块中的代码实现即可,但是对Controller代码单独修改一下.
修改了Controller及其接口方法的名称.
package com.syc.cloud.web; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.syc.cloud.service.RibbonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class SayHelloController { @Autowired private RibbonService helloService; //给需要降级的方法加上注解@HystrixCommand @HystrixCommand(fallbackMethod = "handleError") @RequestMapping(value = "/sayHello") public String sayHello(@RequestParam String name) { return helloService.helloService(name); } /*熔断处理*/ public String handleError(String name) { return "Hello," + name + ". Sorry,please check your network!"; } }
其他基本一样.
2. 创建service-turbine 2.1 添加依赖ribbon02com.syc.cloud1.0-SNAPSHOT4.0.0service_turbineorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.cloudspring-cloud-starter-netflix-ribbon org.springframework.cloudspring-cloud-starter-netflix-hystrixorg.springframework.cloudspring-cloud-starter-netflix-hystrix-dashboardorg.springframework.bootspring-boot-starter-actuator org.springframework.cloudspring-cloud-starter-netflix-turbine2.2 入口类添加@EnableTurbine注解
在其入口类ServiceTurbineApplication加上注解@EnableTurbine,开启turbine聚合功能. @EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务.
package com.syc.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 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.cloud.netflix.turbine.EnableTurbine; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient //注意:必须是@EnableDiscoveryClient注解,不能是@EnableEurekaClient,否则无法实现负载均衡. @RestController @EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker //开启服务熔断功能 @EnableTurbine //开启聚合面板功能 public class ServiceTurbineApplication { public static void main(String[] args) { SpringApplication.run(ServiceTurbineApplication.class, args); } }2.3 修改配置文件application.yml
server: port: 8767 spring: application: name: service-turbine eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*" turbine: app-config: eureka-ribbon,eureka-ribbon2 aggregator: clusterConfig: default clusterNameExpression: new String("default") combine-host: true instanceUrlSuffix: default: actuator/hystrix.stream3. 项目结构

依次开启项目目录结构截图中的工程.

打开浏览器输入 http://localhost:8767/turbine.stream,界面如下:

http://localhost:8764/hello?name=一一哥

http://localhost:8766/sayHello?name=一一哥

打开:http://localhost:8767/hystrix,输入监控流http://localhost:8767/turbine.stream

可以看到我们已经实现了面板聚合,把原先每个服务中单独的熔断监控界面整合到了一起.

如果出现下图所示异常现象:
首先确保eureka,hystrix,hystrix-dashboard,actuator等相关依赖包一个都不少,另外所有该开启的注解功能都enable了.
如果以上都做到了还是有这个异常现象.
则在出现异常的服务中的入口类内部添加如下代码://解决Unable to connect to Command Metric Stream问题. @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; }
此时重启项目,再去查看Hystrix Dashboard监控面板,会发现问题得以解决.