SpringCloud微服务 之hystrix(五)

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

前言

本小节将学习一下Hystrix的Dashboard。Hystrix Dashboard是Hystrix的仪表盘组件,主要用来实时监控Hystrix的各项指标信息,通过界面反馈的信息可以快速发现系统中存在的问题,即服务节点实现Hystrix Metrics Stream,然后通过Hystrix Dashboard将stream的信息可视化。下面我们将通过案例来学习一下Hystrix-Dashboard。

案例

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

  • Eureka Client端服务提供方编写。

    • 项目结构
      在这里插入图片描述

    • 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</artifactId>
      	<packaging>jar</packaging>
      
      	<name>microservice-deal-broker-cloud-hystrix-dashboard</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>
      		<!-- Eureka -->
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      		</dependency>
      
      		<!-- 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>
      		
      	</dependencies>
      
      </project>
      
      
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients //开启FeignClient注解
      @EnableCircuitBreaker //开启Hystrix熔断机制
      @EnableHystrixDashboard
      public class MicroserviceDealBrokerHystrixDashBoardApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(MicroserviceDealBrokerHystrixDashBoardApplication.class,
      				args);
      	}
      }
      
      /**
       * SpringCould 2.0+需要为HystrixMricsStreamServlet手动注入到ServletRegistrationBean中
       * @author Administrator
       *
       */
      @Component
      public class HystrtixStreamServlet {
      	
      	@Bean
      	public ServletRegistrationBean<Servlet> getSerlvet(){
      		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
      		ServletRegistrationBean<Servlet> registrationBean =  new ServletRegistrationBean<>(streamServlet);
      		registrationBean.setLoadOnStartup(1);
      		registrationBean.addUrlMappings("/hystrix.stream");
      		//registrationBean.addUrlMappings("/hystrix");
      		registrationBean.setName("HystrixMricsStreamServlet");
      		return registrationBean;
      	}
      }
      
      @FeignClient(name = "microservice-deal-cloud",fallback = HystrixFallBackMethod.class)
      public interface FeignConfig {
      
      	//第一个坑:使用Feign的时候,如果参数中带有@PathVariable形式的参数,则要用value=""标明对应的参数,否则会抛出IllegalStateException异常
      	@GetMapping("/deal/{id}")
      	public Deal findById(@PathVariable(value="id") Long id);
      	
      	@GetMapping("/dealMock/{id}")
      	public Deal findByIdMock(@PathVariable(value="id") Long id);
      }
      
      @Component
      public class HystrixFallBackMethod implements FeignConfig {
      
      	@Override
      	public Deal findById(Long id) {
      		Deal deal = new Deal();
      		deal.setId(id.intValue());
      		deal.setName("Dustyone");
      		return deal;
      	}
      
      	@Override
      	public Deal findByIdMock(Long id) {
      		Deal deal = new Deal();
      		deal.setId(id.intValue());
      		deal.setName("Dustyone");
      		return deal;
      	}
      
      }
      
      
      @RestController
      public class BrokerController {
      	
      	@Autowired
      	private FeignConfig feignConfig;
      	
      	@GetMapping("/deal/{id}")
      	public Deal findById(@PathVariable(value="id") Long id) {
      		return this.feignConfig.findById(id);
      	}
      	
      	@GetMapping("/dealMock/{id}")
      	public Deal findByIdMock(@PathVariable(value="id") Long id) {
      		return this.feignConfig.findByIdMock(id);
      	}
      }
      
      server:
        port: 8082
      spring:
        application:
          name: microservice-deal-broker-cloud-hystrix-dashboard
      eureka:
        client:
          serviceUrl:
            defaultZone: http://localhost:8080/eureka/
        instance:
          prefer-ip-address: true
      
      
      #开放所有的监控节点
      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
      
  • Eureka Client端服务提消费编写(单实例) (参考前例)。

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

在这里插入图片描述

小结

  • Dashboard服务是个独立的结点,不需要配置eureka信息,需要Hystrix Dashboard 依赖包:spring-cloud-starter-netflix-hystrix-dashboard。
  • 启用Hystrix Dashboard只需在主类上开启注解:@EnableHystrixDashboard。
  • 要确保被监控的服务打开了Actuator,并开启了断路器(@EnableCircuitBreaker注解)。同时我们需要对Actuator做访问时需要对一些节点做声明,本案例中默认所有监控节点都可以被查看:
    management: endpoints: web:exposure: include: ‘*’。
  • SpringCloud V2.0+之后需要手动声明HystrixMetricsStreamServlet,详情查看本小节HystrtixStreamServlet.java。
  • 本小节用到的案例:microservice-deal-eureka、microservice-deal-cloud、microservice-deal-broker-cloud-hystrix-dashboard。

猜你喜欢

转载自blog.csdn.net/u012437781/article/details/83714768