Spring Cloud Hystrix是分布式系统处理超时和错误的机制,以下图所示,分布式系统中某个用户请求依赖A、H、I、P服务。html
当此请求并发超过50的时候,服务I处理速度变慢,可是服务I仍是被调用。java
大量请求会阻塞在Tomcat服务器上,影响其它整个服务。在复杂的分布式架构的应用程序有不少的依赖,都会不可避免地在某些时候失败。高并发的依赖失败时若是没有隔离措施,当前应用服务就有被拖垮的风险。laravel
Spring Cloud Hystrix就是隔离措施的一种实现,能够设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,从而提升分布式系统的稳定性。git
2.1.1改造ribbon-service模块,在pom.xml文件中添加spring-cloud-starter-hystrix起步依赖github
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
2.1.2 在启动类上添加@EnableHystrix注解开启Hystrix断路器功能spring
@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.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 显示:
改造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