在微服务架构中,我们的服务被拆分成多个微服务,每个微服务完成自己的职责,微服务之间通过rpc或http进行调用。这个时候我们就要确保我们的服务高可用,但谁也说不准我们的服务能永远快速的提供服务。假如现在发生了这个一种情况 A->B->C->D->E 即A服务调用B服务,B调用C服务,C调用D服务,D调用E服务,这个时候我们的E服务过载,响应速度特别慢,当并发高时,我们的请求就会堆积在E服务,E服务无法返回,导致请求堆积在D服务,以此类推,就会导致请求依次堆积在我们的这个调用链上,最终导致所有的服务都不可用,形成服务的雪崩。下面以一个简单的图来表示服务的雪崩。
为了解决以上问题,spring cloud中为我们提供了 hystrix 断路器来保护我们的应用程序。
断路器机制:hystrix存在三种状态:CLOSED、OPEN和HALF_OPEN。默认情况下为CLOSED,当一个服务在一定的时间内(metrics.rollingStats.timeInMilliseconds默认10s),请求次数达到了某个阀值(circuitBreaker.requestVolumeThreshold默认20次),并且错误率也达到了某个阀值(circuitBreaker.errorThresholdPercentage
默认>50%),此时断路器变成了OPEN的状态,当断路器打开,过了一定的时间(circuitBreaker.sleepWindowInMilliseconds默认为5s)将会放行一个请求,此时变成HALF_OPEN状态,如果可以访问就变成CLOSED否则变成OPEN状态
资源隔离:hystrix为每个依赖都提供了一个线程池或信号量。当线程池满了之后,发往该依赖的请求会被直接拒绝,从而加速失败。
注意: 进入fallback方法并不意味者断路器一定是打开的,请求失败、超时、被拒绝以及断路器打开时都会执行回退逻辑。
需求:
1、在feign中使用hystrix
2、在降级方法中获取到为什么进入了降级方法
代码结构:
eureka-server
|- 服务注册中心
hystrix
product-provider-8091
|- 服务提供者
product-consumer-feign-hystrix-8093
|- feign 和 hystrix的整合
feign
fallback
ProductServiceFeignFallback(回退实现)
ProductServiceFeignFallbackFactory(可以获知为何进入回退)
代码实现
一、注册中心和服务提供者的实现(见下方的代码)
二、服务消费者(需要引入hystrix)
1、feign和hystrix引入
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、启动方法上增加@EnableCircuitBreaker 注解
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker public class ApplicationProductConsumer8093 { public static void main(String[] args) { SpringApplication.run(ApplicationProductConsumer8093.class, args); } }
3、配置文件中在feign中启动hystrix
feign: hystrix: enabled: true
4、回退的实现,fallback的实现
注意: fallbackFatory 需要注意的事项
5、控制层的写法
/** * 商品控制器 * * @author huan.fu * @date 2018/5/30 - 16:52 */ @RestController @RequestMapping("product") public class ProductController { @Autowired private ProductService01Feign productService01Feign; @Autowired private ProductService02Feign productService02Feign; /** * 获取商品信息 * * @param productId * @return */ @GetMapping("/01/selectOne/{productId}") public Map<String, Object> select01ByProductId(@PathVariable String productId) { return productService01Feign.selectByProductId(productId); } /** * 获取商品信息 * * @param productId * @return */ @GetMapping("/02/selectOne/{productId}") public Map<String, Object> select02ByProductId(@PathVariable String productId) { return productService02Feign.selectByProductId(productId); } }
/product/01/selectOne/{productId} ===> fallback 回退
/product/02/selectOne/{productId} ===> fallbackFactory 回退,可以知道回退的原因
6、测试
测试的服务启动者先启动,然后停止看效果。
完整代码
https://gitee.com/huan1993/spring-cloud-parent/tree/master/hystrix
相关推荐
详细讲解了如何在Spring Cloud中使用Hystrix,如何将Hystrix整合到Feign中
spring cloud
springcloud2 hystrix feign ribbon 博客地址:https://blog.csdn.net/qq_36963950/article/details/107428237
Spring Cloud Netfix Hystrix断路器例子工程。使用Spring Cloud Netflix Hystrix以及Spring RestTemplate或Spring Cloud Netflix Feign实现断路器模式。
spring cloud hystrix通过服务隔离、熔断、降级等手段控制依赖服务的延迟与失败。
SpringCloud10-Hystrix熔断器及服务熔断
spring cloud eureka zuul ribbon hystrix feign config 示例代码
Spring Cloud(Hystrix)使用,通过代码学习Spring Cloud(Hystrix)使用.。
SpringCloud10-Hystrix熔断器学习代码及指定默认的全局兜底方法
Spring Cloud断路器Hystrix上文讲到我们服务间调用使用Feign——声明式Web服务客户端,在分布式系统中,一个服务很可能会调用多个其他微服务,
springcloud feign整合hystrix 示例,希望对大家有所帮助
10.Spring Cloud中的断路器Hystrix 11.Spring Cloud自定义Hystrix请求命令 12.Spring Cloud中Hystrix的服务降级与异常处理 13.Spring Cloud中Hystrix的请求缓存 14.Spring Cloud中Hystrix的请求合并 15.Spring ...
springcloud hystrix 断路由 http://knight-black-bob.iteye.com/blog/2356431
springcloud hystrix jar包,java -jar xx.jar,小熊界面
spring-cloud-netflix-hystrix应用
spring cloud降级服务-hystrix.7z
Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh spring-cloud-config-bus-rabbitMQ Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
feign中包含了hystrix以及ribbon,即feign在不导入hystrix和ribbon的依赖下也能完成他们所能实现的功能,当然,如果想使用hystrix和ribbon自带的注解以及配置,必须导入依赖才可以,feign结合hystrix实现熔断+降级,...
05Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用1