|
@@ -0,0 +1,49 @@
|
|
1
|
+package com.xhkjedu.gateway.config;
|
|
2
|
+
|
|
3
|
+import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
|
|
4
|
+import io.github.resilience4j.timelimiter.TimeLimiterConfig;
|
|
5
|
+import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory;
|
|
6
|
+import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
|
|
7
|
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
|
8
|
+import org.springframework.context.annotation.Bean;
|
|
9
|
+import org.springframework.context.annotation.Configuration;
|
|
10
|
+
|
|
11
|
+import java.time.Duration;
|
|
12
|
+
|
|
13
|
+/**
|
|
14
|
+ * @ClassName CustomizeCircuitBreakerConfig
|
|
15
|
+ * Description TODO
|
|
16
|
+ * Author WN
|
|
17
|
+ * Date 2022/2/16 10:59
|
|
18
|
+ **/
|
|
19
|
+@Configuration
|
|
20
|
+@EnableEurekaClient
|
|
21
|
+public class CustomizeCircuitBreakerConfig {
|
|
22
|
+
|
|
23
|
+ @Bean
|
|
24
|
+ public ReactiveResilience4JCircuitBreakerFactory defaultCustomizer(){
|
|
25
|
+
|
|
26
|
+ CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() //
|
|
27
|
+ .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.TIME_BASED) // 滑动窗口的类型为时间窗口
|
|
28
|
+ .slidingWindowSize(3) // 时间窗口的大小为2秒
|
|
29
|
+ .minimumNumberOfCalls(2) // 在单位时间窗口内最少需要1次调用才能开始进行统计计算
|
|
30
|
+ .failureRateThreshold(50) // 在单位时间窗口内调用失败率达到50%后会启动断路器
|
|
31
|
+ .enableAutomaticTransitionFromOpenToHalfOpen() // 允许断路器自动由打开状态转换为半开状态
|
|
32
|
+ .permittedNumberOfCallsInHalfOpenState(5) // 在半开状态下允许进行正常调用的次数
|
|
33
|
+ .waitDurationInOpenState(Duration.ofSeconds(5)) // 断路器打开状态转换为半开状态需要等待60秒
|
|
34
|
+ .recordExceptions(Throwable.class) // 所有异常都当作失败来处理
|
|
35
|
+ .build();
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+ ReactiveResilience4JCircuitBreakerFactory factory = new ReactiveResilience4JCircuitBreakerFactory();
|
|
39
|
+ factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
|
|
40
|
+ //超时规则,设置为3s
|
|
41
|
+ .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(3000)).build())
|
|
42
|
+ //设置断路器配置
|
|
43
|
+ .circuitBreakerConfig(circuitBreakerConfig).build());
|
|
44
|
+
|
|
45
|
+ return factory;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+}
|