如何使用SpringCloud的Hystrix组件

Owen Jia 2020年03月26日 920次浏览

Circuit Breaker解决方案Spring Cloud Hystrix组件。

以 2.2.0.release 为例,教你如何入门该组件。

核心概念

  • hystrix:就是一般所说的断路器组件
  • hystrix-dashboard:断路器监控统计展示面板组件
  • turbine:多节点的断路器统计数据合并展示组件,一般用于集群场景

引入的jar包

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

搭建各Project

本例采用Maven进行project的build等工作。下面关于spring cloud的discover和config模块配置什么的被剔除了,为了减少篇幅内容,但实际的spring cloud方案中是一定有的。

cloud-service-hystrix-dashboard

这个项目就是用来展示监控统计Circuit Breaker的数据,也就是hystrix.stream报上来的各应用线程接口熔断统计数据。在微服务体系中,dashboard要独立搭建项目,因为后续可能需要增加定制功能。dashboard需要支持单节点的统计数据同时,还需要能支持集群统计数据,因为微服务里每个应用至少是部署2台的,这些熔断数据不可能一个一个节点单独查看,需要汇总在一起,这里就用到了turbine组件。

pom.xml:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

spring:
  application:
    name: service-hystrix-dashbaord
  profiles:
    active: ${ENV:local}

server:
  port: 8888

turbine:
  app-config: service-demo3
  cluster-name-expression: "'default'"

applicatin.java

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixApp {
    public static void main( String[] args ) {
        SpringApplication.run(HystrixApp.class,args);
    }
}

cloud-service-demo3

demo3是一个应用系统实际集成使用的样例。应用系统中只需要引入hystrix组件即可,但是对于springboot项目还需要引入actuator组件。另外openfeign组件的引入是为提供另一种使用hystrix方式的样例,应用系统中有两种方式使用hystrix组件。

pom.xml:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

bootstrap.yml

spring:
  application:
    name: service-demo3
  profiles:
    active: ${ENV:local}

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

application.java

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients
public class Demo3App {
    public static void main( String[] args ) {
        SpringApplication.run(Demo3App.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;
    }

这个是hystrix2.0一个小BUG,需要添加@Bean把监控统计数据的接口暴露成/actuator/hystrix.stream接口路径方式。

使用hystrix

原生hystrix方式

@RestController
public class UserController {

    @Autowired(required = false)
    ITestService iTestService;
    @Autowired(required = false)
    IUserService iUserService;

    @GetMapping("/user/users")
    public String getUsers(){
        return "users is success" + iUserService.getUserId("13567890123");
    }

    @HystrixCommand(fallbackMethod = "defaultFindUser")
    @GetMapping("/user/find")
    public String getFindUser(){
        return iTestService.sayHello("test");
    }

    public String defaultFindUser(){
        return "hystrix default data";
    }
}

openFeign方式

OpenFeign作为一个前端路由的HTTP Client工具,自身已经集成了hystrix功能。

@FeignClient(value = "service-demo2",fallback = UserServiceFallbackImpl.class)
public interface IUserService {

    @RequestMapping(value="/user/{mobile}", method= RequestMethod.GET)
    @ResponseBody
    Long getUserId(@PathVariable(name = "mobile") String mobile);
}

@Service
public class UserServiceFallbackImpl implements IUserService {
    @Override
    public Long getUserId(String mobile) {
        System.out.println(mobile);
        return 0L;
    }
}

openfeign对hystrix的包依赖关系:

dashboard面板

指标的介绍

样例代码:https://github.com/owen-jia/cloud-parent