Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
Hystrix是一个延迟和容错库,旨在隔离远程系统、服务和第三方库的访问点,停止次级故障,并且不可避免的复杂分布式系统中实现自我恢复能力。
pom
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.67version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
application.yml
spring:
application:
name: eureka-consumer-feign-hystrix
# 当前应用名称
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
# 映射到Eureka server的路径
server:
port: 9001
feign:
hystrix:
enabled: true
# an OpenFeign client will be wrapped with a Hystrix circuit breaker
# 一个OpenFeign客户端将包装有Hystrix断路器
management:
endpoints:
web:
exposure:
include: hystrix.stream
# 为了给Hystrix Dashboard提供使用
启动类配置
@EnableFeignClients
//扫描包下被@FeignClient注解的,需要和@Configuration同时使用
@EnableHystrix
//开启断路器,并且自动配置找到Hystrix类(if they are available)
@SpringBootApplication
public class CloudConsumerFeignHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConsumerFeignHystrixApplication.class, args);
}
}
远程调用
@Component
// name: the service id with optional protocol prefix.带有可选协议前缀的服务ID
// fallback: 指定的Feign客户端接口的后备类。fallback类必须实现该接口并且是一个有效的spring bean
//Fallback class for the specified Feign client interface. The fallback class must
// implement the interface annotated by this annotation and be a valid spring bean.
@FeignClient(name = "eureka-producer",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
@RequestMapping("/hello")
String hello(@RequestBody JSONObject requestJSON);
}
fallback类
@Component
public class HelloRemoteHystrix implements HelloRemote {
@Override
public String hello(JSONObject requestJSON) {
return "hello world";
}
}
controller
@RestController
public class HelloController {
@Resource
private HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String hello(@PathVariable(name = "name")String name){
JSONObject requestJSON = new JSONObject();
requestJSON.put("name",name);
return helloRemote.hello(requestJSON);
}
}
测试
将eureka-server,producer,feign-hystrix三个项目启动,在eureka-server上可以看到producer和feign-hystrix都已经注册成功。
访问:localhost:9001/hello/huskyui
hello, huskyui Tue Mar 24 14:34:44 CST 2020
将producer关闭
访问:localhost:9001/hello/huskyui
hello world
再次将producer启动
hello, huskyui Tue Mar 24 14:40:27 CST 2020
这边,可以看到熔断能力,以及自我恢复能力
pom
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
application.yml
spring:
application:
name: hystrix-dashboard
server:
port: 11000
添加注解@EnableHystrixDashboard
@SpringBootApplication
@EnableHystrixDashboard
public class CloudHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(CloudHystrixDashboardApplication.class, args);
}
}
测试
运行该项目
访问: http://localhost:11000/hystrix
在启动后输入http://localhost:9001/actuator/hystrix.stream
9001是feign-hystrix的项目的端口,当时我们引入了actuator,开放hystrix.stream
可以看到dashboard相关