From cf53e5faec3b93c99453b0f86ca86d94160fb214 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Fri, 13 Jan 2023 10:08:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0consul?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + springcloud-service-consul/pom.xml | 45 ++++++++++++ .../kwan/springcloud/ConsulApplication.java | 15 ++++ .../controller/ConsulController.java | 25 +++++++ .../src/main/resources/application.yaml | 11 +++ .../springcloud/PortalApplicationTests.java | 13 ++++ .../controller/KwanGoodsInfoController.java | 2 +- .../controller/KwanGoodsInfoController.java | 2 +- .../controller/KwanGoodsInfoController.java | 2 +- .../controller/PortalController.java | 6 ++ springcloud-service-zuul/pom.xml | 3 +- .../GoodsServiceFallBackProvider.java | 72 ------------------- .../kwan/springcloud/filter/MyZuulFilter.java | 49 ------------- .../src/main/resources/application.yaml | 18 ++++- 14 files changed, 136 insertions(+), 128 deletions(-) create mode 100644 springcloud-service-consul/pom.xml create mode 100644 springcloud-service-consul/src/main/java/com/kwan/springcloud/ConsulApplication.java create mode 100644 springcloud-service-consul/src/main/java/com/kwan/springcloud/controller/ConsulController.java create mode 100644 springcloud-service-consul/src/main/resources/application.yaml create mode 100644 springcloud-service-consul/src/test/java/com/kwan/springcloud/PortalApplicationTests.java delete mode 100644 springcloud-service-zuul/src/main/java/com/kwan/springcloud/GoodsServiceFallBackProvider.java delete mode 100644 springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java diff --git a/pom.xml b/pom.xml index a49f52a..7ea2a29 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,7 @@ springcloud-service-config-client springcloud-service-feign springcloud-service-zuul + springcloud-service-consul springcloud-service-eureka diff --git a/springcloud-service-consul/pom.xml b/springcloud-service-consul/pom.xml new file mode 100644 index 0000000..f8fff9c --- /dev/null +++ b/springcloud-service-consul/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.kwan.springcloud + springcloud-service-parent + 1.0.0 + + springcloud-service-consul + 0.0.1-SNAPSHOT + springcloud-service-consul + + 1.8 + + + + com.kwan.springcloud + springcloud-service-common + 1.0.0 + compile + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-starter-consul-discovery + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/springcloud-service-consul/src/main/java/com/kwan/springcloud/ConsulApplication.java b/springcloud-service-consul/src/main/java/com/kwan/springcloud/ConsulApplication.java new file mode 100644 index 0000000..1874530 --- /dev/null +++ b/springcloud-service-consul/src/main/java/com/kwan/springcloud/ConsulApplication.java @@ -0,0 +1,15 @@ +package com.kwan.springcloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class ConsulApplication { + + public static void main(String[] args) { + + SpringApplication.run(ConsulApplication.class, args); + } + +} diff --git a/springcloud-service-consul/src/main/java/com/kwan/springcloud/controller/ConsulController.java b/springcloud-service-consul/src/main/java/com/kwan/springcloud/controller/ConsulController.java new file mode 100644 index 0000000..b29978b --- /dev/null +++ b/springcloud-service-consul/src/main/java/com/kwan/springcloud/controller/ConsulController.java @@ -0,0 +1,25 @@ +package com.kwan.springcloud.controller; + +import com.kwan.springcloud.response.Result; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +/** + * portal控制器 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/1/8 11:12 + */ +@RestController +@RequestMapping("/consul") +public class ConsulController { + + @GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result test() { + return Result.ok("consul 访问成功"); + } +} \ No newline at end of file diff --git a/springcloud-service-consul/src/main/resources/application.yaml b/springcloud-service-consul/src/main/resources/application.yaml new file mode 100644 index 0000000..ba22075 --- /dev/null +++ b/springcloud-service-consul/src/main/resources/application.yaml @@ -0,0 +1,11 @@ +#端口号 +server: + port: 8500 + +spring: + application: + name: consul-service #服务名称 + cloud: + consul: + host: localhost + port: 8500 \ No newline at end of file diff --git a/springcloud-service-consul/src/test/java/com/kwan/springcloud/PortalApplicationTests.java b/springcloud-service-consul/src/test/java/com/kwan/springcloud/PortalApplicationTests.java new file mode 100644 index 0000000..1a8e650 --- /dev/null +++ b/springcloud-service-consul/src/test/java/com/kwan/springcloud/PortalApplicationTests.java @@ -0,0 +1,13 @@ +package com.kwan.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PortalApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springcloud-service-goods-9100/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java b/springcloud-service-goods-9100/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java index 35e64f9..54d4a1e 100644 --- a/springcloud-service-goods-9100/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java +++ b/springcloud-service-goods-9100/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java @@ -13,7 +13,7 @@ import java.io.Serializable; import java.util.List; @RestController -@RequestMapping("kwanGoodsInfo") +@RequestMapping("/kwanGoodsInfo") public class KwanGoodsInfoController { /** * 服务对象 diff --git a/springcloud-service-goods-9200/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java b/springcloud-service-goods-9200/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java index 35e64f9..54d4a1e 100644 --- a/springcloud-service-goods-9200/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java +++ b/springcloud-service-goods-9200/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java @@ -13,7 +13,7 @@ import java.io.Serializable; import java.util.List; @RestController -@RequestMapping("kwanGoodsInfo") +@RequestMapping("/kwanGoodsInfo") public class KwanGoodsInfoController { /** * 服务对象 diff --git a/springcloud-service-goods-9300/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java b/springcloud-service-goods-9300/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java index 35e64f9..54d4a1e 100644 --- a/springcloud-service-goods-9300/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java +++ b/springcloud-service-goods-9300/src/main/java/com/kwan/springcloud/controller/KwanGoodsInfoController.java @@ -13,7 +13,7 @@ import java.io.Serializable; import java.util.List; @RestController -@RequestMapping("kwanGoodsInfo") +@RequestMapping("/kwanGoodsInfo") public class KwanGoodsInfoController { /** * 服务对象 diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java index 091656b..0f05405 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java @@ -34,4 +34,10 @@ public class PortalController { public Result selectOne(@PathVariable String id) { return portalService.addService(id); } + + + @GetMapping(value = "/test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result test() { + return Result.ok("portal 访问成功"); + } } \ No newline at end of file diff --git a/springcloud-service-zuul/pom.xml b/springcloud-service-zuul/pom.xml index 0b62cec..e88e34b 100644 --- a/springcloud-service-zuul/pom.xml +++ b/springcloud-service-zuul/pom.xml @@ -34,9 +34,8 @@ org.springframework.cloud - spring-cloud-starter-netflix-eureka-client + spring-cloud-starter-eureka - diff --git a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/GoodsServiceFallBackProvider.java b/springcloud-service-zuul/src/main/java/com/kwan/springcloud/GoodsServiceFallBackProvider.java deleted file mode 100644 index 66cc3ee..0000000 --- a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/GoodsServiceFallBackProvider.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.kwan.springcloud; - - -import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.stereotype.Component; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * FallbackProvider(请求异常处理) - *

