环境:Springboot2.3.12.RELEASE + cloud-netflix-hystrix2.2.10.RELEASE SpringCloud Circuit breaker(断路器)提供了跨不同断路器实现的断路抽象。它提供了在应用程序中使用的应用一致API,允许开发人员选择最适合应用程序需要的断路断路器实现。 要在代码中创建断路器(circuit breaker),可以使用断路器工厂API。断路当您在类路径中包含Spring Cloud Circuit Breaker starter时,应用将自动创建一个实现此API的断路bean。下面给出了使用此API的应用一个非常简单的示例: 通过引入下面不同依赖来确定使用具体的那个断路器: 以上5种断路器是不同的实现方式,根据需要引入即可。应用 这里以Hystrix为例来使用 CircuitBreakerFactory#create方法创建了CircuitBreaker实例。 根据当前的CLASSPATH我们使用的服务器托管是Hystrix,那么这里使用的工厂就是: HystrixCircuitBreakerFactory类 泛型参数:Setter就是用来配置Hystrix相关配置信息的(这里主要用来CommandKey与Setter进行绑定),HystrixConfigBuilder用来构建 HystrixCommand.Setter对象。 当执行HystrixCircuitBreakerFactory#configure方法时: HystrixCircuitBreakerFactory 断路器工厂有了,接下来就是通过工厂创建具体的断路器对象了。 通过上面的代码执行cbf().create("demo-slow")方法时执行了什么? 上面创建的是HystrixCircuitBreaker断路器,当执行run方法时:简介
支持的应用断路器类型:
Netfix Hystrix Resilience4J Sentinel Spring Retry 核心概念
项目配置
示例
引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.10.RELEASE</version> </dependency> 定义具有熔断功能的断路服务
@Service public class DemoService { private RestTemplate rest; // 注入系统默认的实现 private CircuitBreakerFactory cbFactory; public DemoService(RestTemplate rest, CircuitBreakerFactory cbFactory) { this.rest = rest; this.cbFactory = cbFactory; } public String slow() { // 使用系统默认的实现创建断路器进行业务的源码库处理 return cbFactory.create("slow").run(() -> rest.getForObject("http://localhost:8080/demos/slow", String.class), throwable -> "fallback"); } public String slow2() { // 使用自定义的断路器工厂进行业务的处理 return cbf().create("demo-slow").run(() -> rest.getForObject("http://localhost:8080/demos/slow", String.class), throwable -> "fallback"); } // 可以将这个定义为Bean来覆盖系统默认的实现,在系统默认的应用实现上有条件限定 private CircuitBreakerFactory<HystrixCommand.Setter, HystrixCircuitBreakerFactory.HystrixConfigBuilder> cbf() { HystrixCircuitBreakerFactory cbf = new HystrixCircuitBreakerFactory() ; // 配置线程池 HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter() ; threadPoolProperties.withCoreSize(5) .withKeepAliveTimeMinutes(5) .withMaxQueueSize(Integer.MAX_VALUE) .withQueueSizeRejectionThreshold(1000) ; // 配置默认的执行行为属性 HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter() ; commandProperties.withCircuitBreakerEnabled(true) // 当请求超过了3s那么断路器就会工作进行回退(降级处理),执行上面run方法中的断路第二个参数 .withExecutionTimeoutInMilliseconds(3000) .withRequestCacheEnabled(true) // 隔离策略有两种THREAD,SEMAPHORE // THREAD: 避免线程被阻塞 // SEMAPHORE: 适合高并发限流处理;因为线程池的应用方式一般不会创建过多的线程 // 线程是有限的,在高并发情况下是断路没法满足响应处理的。 .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD); // 将其加入到集合中,为不同的服务创建不同的配置 cbf.configure(builder -> { builder.commandProperties(commandProperties).groupName("demo") ; }, "demo-slow"); // 当默认的id不存在时使用这默认的配置 cbf.configureDefault(id -> { HystrixCommand.Setter setter = HystrixCommand.Setter .withGroupKey(HystrixCommandGroupKey.Factory.asKey("demo")) // 服务分组,大的模块 .andCommandKey(HystrixCommandKey.Factory.asKey("demo-slow")) // 服务标识(具体服务分组中的某一个子的服务),子模块 .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("demo-pools")) // 线程池名称 .andThreadPoolPropertiesDefaults(threadPoolProperties) // 线程池相关配置 .andCommandPropertiesDefaults(commandProperties) ; // 执行时相关属性配置 return setter ; }); return cbf ; } } Controller接口
@RestController @RequestMapping("/demos") public class DemoController { @Resource private DemoService demoService ; @GetMapping("/index") public Object index() { return demoService.slow2() ; } @GetMapping("/slow") public Object slow() { try { TimeUnit.SECONDS.sleep(5) ; } catch (InterruptedException e) { e.printStackTrace(); } return "slow" ; } } 原理
断路器具体的子类实现