Browse Source

网关

tags/正式3.2.0
王宁 2 years ago
parent
commit
6324d3ada9

+ 6
- 2
gateway/pom.xml View File

@@ -22,14 +22,18 @@
22 22
             <groupId>org.springframework.cloud</groupId>
23 23
             <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
24 24
         </dependency>
25
-        <dependency>
25
+        <!--<dependency>
26 26
             <groupId>org.springframework.cloud</groupId>
27 27
             <artifactId>spring-cloud-starter-config</artifactId>
28
-        </dependency>
28
+        </dependency>-->
29 29
         <dependency>
30 30
             <groupId>org.springframework.cloud</groupId>
31 31
             <artifactId>spring-cloud-starter-gateway</artifactId>
32 32
         </dependency>
33
+        <dependency>
34
+            <groupId>org.springframework.cloud</groupId>
35
+            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
36
+        </dependency>
33 37
 
34 38
         <dependency>
35 39
             <groupId>org.springframework.boot</groupId>

+ 49
- 0
gateway/src/main/java/com/xhkjedu/gateway/config/CustomizeCircuitBreakerConfig.java View File

@@ -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
+}

+ 26
- 0
gateway/src/main/java/com/xhkjedu/gateway/controller/ErrorController.java View File

@@ -0,0 +1,26 @@
1
+package com.xhkjedu.gateway.controller;
2
+
3
+import org.springframework.web.bind.annotation.RequestMapping;
4
+import org.springframework.web.bind.annotation.RestController;
5
+
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
9
+/**
10
+ * @ClassName ErrorController
11
+ * Description TODO
12
+ * Author WN
13
+ * Date 2022/2/14 14:22
14
+ **/
15
+@RestController
16
+public class ErrorController {
17
+
18
+    @RequestMapping("/fallback")
19
+    public Object fallback(){
20
+        Map<String,Object> result = new HashMap<>();
21
+        result.put("code",1);
22
+        result.put("msg","服务器繁忙了!");
23
+        result.put("state",false);
24
+        return result;
25
+    }
26
+}

+ 10
- 0
gateway/src/main/resources/application.properties View File

@@ -1,3 +1,13 @@
1 1
 server.port=8080
2 2
 
3 3
 spring.application.name=gateway
4
+
5
+spring.cloud.gateway.routes[0].id=path_sstudy
6
+spring.cloud.gateway.routes[0].uri=lb://sstudy
7
+spring.cloud.gateway.routes[0].predicates[0]=Path=/sstudy/**
8
+spring.cloud.gateway.routes[0].filters[0]=RewritePath=/sstudy/paper/(?<segment>.*), /sstudy/$\{segment}
9
+spring.cloud.gateway.routes[0].filters[1].name=CircuitBreaker
10
+spring.cloud.gateway.routes[0].filters[1].args.name=myCircuitBreaker
11
+spring.cloud.gateway.routes[0].filters[1].args.fallbackUri=forward:/fallback
12
+
13
+eureka.client.service-url.defaultZone=http://localhost:8081/eureka/

Loading…
Cancel
Save