如果您配置过断路器,对这个ReactiveResilience4JCircuitBreakerFactory就很熟悉了,设置该对像是配置断路器的基本操作,回顾一下前文的代码:
@Configuration public class CustomizeCircuitBreakerConfig { @Bean public ReactiveResilience4JCircuitBreakerFactory defaultCustomizer() { CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() // .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.TIME_BASED) // 滑动窗口的类型为时间窗口 .slidingWindowSize(10) // 时间窗口的大小为60秒 .minimumNumberOfCalls(5) // 在单位时间窗口内最少需要5次调用才能开始进行统计计算 .failureRateThreshold(50) // 在单位时间窗口内调用失败率达到50%后会启动断路器 .enableAutomaticTransitionFromOpenToHalfOpen() // 允许断路器自动由打开状态转换为半开状态 .permittedNumberOfCallsInHalfOpenState(5) // 在半开状态下允许进行正常调用的次数 .waitDurationInOpenState(Duration.ofSeconds(5)) // 断路器打开状态转换为半开状态需要等待60秒 .recordExceptions(Throwable.class) // 所有异常都当作失败来处理 .build(); ReactiveResilience4JCircuitBreakerFactory factory = new ReactiveResilience4JCircuitBreakerFactory(); factory.configureDefault(id -> new Resilience4JConfigBuilder(id) .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(200)).build()) .circuitBreakerConfig(circuitBreakerConfig).build()); return factory; } }
既然ReactiveResilience4JCircuitBreakerFactory是spring的bean,那我们在StatePrinterGatewayFilterFactory类中用Autowired注解就能随意使用了
至此,理论分析已全部完成,问题都已经解决,开始编码
源码下载本篇实战中的完整源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
名称 链接 备注项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
这个git项目中有多个文件夹,本篇的源码在spring-cloud-tutorials文件夹下,如下图红框所示:
spring-cloud-tutorials文件夹下有多个子工程,本篇的代码是circuitbreaker-gateway,如下图红框所示:
编码
前文创建了子工程circuitbreaker-gateway,此工程已添加了断路器,现在咱们的过滤器代码就写在这个工程中是最合适的了
接下来按照套路写代码,首先是StatePrinterGatewayFilter.java,代码中有详细注释就不再啰嗦了,要注意的是getOrder方法返回值是10,这表示过滤器的执行顺序:
package com.bolingcavalry.circuitbreakergateway.filter; import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; import io.vavr.collection.Seq; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.lang.reflect.Method; public class StatePrinterGatewayFilter implements GatewayFilter, Ordered { private ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory; // 通过构造方法取得reactiveResilience4JCircuitBreakerFactory实例 public StatePrinterGatewayFilter(ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory) { this.reactiveResilience4JCircuitBreakerFactory = reactiveResilience4JCircuitBreakerFactory; } private CircuitBreaker circuitBreaker = null; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 这里没有考虑并发的情况,如果是生产环境,请您自行添加上锁的逻辑 if (null==circuitBreaker) { CircuitBreakerRegistry circuitBreakerRegistry = null; try { Method method = reactiveResilience4JCircuitBreakerFactory.getClass().getDeclaredMethod("getCircuitBreakerRegistry",(Class[]) null); // 用反射将getCircuitBreakerRegistry方法设置为可访问 method.setAccessible(true); // 用反射执行getCircuitBreakerRegistry方法,得到circuitBreakerRegistry circuitBreakerRegistry = (CircuitBreakerRegistry)method.invoke(reactiveResilience4JCircuitBreakerFactory); } catch (Exception exception) { exception.printStackTrace(); } // 得到所有断路器实例 Seq<CircuitBreaker> seq = circuitBreakerRegistry.getAllCircuitBreakers(); // 用名字过滤,myCircuitBreaker来自路由配置中 circuitBreaker = seq.filter(breaker -> breaker.getName().equals("myCircuitBreaker")) .getOrNull(); } // 取断路器状态,再判空一次,因为上面的操作未必能取到circuitBreaker String state = (null==circuitBreaker) ? "unknown" : circuitBreaker.getState().name(); System.out.println("state : " + state); // 继续执行后面的逻辑 return chain.filter(exchange); } @Override public int getOrder() { return 10; } }