Spring Cloud学习:03断路器(Hystrix)
Spring Cloud Hystrix是分布式系统处理超时和错误的机制,如下图所示,分布式系统中某个用户请求依赖A、H、I、P服务。
当此请求并发超过50的时候,服务I处理速度变慢,但是服务I还是被调用。
大量请求会阻塞在Tomcat服务器上,影响其它整个服务。在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败。高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险。
Spring Cloud Hystrix就是隔离措施的一种实现,可以设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,从而提高分布式系统的稳定性。
2 测试Hystrix2.1 Ribbon使用Hystrix
2.1.1改造ribbon-service模块,在pom.xml文件中添加spring-cloud-starter-hystrix起步依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2.1.2 在启动类上添加@EnableHystrix注解开启Hystrix断路器功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonServiceApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
}
2.1.3 在测试接口上添加@HystrixCommand注解为该方法添加断路器功能,使用fallbackMethod参数指定服务异常时的熔断方法,同时添加一个服务异常时调用的方法。
@RestController
public class DemoController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/one")
@HystrixCommand(fallbackMethod = "serviceError")
public String one(){
return restTemplate.getForObject("http://ONE-SERVICE/one",String.class);
}
/**
*服务异常调用方法
* @return
*/
public String serviceError(){
return "service has error...";
}
}
2.1.4 启动服务注册中心、one-service、ribbon-service,服务正常时访问http://localhost:8764/one显示:
关闭one-service服务,访问http://localhost:8764/one 显示:
2.2 Feign使用Hystrix
2.2.1 Feign已经包含断路器功能,但默认未打开,可以在YML配置文件中配置打开:
server:
port: 8765
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: feign-service #指明应用名称(服务与服务相互调用根据name属性)
feign:
hystrix:
enabled: true #开启feign断路器功能
2.2.2 添加Feign接口断路器类,必须实现Feign接口
@Component
public class FeignDemoServiceHystrix implements FeignDemoService {
@Override
public String invocateOneService() {
return "feign service has error...";
}
}
2.2.3 在Feign接口的@FeignClient注解上添加fallback参数指定断路器类
@FeignClient(value = "one-service", fallback = FeignDemoServiceHystrix.class)
public interface FeignDemoService {
@RequestMapping(value = "/one", method = RequestMethod.GET)
String invocateOneService();
}
2.2.4 启动服务注册中心、one-service、feign-service,服务正常时访问http://localhost:8765/one显示:
关闭one-service服务,访问http://localhost:8765/one 显示:
3 Hystrix Dashboard改造ribbon-service模块,添加断路器仪表盘功能
3.1 在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-hystrix-dashboard</artifactId>
</dependency>
3.2 在启动类上添加@EnableHystrixDashboard注解开启断路器仪表盘功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonServiceApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
}
3.3 访问http://localhost:8764/hystrix如下图:
参数解释:
Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,我们可以通过配置该属性来降低客户端的网络和CPU消耗。
Title:该参数对应了Monitor Stream监控页面Hystrix Stream之后的内容,默认会使用具体监控实例的URL,我们可以通过配置该信息来展示更合适的标题。
3.4 点击Monitor Stream按钮,并请求http://localhost:8764/one,可以查看到监控数据:
本文源码下载地址:
https://github.com/laravelshao/spring-cloud-learning/tree/master/setion03-hystrix
4 参考资料Spring->How to Include Hystrix
GitHub->Netflix Hystrix
方志朋->史上最简单的SpringCloud教程
翟永超->Spring Cloud基础教程
文章来源:
Author:LaravelShao
link:https://my.oschina.net/LaravelShao/blog/1554547