当前位置:首页 > 通信资讯 > 正文

spring cloud断路器作用(spring cloud熔断器)

spring cloud断路器作用(spring cloud熔断器)

环境:Springboot2.3.12.RELEASE +

cloud-netflix-hystrix2.2.10.RELEASE

简介

SpringCloud Circuit breaker(断路器)提供了跨不同断路器实现的抽象。它提供了在应用程序中使用的一致API,允许开发人员选择最适合应用程序需要的断路器实现。

支持的断路器类型:

  • Netfix Hystrix
  • Resilience4J
  • Sentinel
  • Spring Retry

核心概念

要在代码中创建断路器(circuit breaker),可以使用断路器工厂API。当您在类路径中包含Spring Cloud Circuit Breaker starter时,将自动创建一个实现此API的bean。下面给出了使用此API的一个非常简单的示例:

  1. @Service
  2. public static class DemoService {
  3. private RestTemplate rest;
  4. private CircuitBreakerFactory cbFactory;
  5. public DemoService(RestTemplate rest, CircuitBreakerFactory cbFactory) {
  6. this.rest = rest;
  7. this.cbFactory = cbFactory;
  8. }
  9. public String slow() {
  10. // 通过默认的CircuitBreakerFactory工厂创建一个指定id(名称)的断路器
  11. // run方法是实际执行你的业务方法,第二个参数throwable 是当发生异常或者是执行超时
  12. // 执行的回退(降级)处理
  13. return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), throwable -> "fallback");
  14. }
  15. }

项目配置

通过引入下面不同依赖来确定使用具体的那个断路器

  • Hystrix - org.springframework.cloud:spring-cloud-starter-netflix-hystrix
  • Resilience4J - org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j
  • Reactive Resilience4J - org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j
  • Spring Retry - org.springframework.cloud:spring-cloud-starter-circuitbreaker-spring-retry
  • Sentinal - org.springframework.cloud:spring-cloud-starter-circuitbreaker-sentinal

以上5种断路器是不同的实现方式,根据需要引入即可。

示例

这里以Hystrix为例来使用

引入依赖

  1. org.springframework.cloud
  2. spring-cloud-starter-netflix-hystrix
  3. 2.2.10.RELEASE

定义具有熔断功能的服务

  1. @Service
  2. public class DemoService {
  3. private RestTemplate rest;
  4. // 注入系统默认的实现
  5. private CircuitBreakerFactory cbFactory;
  6. public DemoService(RestTemplate rest, CircuitBreakerFactory cbFactory) {
  7. this.rest = rest;
  8. this.cbFactory = cbFactory;
  9. }
  10. public String slow() {
  11. // 使用系统默认的实现创建断路器进行业务的处理
  12. return cbFactory.create("slow").run(() -> rest.getForObject("http://localhost:8080/demos/slow", String.class), throwable -> "fallback");
  13. }
  14. public String slow2() {
  15. // 使用自定义的断路器工厂进行业务的处理
  16. return cbf().create("demo-slow").run(() -> rest.getForObject("http://localhost:8080/demos/slow", String.class), throwable -> "fallback");
  17. }
  18. // 可以将这个定义为Bean来覆盖系统默认的实现,在系统默认的实现上有条件限定
  19. private CircuitBreakerFactory cbf() {
  20. HystrixCircuitBreakerFactory cbf = new HystrixCircuitBreakerFactory() ;
  21. // 配置线程池
  22. HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter() ;
  23. threadPoolProperties.withCoreSize(5)
  24. .withKeepAliveTimeMinutes(5)
  25. .withMaxQueueSize(Integer.MAX_VALUE)
  26. .withQueueSizeRejectionThreshold(1000) ;
  27. // 配置默认的执行行为属性
  28. HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter() ;
  29. commandProperties.withCircuitBreakerEnabled(true)
  30. // 当请求超过了3s那么断路器就会工作进行回退(降级处理),执行上面run方法中的第二个参数
  31. .withExecutionTimeoutInMilliseconds(3000)
  32. .withRequestCacheEnabled(true)
  33. // 隔离策略有两种THREAD,SEMAPHORE
  34. // THREAD: 避免线程被阻塞
  35. // SEMAPHORE: 适合高并发限流处理;因为线程池的方式一般不会创建过多的线程
  36. // 线程是有限的,在高并发情况下是没法满足响应处理的。
  37. .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD);
  38. // 将其加入到集合中,为不同的服务创建不同的配置
  39. cbf.configure(builder -> {
  40. builder.commandProperties(commandProperties).groupName("demo") ;
  41. }, "demo-slow");
  42. // 当默认的id不存在时使用这默认的配置
  43. cbf.configureDefault(id -> {
  44. HystrixCommand.Setter setter = HystrixCommand.Setter
  45. .withGroupKey(HystrixCommandGroupKey.Factory.asKey("demo")) // 服务分组,大的模块
  46. .andCommandKey(HystrixCommandKey.Factory.asKey("demo-slow")) // 服务标识(具体服务分组中的某一个子的服务),子模块
  47. .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("demo-pools")) // 线程池名称
  48. .andThreadPoolPropertiesDefaults(threadPoolProperties) // 线程池相关配置
  49. .andCommandPropertiesDefaults(commandProperties) ; // 执行时相关属性配置
  50. return setter ;
  51. });
  52. return cbf ;
  53. }
  54. }

Controller接口

  1. @RestController
  2. @RequestMapping("/demos")
  3. public class DemoController {
  4. @Resource
  5. private DemoService demoService ;
  6. @GetMapping("/index")
  7. public Object index() {
  8. return demoService.slow2() ;
  9. }
  10. @GetMapping("/slow")
  11. public Object slow() {
  12. try {
  13. TimeUnit.SECONDS.sleep(5) ;
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. return "slow" ;
  18. }
  19. }

原理

CircuitBreakerFactory#create方法创建了CircuitBreaker实例

根据当前的CLASSPATH我们使用的是Hystrix,那么这里使用的工厂就是:

HystrixCircuitBreakerFactory类

  1. public class HystrixCircuitBreakerFactory extends CircuitBreakerFactory {

泛型参数:Setter就是用来配置Hystrix相关配置信息的(这里主要用来CommandKey与Setter进行绑定),HystrixConfigBuilder用来构建 HystrixCommand.Setter对象。

当执行HystrixCircuitBreakerFactory#configure方法时:

  1. public abstract class AbstractCircuitBreakerFactory
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。