SpringCloud微服务 之hystrix(六)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012437781/article/details/83894951

前言

本小结我们来学习一下使用Turbine来聚合各个服务节点的Hystrix.stream信息。

案例

  • Eureka Server端编写(参考前例)。
    在这里插入图片描述

  • Eureka Client端服务提供方编写(参考:SpringCloud微服务 之hystrix(五))
    Mock两个服务调用方且各自都实现了监控各自Hystrix.stream端点的功能。

    • microservice-deal-broker-cloud-hystrix-dashboard
    • microservice-deal-broker-cloud-hystrix-dashboard-2
  • Eureka Client Turbine Dashboard编写

    • 项目结构
      在这里插入图片描述
    • CoreCode
      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      	<modelVersion>4.0.0</modelVersion>
      
      	<artifactId>microservice-deal-broker-cloud-hystrix-dashboard-turbine</artifactId>
      	<packaging>jar</packaging>
      
      	<name>microservice-deal-broker-cloud-hystrix-dashboard-turbine</name>
      	<description>Demo project for Spring Boot</description>
      
      	<parent>
      		<groupId>com.example</groupId>
      		<artifactId>microservice-deal-parent</artifactId>
      		<version>0.0.1-SNAPSHOT</version>
      	</parent>
      
      	<dependencies>
      		<!-- Feignf -->
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-openfeign</artifactId>
      		</dependency>
      
      		<!-- springCloud Hystrix依赖 -->
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
      		</dependency>
      
      		<!-- springCloud Hystrix依赖 -->
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
      		</dependency>
      
      		<!-- Turbine依赖 -->
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
      		</dependency>
      
      	</dependencies>
      
      </project>
      
      
      server:
        port: 8084
      spring:
        application:
          name: microservice-deal-broker-cloud-hystrix-dashboard-turbine
      eureka:
        client:
          serviceUrl:
            #defaultZone: http://localhost:8080/eureka/
            defaultZone: http://Dustyone:[email protected]:8080/eureka/
        instance:
          prefer-ip-address: true
      
      
      
      turbine:
        # 配置 Eureka 中的 serviceId 列表,指定要监控的服务
        app-config: MICROSERVICE-DEAL-BROKER-CLOUD-HYSTRIX-DASHBOARD-2,MICROSERVICE-DEAL-BROKER-CLOUD-HYSTRIX-DASHBOARD
        aggregator:
          cluster-config: default
        cluster-name-expression: "'default'" # 指定集群名称
        combine-host-port: true #表示同一主机上的服务通过host和port的组合来进行区分,默认情况下是使用host来区分,这样会使本地调试有问题
      
      #开放所有的监控节点
      management:
        endpoints:
          web:
            exposure:
              include: '*'
      
      #声明Feign中的Hystrix
      # 说明:请务必注意,从Spring Cloud Dalston开始,Feign默认是不开启Hystrix的。
      # 因此,如使用Dalston请务必额外设置属性:feign.hystrix.enabled=true,否则断路器不会生效。
      # 而,Spring Cloud Angel/Brixton/Camden中,Feign默认都是开启Hystrix的。无需设置该属性。
      feign:
        hystrix:
          enabled: true
      
      
      
      #使用Feign时必须添加以下两项配置
      ribbon:
        eureka:
          enabled: true
      
      #设置feign的hystrix响应超时时间(必须)
      hystrix:
        command:
            default:
              execution:
                isolation:
                  thread:
                    timeoutInMilliseconds: 5000
      
      @Component
      public class InitiServlets {
      	
      	/**
      	 * 初始化Hystrix 容器
      	 * @return
      	 */
      	@Bean
      	public ServletRegistrationBean<Servlet> getHystrixSerlvet() {
      		// HystrixStreamServlet
      		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
      
      		ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
      		registrationBean.setLoadOnStartup(1);
      		registrationBean.addUrlMappings("/hystrix.stream");
      		registrationBean.setName("HystrixMricsStreamServlet");
      		return registrationBean;
      	}
      	
      }
      
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients //开启FeignClient注解
      @EnableCircuitBreaker //开启Hystrix熔断机制
      @EnableHystrixDashboard
      @EnableTurbine //开启Hystrix Turbine聚合Dashboard
      public class MicroserviceDealBrokerHystrixDashBoardTurbineApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(MicroserviceDealBrokerHystrixDashBoardTurbineApplication.class,
      				args);
      	}
      	
      	/**
      	 * 1被监控的所有服务节点都要开启Hystrix.stream监控,不然Turbine会报错
      	 * 
      	 * 2访问Turnbine.stream时需注意
      	 * http://localhost:8084/turbine.stream 
      	 * unable to connect to command metric stream:
      	 * 1)stream地址输入错误,一定要输入:ip:port/turbine.stream,可能会误写成:ip:port/turbine.stream,这样一来,反复检查都不容易检查出问题,因为本身后者在使用hystrix时是没错的,但用turbine时就不对了;
      	 * 2)缺少pom依赖或注解
      	 */
      }
      
      
  • Eureka Client端服务提消费编写(单实例) (参考前例)。

  • 访问:http://localhost:8084/hystrix
    在这里插入图片描述
    在这里插入图片描述

  • 访问被Turbine监控的服务节点:
    http://localhost:8082/deal/1、http://localhost:8082/dealMock/1、http://localhost:8083/deal/1
    http://localhost:8083/dealMock/1:
    在这里插入图片描述

  • 手动shutdown服务提供方:
    在这里插入图片描述

  • 再次访问被Turbine监控的服务节点:
    http://localhost:8082/deal/1、http://localhost:8082/dealMock/1、http://localhost:8083/deal/1
    http://localhost:8083/dealMock/1:
    在这里插入图片描述

由此便实现了使用Turbine聚合监控各个服务节点的Hystrix.stream信息。

小结

  • 实现Turbine聚合监控各个服务节点Hystrix.stream信息,首先需要添加Turbine依赖:spring-cloud-starter-netflix-turbine然后在主类上声明:@EnableTurbine(Turbine所在的服务节点也必须先实现Hystrix.stream Dashboard监控功能)。
  • Turbine监控的所有服务节点必须先实现Hystrix.stream Dashboard监控功能,否则Turbine监控服务会报异常。
  • 访问Turnbine的Hystrix Dashboard时需注意:一定要输入:ip:port/turbine.stream否则无法进入监控页面。
  • 本小结使用到的案例:microservice-deal-eureka-authentication、microservice-deal-cloud、microservice-deal-broker-cloud-hystrix-dashboard、microservice-deal-broker-cloud-hystrix-dashboard-2、microservice-deal-broker-cloud-hystrix-dashboard-turbine。

猜你喜欢

转载自blog.csdn.net/u012437781/article/details/83894951
今日推荐