1、Hystrix简介
在微服务架构中,服务与服务之间会存在相互调用、依赖的情况,假如其中某一个由于网络原因或本身就挂了导致服务不可用,大量请求进来可能就会把系统资源用尽,导致其他服务都不可用,这不是我们希望看到的,你自己挂了就挂了,还把其他服务都搞挂了就不好了,这也就是常说的故障传播,雪崩效应,而Hystrix就是来解决这个问题,当系统中某个服务发生故障或当调用一个特定的服务达到一定阈值(默认5秒失败20次) 后,通过断路器的故障监控,会返回一个FallBack响应,而不是长时间的等待,这样就保证了服务调用方的线程不会长时间占用系统资源,避免故障蔓延。
2、在Ribbon中使用Hystrix,基于这篇文章改造:SpringCloud:服务消费(Ribbon+RestTemplate)
新增依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在启动类上加上@EnableHystrix,开启Hystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
在接口上使用@HystrixCommand注解,fallbackMethod指的是当该接口出现故障是会去调用error方法,这里返回一个固定字符串
/**
* @author XuJD
* @create 2019-11-22 16:02
**/
@Service
public class TestService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "error")
public String testService(){
return restTemplate.getForObject("http://eureka-client1/serviceTest/test",String.class);
}
public String error(){
return " error";
}
}
依次启动项目(eureka-server\eureka-client1\service-ribbon),这里eureka-client1依然是起了两个实例,怎么起多个实例可以看这篇文章:SpringCloud:服务消费(Ribbon+RestTemplate)
已经起了两个实例
启动ribbon后访问:http://localhost:8003/test/hello,可以看到8001和8002正常轮询打印
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002
这时我们关闭8002这个端口的实例
可以看到只能访问到8001这个实例,当访问到8002的时候打印了 error,说明Hystrix已经起作用了
3、在Feign中使用Hystrix,基于上篇文章改造:SpringCloud:服务消费(Feign)
Feign默认集成了Hystrix,但是默认是关闭的,手动开启下,在配置文件加上
feign:
hystrix:
enabled: true
指定服务故障时需要调用的方法,这里FeignServiceHystrix是FeignService的实现类,服务故障时会掉这个实现类里面重写的方法
/**
* @author XuJD
* @create 2019-11-26 11:13
**/
@FeignClient(value = "eureka-client1",fallback = FeignServiceHystrix.class)
@Service
public interface FeignService {
@RequestMapping(value = "/serviceTest/test")
String test();
}
/**
* @author XuJD
* @create 2019-11-28 11:13
**/
@Component
public class FeignServiceHystrix implements FeignService {
@Override
public String test() {
return "error";
}
}
启动项目(eureka-server\eureka-client1\service-feign),eureka-client1依然起的是两个实例,
访问:http://localhost:8003/test/hello,可以看到8001和8002两个实例正常访问
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002
我们关闭其中8001的实例再看下,只会出现8002或 error
Hello world,this is eureka-client1--8002
error
4、Hystrix Dashboard 使用(Hystrix仪表盘)
这里就基于上面的代码修改(service-feign工程),pom.xml新增依赖
<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>
配置文件新增一个配置,并注册一个bean,不然打开仪表盘页面会出现:
Unable to connect to Command Metric Stream
新增配置
management:
endpoints:
web:
exposure:
include: hystrix.stream
注册这个bean
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
ok,启动工程并打开:http://localhost:8003/hystrix
输入地址:http://localhost:8003/hystrix.stream,Title随便取,点击Monitor Stream,刷新几次接口这里可以看到变化
源代码:https://github.com/xujiangdong/SpringCloud_Study