美文网首页
【六】Spring Cloud Hystrix监控

【六】Spring Cloud Hystrix监控

作者: lemonMT | 来源:发表于2019-03-12 13:56 被阅读0次

    1.Hystrix单体应用监控

    1.1新建一个工程 demo-springCloud-hystrix-dashboard,增加依赖

     <!--熔断器-->
            <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>
    

    1.2启动类增加代码

    //开启面板功能
    @EnableHystrixDashboard
    @SpringBootApplication
    public class DemoSpringcloudHystrixDashboardApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoSpringcloudHystrixDashboardApplication.class, args);
        }
    }
    

    1.3 修改微服务端,受监控端, 增加依赖

     <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>
            <!--熔断器-->
    

    1.4 微服务启动类增加修改代码

    
    //发现服务注册中心,将服务进行注册
    @EnableDiscoveryClient
    //开启feign能力
    @EnableFeignClients
    //开启断路器功能
    @EnableHystrix
    @SpringBootApplication
    public class DemoSpringcloudServiceUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoSpringcloudServiceUserApplication.class, args);
        }
        
    
        //需要重写这个注册类,不然无法连接hystrix.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;
        }
    }
    

    1.5 打开监控页面,输入对应微服务的监控地址

    image.png image.png

    因为上次的代码,加上了异常,所以全部为红色。

    2.Hystrix集群应用监控

    2.1 集群监控应用的项目有如下:

    • demo-springcloud-eureka-standalone
    • demo-springcloud-service-user
    • demo-springcloud-service-order
    • demo-springcloud-hystrix-turbine
    • demo -springcloud-hystrix-dashboard

    2.2 增加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>
    

    2.3 增加turbine项目的配置文件

    spring:
      application:
        name: demo-springcloud-hystrix-turbine
    eureka:
      client:
        service-url:
          defaultZone: http://user:user@demo-springcloud-eureka-one:8761/eureka/
      instance:
        prefer-ip-address: true
    server:
      port: 8877
    turbine:
      app-config: demo-springcloud-service-user,demo-springcloud-service-order  # 配置Eureka中的serviceId列表,表明监控哪些服,逗号分隔开
      cluster-name-expression: new String('default')
      combine-host-port: true
      aggregator:
        cluster-config: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
    
    

    2.4 增加turbine项目启动类代码

    //开启聚合功能
    @EnableTurbine
    @SpringBootApplication
    public class DemoSpringcloudHystrixTurbineApplication {
    
      public static void main(String[] args) {
          SpringApplication.run(DemoSpringcloudHystrixTurbineApplication.class, args);
      }
    
    }
    

    2.5 启动预览

    -启动所有的项目,在eureka上进行观察

    image.png

    -开始调用user项目testFeign和Order项目的add接口


    image.png image.png
    • 输入地址,查看聚合页面的工作情况
      可以观察到多个server的调用情况


      image.png

    相关文章

      网友评论

          本文标题:【六】Spring Cloud Hystrix监控

          本文链接:https://www.haomeiwen.com/subject/nxfdpqtx.html