diff --git a/pom.xml b/pom.xml index a49f52ae5230c31d27295ec49f34b3b89503b51e..7ea2a294098ae47aac955fb58cfad04988bc04fc 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 0000000000000000000000000000000000000000..f8fff9c96f18a7884ab8730824dc8f656a5c5551 --- /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 0000000000000000000000000000000000000000..187453008513b6099bd6f4db57127dd8054485ad --- /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 0000000000000000000000000000000000000000..b29978b92eb64a55db2b434a8fd41e7412f139cf --- /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 0000000000000000000000000000000000000000..ba22075290414405c939c37b37b51ebe6c599f01 --- /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 0000000000000000000000000000000000000000..1a8e650e3ec9e02f15e843b39496d32bf600724b --- /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 35e64f9ff8476527fcf3c5bbad6a0eb175cf52b7..54d4a1e9dfac71afe953d0b00f483fc30454a096 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 35e64f9ff8476527fcf3c5bbad6a0eb175cf52b7..54d4a1e9dfac71afe953d0b00f483fc30454a096 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 35e64f9ff8476527fcf3c5bbad6a0eb175cf52b7..54d4a1e9dfac71afe953d0b00f483fc30454a096 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 091656bb379681569ac13016e7b81442884da50c..0f05405b7467dd09d819c0cef435ad138c8fcaa2 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 0b62cecdc7ac84c26ad25abca73cf6688945a792..e88e34b6ae8d32b7d963c2a7d5caae9f06ee7425 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 66cc3eefb86b8befe438c492c9222739df152fcc..0000000000000000000000000000000000000000 --- 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 edca4538b46ba407a6eb6ce881174dfb05bc2e34..0000000000000000000000000000000000000000 --- 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 2e0e56865b56db0335d8d44fb97a3e8d3a61a0c8..1da6d169f1f1e9cd6dfa6fb2b5ce60c3745583e8 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