From 7ec7fc1e4fdeacb0acc178a2b9269da377b47e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sun, 6 Nov 2022 00:18:46 +0800 Subject: [PATCH] =?UTF-8?q?Gateway=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++ backend/.idea/runConfigurations/Gateway.xml | 16 ++++++++++++ backend/microservice-gateway/pom.xml | 1 + .../cxyxiaomo/epp/controller/Controller.java | 13 ++++++++++ .../src/main/resources/application.yml | 26 ++++++++++++++++--- .../epp/controller/TestController.java | 11 ++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 backend/.idea/runConfigurations/Gateway.xml create mode 100644 backend/microservice-gateway/src/main/java/com/cxyxiaomo/epp/controller/Controller.java diff --git a/README.md b/README.md index f83e2de..17c1d3e 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,10 @@ Spring initializr:https://start.spring.io/ https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.7.5&packaging=jar&jvmVersion=11&groupId=com.cxyxiaomo&artifactId=entrance&name=Epidemic%20prevention%20platform&description=&packageName=com.cxyxiaomo.entrance&dependencies=lombok,mysql,mybatis +##### Gateway + +B站教程:https://www.bilibili.com/video/BV1JB4y1F7aL + #### Maven MVN REPOSITORY:https://mvnrepository.com/ diff --git a/backend/.idea/runConfigurations/Gateway.xml b/backend/.idea/runConfigurations/Gateway.xml new file mode 100644 index 0000000..e762fa3 --- /dev/null +++ b/backend/.idea/runConfigurations/Gateway.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/backend/microservice-gateway/pom.xml b/backend/microservice-gateway/pom.xml index 33b6cff..66749a7 100644 --- a/backend/microservice-gateway/pom.xml +++ b/backend/microservice-gateway/pom.xml @@ -27,6 +27,7 @@ org.springframework.cloud spring-cloud-starter-gateway + org.springframework.cloud spring-cloud-starter-loadbalancer diff --git a/backend/microservice-gateway/src/main/java/com/cxyxiaomo/epp/controller/Controller.java b/backend/microservice-gateway/src/main/java/com/cxyxiaomo/epp/controller/Controller.java new file mode 100644 index 0000000..a47e047 --- /dev/null +++ b/backend/microservice-gateway/src/main/java/com/cxyxiaomo/epp/controller/Controller.java @@ -0,0 +1,13 @@ +package com.cxyxiaomo.epp.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + @RequestMapping("/error") + public String error() { + return "[ERROR] 500 Internal Server Error"; + } +} diff --git a/backend/microservice-gateway/src/main/resources/application.yml b/backend/microservice-gateway/src/main/resources/application.yml index b7f4f95..38d4d54 100644 --- a/backend/microservice-gateway/src/main/resources/application.yml +++ b/backend/microservice-gateway/src/main/resources/application.yml @@ -15,7 +15,27 @@ spring: enabled: true lower-case-service-id: true routes: - - id: test - uri: lb://microservice-provider-test-8011 + - id: test1 + uri: lb://microservice-provider-test predicates: - - Path=/test/** + - Path=/test/**,/test1/** + - Method=GET,POST + filters: + - StripPrefix=1 # 跳过第一段 /test + + - id: test2 + uri: lb://microservice-provider-test + predicates: + - Path=/test2/{routes} + filters: + - SetPath=/hi/{routes} + + - id: test3 + uri: lb://microservice-provider-test + predicates: + - Path=/test3/** + filters: + - name: RedirectTo + args: + status: 302 + url: https://www.baidu.com/?wd= diff --git a/backend/microservice-provider-test-8011/src/main/java/com/cxyxiaomo/epp/controller/TestController.java b/backend/microservice-provider-test-8011/src/main/java/com/cxyxiaomo/epp/controller/TestController.java index 69770d6..81a1d7f 100644 --- a/backend/microservice-provider-test-8011/src/main/java/com/cxyxiaomo/epp/controller/TestController.java +++ b/backend/microservice-provider-test-8011/src/main/java/com/cxyxiaomo/epp/controller/TestController.java @@ -3,6 +3,7 @@ package com.cxyxiaomo.epp.controller; // 提供 restful 服务 import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @@ -11,4 +12,14 @@ public class TestController { public String helloWorld() { return "Hello World!"; } + + @GetMapping("/error") + public String error() throws Exception { + throw new Exception("这是一个异常"); + } + + @GetMapping("/hi/{id}") + public String hi(@PathVariable("id") String id) { + return id; + } }