第一章 Hystrix熔断器介绍
Hystrix是Netflix公司开源的一个项目,提供了熔断器功能,能够阻止分布式系统中出现联动故障。
Hystrix是通过隔离服务的访问点阻止联动故障的,并提供了故障的解决方案,从而提供整个分布式系统的弹性。
1.1. Hystrix设计原则
- 防止单个服务的故障耗尽整个服务的servlet容器的线程资源
- 快速失败机制,如果某个服务出现故障,调用该服务的请求快速失败,而不是线程等待
- 提供回退方案(fallback),请求发生故障时,提供设定好的回退方案
- 使用熔断机制,防止故障扩散到其他服务
- 提供监控组件Hystrix Dashbord, 实时监控熔断器的状态
1.2. Hystrix工作机制
- 当服务的API接口的失败次数在一定时间内小于设定的阀值时,熔断器处于关闭状态。正常提供服务。
- 当服务的API接口的失败次数在一定时间内大于设定的阀值时,打开熔断器,执行快速失败逻辑,不执行业务逻辑,请求线程不会处于阻塞状态。
- 处于打开状态的熔断器,一段时间后会处于半打开状态,并将一定数量的请求执行正常逻辑,剩余请求执行快速失败。若执行正常逻辑的请求失败了,熔断器继续打开,如果成功了,将熔断器关闭,既熔断器的修复功能。
第二章 Ribbon和RestTemplate结合时使用熔断器
2.1. 引入依赖
<!-- Hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
旧版本为:spring-cloud-starter-hystrix
2.2. 启动器
开启熔断器功能:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableEurekaClient @EnableHystrix public class ConsumerApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ConsumerApplication.class, args); } }
2.3. 熔断器
引入fallbackMethod 和 编写fallback method
@RequestMapping("/cust/get/{id}") @HystrixCommand(fallbackMethod = "getCustInfoByIdFallback") public CustInfo getCustInfoById(@PathVariable("id") Long id) { log.debug("Consumer_CustInfoController getCustInfoById is running..."); return restTemplate.getForObject(PROVIDER_PRIFIX + "/cust_info/selectid/" + id, CustInfo.class); } public CustInfo getCustInfoByIdFallback(@PathVariable("id") Long id) { CustInfo custInfo = new CustInfo(); custInfo.setId(id); custInfo.setUsername("fallback"); return custInfo; }
第三章 Ribbon和Feign结合时使用熔断器
3.1. 引入依赖
Feign的起步依赖中已经引入了Hystrix的依赖,所有不需要引入依赖了。
3.2. Yml配置
开启hystrix功能:
feign: hystrix: enabled: true
3.3. 在FeignClient中引入fallback类
3.3.1. 用fallback时
@FeignClient(value = "EUREKA-PROVIDER", configuration = FeignConfig.class,fallback = CustEurekaClientFeignFallback.class) public interface CustEurekaClientFeign { @GetMapping("/cust_info/selectid/{id}") public String getCustInfoById(@RequestParam(value="id") Long id); @GetMapping("/cust_info/getServerInfo") public String getServerInfo(); }
3.3.2. 当用FallbackFactory时
@FeignClient(value = "EUREKA-PROVIDER", configuration = FeignConfig.class,fallbackFactory = CustEurekaClientFeignFallbackFactory.class) public interface CustEurekaClientFeign { @GetMapping("/cust_info/selectid/{id}") public String getCustInfoById(@RequestParam(value="id") Long id); @GetMapping("/cust_info/getServerInfo") public String getServerInfo(); }
3.4. 编写Fallback类
3.4.1. 用fallback时
@Component public class CustEurekaClientFeignFallback implements CustEurekaClientFeign{ @Override public String getCustInfoById(Long id) { return "getCustInfoById is fall, this is hystrix back info for id:" + id; } @Override public String getServerInfo() { return "getServerInfo is fall, this is hystrix back info"; } }
3.4.2. 当用FallbackFactory时
@Component public class CustEurekaClientFeignFallbackFactory implements FallbackFactory<CustEurekaClientFeign>{ @Override public CustEurekaClientFeign create(Throwable cause) { return new CustEurekaClientFeign() { @Override public String getCustInfoById(Long id) { return "getCustInfoById is fall, this is hystrix back info for id:" + id; } @Override public String getServerInfo() { return "getServerInfo is fall, this is hystrix back info"; } }; } }
第四章 RestTemplate中使用Hystrix dashboard
4.1. 引入依赖
<!-- Hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Hystrix dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
4.2. 启动器
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableEurekaClient @EnableHystrix @EnableHystrixDashboard public class ConsumerApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ConsumerApplication.class, args); } }
4.3. 访问
http://127.0.0.1/hystrix.stream
第五章 Feign中使用hystrix dashboard
5.1. 引入依赖
<!-- Hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Hystrix dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
为什么还要引入spring-cloud-starter-netflix-hystrix?
因为Feign中引入的不是hystrix起步依赖
5.2. 启动器
加入@EnableHystrixDashboard
第六章 Turbine聚合监控
新建工程:
turbine-actuator
6.1. 引入依赖
<!-- turbine --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <!-- Hystrix dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!-- actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
6.2. 启动器
@SpringBootApplication @EnableTurbine public class Turbine_StartSpringCloudApp { public static void main(String[] args) throws Exception { SpringApplication.run(Turbine_StartSpringCloudApp.class, args); } }
6.3. Yml配置
server: port: 8700 spring: application: name: turbind-actuator eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://eureka-server-7001:7001/eureka/ instance: instance-id: turbind-actuator-8700 prefer-ip-address: true turbine: aggregator: clusterConfig: default appConfig: eureka-provider,consumer-80 clusterNameExpression: new String("default")
6.4. 访问
http://127.0.0.1:8700/turbine.stream
汇总: