熔断器Hystrix
在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。
没有加熔断器时,如果service-provider实例全部挂掉了,访问service-consumer就会显示如下报错:
这时,就需要熔断器。
在service-consumer中引入如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在service-consumer的启动类中添加@EnableCircuitBreaker
注解,如下:
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
修改service-consumer的controller方法:
在controller的方法上加@HystrixCommand
注解,并定义callback方法:
@RestController
public class ServiceConsumerController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloBack")
@RequestMapping("hello")
public String hello() {
return restTemplate.getForEntity("http://service-provider/service- provider/provider-hello"
, String.class).getBody();
}
public String helloBack() {
return "error";
}
}
这样,如果service-provider实例全部挂掉后,service-consumer就会调用我们指定的回调方法,去做响应的处理,不至于引起连锁反应。
以此启动,访问service-consumer返回结果正常,我们现在把service-provider停掉,再次访问service-consumer,发现不会出现之前的报错页面,而是返回了我们指定的回调方法的内容:
至此,熔断器配置完成。
熔断监控
Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。
下面演示如何使用Hystrix Dashboard。
首先需要在service-consumer中引入Hystrix Dashboard相关的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在启动类上添加@EnableHystrixDashboard
注解,并且注入一个名为HystrixMetricsStreamServlet
的servlet,代码如下:
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
HystrixMetricsStreamServlet hystrixMetricsStreamServlet
= new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean
= new ServletRegistrationBean<>(hystrixMetricsStreamServlet);
servletRegistrationBean.setLoadOnStartup(1);
servletRegistrationBean.addUrlMappings("/hystrix.stream");
servletRegistrationBean.setName("HystrixMetricsStreamServlet");
return servletRegistrationBean;
}
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
启动项目,访问:http://localhost:9002/hystrix,可以看到如下页面:
然后根据页面提示在页面最上面的那个输入框中输入:http://localhost:9002/hystrix.stream ,注意这里的URL也与启动类中注入servlet是指定的URLMapping一致。点击“Monitor Stream”按钮,即可看到如下页面:
至此,Hystrix Dashboard配置完成!