(四)熔断监控Hystrix Dashboard和Turbine

1、Hystrix Dashboard

通过Hystrix Dashboard可以看到单个应用内的服务信息,本次使用到的应用包括:

  • eureka-server:服务注册中心
  • eureka-producer:服务提供者
  • eureka-consumer-feign-hystrix:使用 Feign 和 Hystrix 实现的服务消费者

使用新的工程Hystrix Dashboard
(1)pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.springcloud
    hystrix-dashboard
    0.0.1-SNAPSHOT
    hystrix-dashboard
    Demo project for Spring Boot
    
        1.8
        Greenwich.SR1
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


(2)application.yml

server:
  port: 8093
spring:
  application:
    name: hystrix-dashboard
eureka:
  server:
    enable-self-preservation: false #关闭eureka的自我保护,防止已被关停的节点也错误的显示在线
  client:
    register-with-eureka: true #否允许客户端向Eureka 注册表获取信息,服务器为设置为false,客户端设置为true
    fetch-registry: true #是否允许向Eureka Server注册信息 如果是服务器端,应该设置为false
    service-url:
      defaultZone: http://peer1:8761/eureka/ #此eureka server的应用注册地址
feign:
  hystrix:
    enabled: true

(3)HystrixDashboardApplication.java

@SpringBootApplication
@EnableHystrixDashboard
@EnableEurekaClient
@EnableFeignClients //这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

(4)Controller和远程调用文件

@RestController
public class HelloController {

    @Autowired
    private HelloRemote helloRemote;

    @RequestMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return helloRemote.hello(name);
    }

    @RequestMapping("/hello1")
    public String hello1(@RequestParam("name") String name) {
        return helloRemote.hello(name);
    }
}
@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteFallback.class)
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}
@Component
public class HelloRemoteFallback implements HelloRemote {

    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " + name + ",i am fallback message";
    }
}

启动eureka和hystrix-dashboard,访问http://localhost:8093/hystrix,在页面中间长文本框中输入http://localhost:8093/hystrix.stream,点击Monitor Stream,显示Loading ...,再访问http://localhost:8093/hello?name=1,可以看到生成如下图表

(四)熔断监控Hystrix Dashboard和Turbine_第1张图片
2、Turbine

引入Turbine来聚合Ribbon-consumer服务的监控信息,并输出给Hystrix Dashboard来进行展示。
创建一个新的工程turbine
(1)pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.springcloud
    turbine
    0.0.1-SNAPSHOT
    turbine
    Demo project for Spring Boot
    
        1.8
        Greenwich.SR1
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix-dashboard
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-turbine
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


(2)application.yml

server:
  port: 8094
spring:
  application:
    name: hystrix-dashboard-turbine
turbine:
  app-config: consumer-node1,consumer-node2 #配置Eureka中的serviceId列表,表明监控哪些服务
  aggregator:
    cluster-config: default #指定聚合哪些集群,多个使用”,”分割,默认为default
  cluster-name-expression: new String("default") #参数指定了集群名称为 default,当我们服务数量非常多的时候,可以启动多个 Turbine 服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群,同时该参数值可以在 Hystrix 仪表盘中用来定位不同的聚合集群,只需要在 Hystrix Stream 的 URL 中通过 cluster 参数来指定
  combine-host-port: true #参数设置为true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以 host 来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/

(3)TurbineApplication.java启动类

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApplication {

    public static void main(String[] args) {
        SpringApplication.run(TurbineApplication.class, args);
    }

}

创建消费者集群,按consumers项目的内容创建两个项目consumer-node1,consumer-node2
修改对应的应用名

spring:
  application:
    name: consumer-node1

增加依赖包


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix-dashboard

增加启动类注册

@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;
}

依次启动注册中心eureka,turbine和两个消费者,访问两个消费者,输入http://localhost:8094/hystrix, 会返回小熊界面,输入:http://localhost:8094/turbine.stream,然后点击 Monitor Stream ,可以看到出现了俩个监控列表

你可能感兴趣的:((四)熔断监控Hystrix Dashboard和Turbine)