- * 网关的默认回退FallbackProvider的机制只适用于通过配置文件中配置的url来跨服务调用,而不拦截通过FeignClient跨服务调用, - * 也就是说要自己实现一个FeignClient服务的fallback才可以,但是如果通过FeignClient的服务要是特别多怎么办,每一个都要写一个不得累死, - * 这里想到了通过网关的错误过滤器来统一处理FeignClient跨服务调用出错的问题 - * - * @author : qinyingjie - * @version : 2.2.0 - * @date : 2023/1/11 12:48 - */ -@Component -public class GoodsServiceFallBackProvider implements FallbackProvider { - - @Override - public String getRoute() { - return "goods-service"; - } - - @Override - public ClientHttpResponse fallbackResponse(String route, Throwable cause) { - return new ClientHttpResponse() { - - @Override - public HttpHeaders getHeaders() { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); - return headers; - } - - @Override - public InputStream getBody() throws IOException { - return new ByteArrayInputStream("order-service is not available!".getBytes()); - } - - @Override - public HttpStatus getStatusCode() throws IOException { - return HttpStatus.BAD_REQUEST; - } - - @Override - public int getRawStatusCode() throws IOException { - return this.getStatusCode().value(); - } - - @Override - public String getStatusText() throws IOException { - return this.getStatusCode().getReasonPhrase(); - } - - @Override - public void close() { - - } - }; - } - -} diff --git a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java b/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java deleted file mode 100644 index edca453..0000000 --- a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.kwan.springcloud.filter; - -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; -import com.netflix.zuul.exception.ZuulException; -import org.springframework.stereotype.Component; - -import javax.servlet.http.HttpServletRequest; - -@Component -public class MyZuulFilter extends ZuulFilter { - /** - * 表示当前的过滤器是否被调用 - */ - @Override - public boolean shouldFilter() { - return true; - } - - /** - * filterOrder表示执行的优先级,值越小表示优先级越高 - */ - @Override - public int filterOrder() { - return 0; - } - - /** - * pre: 在请求被路由之前调用 - * routing: 在请求被路由之中调用 - * post: 在请求被路由之后调用 - * error: 处理请求发生错误时调用 - */ - @Override - public String filterType() { - return "pre"; - } - - /** - * 真正执行Filter逻辑的方法 - */ - @Override - public Object run() throws ZuulException { - RequestContext ctx = RequestContext.getCurrentContext(); - HttpServletRequest request = ctx.getRequest(); - System.err.println("--------------uri: -------------- " + request.getRequestURI()); - return ctx; - } -} \ No newline at end of file diff --git a/springcloud-service-zuul/src/main/resources/application.yaml b/springcloud-service-zuul/src/main/resources/application.yaml index 2e0e568..1da6d16 100644 --- a/springcloud-service-zuul/src/main/resources/application.yaml +++ b/springcloud-service-zuul/src/main/resources/application.yaml @@ -11,14 +11,28 @@ zuul: sensitiveHeaders: Cookie,Set-Cookie,Authorization routes: api-portal: - path: /portal-service/** #访问路径:http:/localhost:5001/hello-service/hello + path: /portal-service/** #访问路径:http:/localhost:8888/portal-service/portal/1 serviceId: portal-service api-goods: path: /goods-service/** serviceld: goods-service + url: http://localhost:9100/,http://localhost:9200/,http://localhost:9300/ + retryable: true + semaphore: + max-semaphores: 100 + ribbon-isolation-strategy: thread +# prefix: /api + strip-prefix: false #服务提供者 eureka: client: service-url: - defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ + + +ribbon: + ConnectTimeout: 250 #连接超时时间(ms),默认值为250ms + ReadTimeout: 2000 #通信超时时间(ms),默认值为2000ms + MaxAutoRetriesNextServer: 0 #对同一服务不同实例重试次数(同一服务下集群个数的重试次数) + MaxAutoRetries: 2 #对同一实例重试的次数(单个集群节点服务重试的次数) \ No newline at end of file -- GitLab