From cea8fbba7b526ca459edeffcbb121f0aa6da7fdb Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 22 Nov 2019 15:49:48 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=8F=AF=E6=8E=92=E9=99=A4=E5=85=B6?= =?UTF-8?q?=E5=AE=83=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/docs/_sidebar.md | 66 +++++++++---------- doc/docs/files/90100_常见问题.md | 9 +++ .../manager/EnvironmentKeys.java | 8 ++- .../route/BaseRegistryListener.java | 17 ++++- 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index 4a8a6951..d72faede 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,35 +1,35 @@ -* [首页](/?t=1573711393653) +* [首页](/?t=1574408534264) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1573711393655) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1573711393680) - * [新增接口](files/10020_新增接口.md?t=1573711393680) - * [开发流程](files/10021_开发流程.md?t=1573711393680) - * [业务参数校验](files/10030_业务参数校验.md?t=1573711393680) - * [错误处理](files/10040_错误处理.md?t=1573711393680) - * [编写文档](files/10041_编写文档.md?t=1573711393680) - * [接口交互详解](files/10050_接口交互详解.md?t=1573711393680) - * [easyopen支持](files/10070_easyopen支持.md?t=1573711393681) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1573711393681) - * [ISV管理](files/10085_ISV管理.md?t=1573711393681) - * [自定义返回结果](files/10087_自定义返回结果.md?t=1573711393681) - * [自定义过滤器](files/10088_自定义过滤器.md?t=1573711393681) - * [自定义校验token](files/10089_自定义校验token.md?t=1573711393681) - * [路由授权](files/10090_路由授权.md?t=1573711393681) - * [接口限流](files/10092_接口限流.md?t=1573711393682) - * [监控日志](files/10093_监控日志.md?t=1573711393682) - * [SDK开发](files/10095_SDK开发.md?t=1573711393682) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1573711393682) - * [应用授权](files/10097_应用授权.md?t=1573711393682) - * [提供restful接口](files/10100_提供restful接口.md?t=1573711393682) - * [文件上传](files/10104_文件上传.md?t=1573711393682) - * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1573711393683) - * [预发布灰度发布](files/10110_预发布灰度发布.md?t=1573711393683) - * [动态修改请求参数](files/10111_动态修改请求参数.md?t=1573711393683) - * [使用eureka](files/10112_使用eureka.md?t=1573711393683) - * [扩展其它注册中心](files/10113_扩展其它注册中心.md?t=1573711393683) + * [快速体验](files/10010_快速体验.md?t=1574408534265) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1574408534281) + * [新增接口](files/10020_新增接口.md?t=1574408534281) + * [开发流程](files/10021_开发流程.md?t=1574408534281) + * [业务参数校验](files/10030_业务参数校验.md?t=1574408534281) + * [错误处理](files/10040_错误处理.md?t=1574408534281) + * [编写文档](files/10041_编写文档.md?t=1574408534282) + * [接口交互详解](files/10050_接口交互详解.md?t=1574408534282) + * [easyopen支持](files/10070_easyopen支持.md?t=1574408534282) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1574408534282) + * [ISV管理](files/10085_ISV管理.md?t=1574408534282) + * [自定义返回结果](files/10087_自定义返回结果.md?t=1574408534282) + * [自定义过滤器](files/10088_自定义过滤器.md?t=1574408534282) + * [自定义校验token](files/10089_自定义校验token.md?t=1574408534282) + * [路由授权](files/10090_路由授权.md?t=1574408534282) + * [接口限流](files/10092_接口限流.md?t=1574408534282) + * [监控日志](files/10093_监控日志.md?t=1574408534282) + * [SDK开发](files/10095_SDK开发.md?t=1574408534283) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1574408534283) + * [应用授权](files/10097_应用授权.md?t=1574408534283) + * [提供restful接口](files/10100_提供restful接口.md?t=1574408534283) + * [文件上传](files/10104_文件上传.md?t=1574408534283) + * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1574408534283) + * [预发布灰度发布](files/10110_预发布灰度发布.md?t=1574408534283) + * [动态修改请求参数](files/10111_动态修改请求参数.md?t=1574408534283) + * [使用eureka](files/10112_使用eureka.md?t=1574408534283) + * [扩展其它注册中心](files/10113_扩展其它注册中心.md?t=1574408534283) * 原理分析 - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1573711393683) - * [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1573711393683) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1573711393683) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1573711393684) - * [常见问题](files/90100_常见问题.md?t=1573711393684) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1574408534283) + * [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1574408534283) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1574408534283) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1574408534284) + * [常见问题](files/90100_常见问题.md?t=1574408534284) diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md index 0a970c6f..7bcc2b33 100644 --- a/doc/docs/files/90100_常见问题.md +++ b/doc/docs/files/90100_常见问题.md @@ -112,3 +112,12 @@ spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servl ``` 不然网关无法拉取路由信息 + +## 其它微服务没有开放接口,需要排除 + +在sop-gateway项目中配置 + +```properties +# 排除服务,多个用,隔开 +sop.service.exclude=your-serviceId1,your-serviceId2 +``` diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java index 3590c040..7ef06db6 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java @@ -26,7 +26,13 @@ public enum EnvironmentKeys { /** * sop.restful.path=/xx ,指定请求前缀,默认/rest */ - SOP_RESTFUL_PATH("sop.restful.path", "/rest"); + SOP_RESTFUL_PATH("sop.restful.path", "/rest"), + + /** + * 排除其它微服务,多个用英文逗号隔开 + */ + SOP_SERVICE_EXCLUDE("sop.service.exclude") + ; private String key; private String defaultValue; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java index 5fc35061..260f7e3b 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java @@ -1,10 +1,13 @@ package com.gitee.sop.gatewaycommon.route; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; +import com.gitee.sop.gatewaycommon.manager.EnvironmentKeys; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -50,7 +53,8 @@ public abstract class BaseRegistryListener implements RegistryListener { } protected boolean canOperator(String serviceId) { - for (String excludeServiceId : EXCLUDE_SERVICE_ID_LIST) { + List excludeServiceIdList = getExcludeServiceId(); + for (String excludeServiceId : excludeServiceIdList) { if (excludeServiceId.equalsIgnoreCase(serviceId)) { return false; } @@ -65,4 +69,15 @@ public abstract class BaseRegistryListener implements RegistryListener { } return can; } + + private List getExcludeServiceId() { + String excludeServiceIds = EnvironmentKeys.SOP_SERVICE_EXCLUDE.getValue(); + List excludeServiceIdList = new ArrayList<>(8); + if (StringUtils.isNotBlank(excludeServiceIds)) { + String[] serviceIdArr = excludeServiceIds.split(","); + excludeServiceIdList.addAll(Arrays.asList(serviceIdArr)); + } + excludeServiceIdList.addAll(EXCLUDE_SERVICE_ID_LIST); + return excludeServiceIdList; + } } From 82b1ff61c88e960561e0548c73577fb46044bf92 Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 22 Nov 2019 15:51:35 +0800 Subject: [PATCH 2/8] 2.5.4 --- changelog.md | 4 ++++ sop-auth/pom.xml | 2 +- sop-common/pom.xml | 2 +- sop-common/sop-gateway-common/pom.xml | 4 ++-- sop-common/sop-service-common/pom.xml | 4 ++-- sop-example/sop-book/sop-book-web/pom.xml | 2 +- sop-example/sop-easyopen/pom.xml | 2 +- sop-example/sop-springmvc/pom.xml | 2 +- sop-example/sop-story/sop-story-web/pom.xml | 2 +- sop-gateway/pom.xml | 2 +- sop-website/pom.xml | 2 +- 11 files changed, 16 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index 13dd1737..57611f50 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 2.5.4 + +- 可排除其它服务(`sop.service.exclude=your-serviceId1,your-serviceId2`) + ## 2.5.3 - 修复JSR-303校验问题 diff --git a/sop-auth/pom.xml b/sop-auth/pom.xml index 460e66de..927d9033 100644 --- a/sop-auth/pom.xml +++ b/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index fcfa286f..0dd3fcfc 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index b975cb4e..78661016 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT ../pom.xml sop-gateway-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index 4dd0236b..bcacbec4 100644 --- a/sop-common/sop-service-common/pom.xml +++ b/sop-common/sop-service-common/pom.xml @@ -6,11 +6,11 @@ com.gitee.sop sop-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT ../pom.xml sop-service-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT jar sop-service-common diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index 9c09ad48..71c92579 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 8ae007c9..0df4715f 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-service-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 08ce4c4f..2aad25ee 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT com.gitee.sop diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 078d768c..cc672283 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-gateway-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT diff --git a/sop-website/pom.xml b/sop-website/pom.xml index e5ea4a64..f35effa7 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -35,7 +35,7 @@ com.gitee.sop sop-gateway-common - 2.5.3-SNAPSHOT + 2.5.4-SNAPSHOT From 1077f57ff6e6d859ca1dde4bbc357c751e1558fb Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 22 Nov 2019 16:32:22 +0800 Subject: [PATCH 3/8] 2.5.4 --- doc/docs/files/90100_常见问题.md | 8 +++++ sop-common/sop-gateway-common/pom.xml | 7 ++++ .../manager/EnvironmentKeys.java | 6 +++- .../route/BaseRegistryListener.java | 36 ++++++++++++++++--- .../gitee/sop/gatewaycommon/ExcludeTest.java | 22 ++++++++++++ 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md index 7bcc2b33..67eb4f8b 100644 --- a/doc/docs/files/90100_常见问题.md +++ b/doc/docs/files/90100_常见问题.md @@ -121,3 +121,11 @@ spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servl # 排除服务,多个用,隔开 sop.service.exclude=your-serviceId1,your-serviceId2 ``` + +或者使用正则: + +```properties +# 排除以"test-"开头的 +# 多个正则用英文分号(;)隔开 +sop.service.exclude-regex=test\\-.* +``` diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 78661016..19b577f0 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -83,6 +83,13 @@ 2.3.0 true + + + junit + junit + 4.12 + test + diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java index 7ef06db6..f05fa2eb 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java @@ -31,7 +31,11 @@ public enum EnvironmentKeys { /** * 排除其它微服务,多个用英文逗号隔开 */ - SOP_SERVICE_EXCLUDE("sop.service.exclude") + SOP_SERVICE_EXCLUDE("sop.service.exclude"), + /** + * 排除其它微服务,正则形式,多个用英文逗号隔开 + */ + SOP_SERVICE_EXCLUDE_REGEX("sop.service.exclude-regex") ; private String key; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java index 260f7e3b..4167e93b 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java @@ -53,11 +53,9 @@ public abstract class BaseRegistryListener implements RegistryListener { } protected boolean canOperator(String serviceId) { - List excludeServiceIdList = getExcludeServiceId(); - for (String excludeServiceId : excludeServiceIdList) { - if (excludeServiceId.equalsIgnoreCase(serviceId)) { - return false; - } + // 被排除的服务,不能操作 + if (isExcludeService(serviceId)) { + return false; } // nacos会不停的触发事件,这里做了一层拦截 // 同一个serviceId5秒内允许访问一次 @@ -70,6 +68,32 @@ public abstract class BaseRegistryListener implements RegistryListener { return can; } + /** + * 是否是被排除的服务 + * + * @param serviceId 服务id + * @return true,是 + */ + private boolean isExcludeService(String serviceId) { + List excludeServiceIdList = getExcludeServiceId(); + for (String excludeServiceId : excludeServiceIdList) { + if (excludeServiceId.equalsIgnoreCase(serviceId)) { + return true; + } + } + // 匹配正则 + String sopServiceExcludeRegex = EnvironmentKeys.SOP_SERVICE_EXCLUDE_REGEX.getValue(); + if (StringUtils.isNotBlank(sopServiceExcludeRegex)) { + String[] regexArr = sopServiceExcludeRegex.split(";"); + for (String regex : regexArr) { + if (serviceId.matches(regex)) { + return true; + } + } + } + return false; + } + private List getExcludeServiceId() { String excludeServiceIds = EnvironmentKeys.SOP_SERVICE_EXCLUDE.getValue(); List excludeServiceIdList = new ArrayList<>(8); @@ -80,4 +104,6 @@ public abstract class BaseRegistryListener implements RegistryListener { excludeServiceIdList.addAll(EXCLUDE_SERVICE_ID_LIST); return excludeServiceIdList; } + + } diff --git a/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java b/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java new file mode 100644 index 00000000..0d7f7b2d --- /dev/null +++ b/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java @@ -0,0 +1,22 @@ +package com.gitee.sop.gatewaycommon; + +import junit.framework.TestCase; +import org.apache.commons.lang.StringUtils; + +/** + * @author tanghc + */ +public class ExcludeTest extends TestCase { + public void testRegex() { + String serviceId = "com.aaa.bbb.story-service"; + String sopServiceExcludeRegex = "com\\..*;story\\-.*"; + if (StringUtils.isNotBlank(sopServiceExcludeRegex)) { + String[] regexArr = sopServiceExcludeRegex.split(";"); + for (String regex : regexArr) { + if (serviceId.matches(regex)) { + System.out.println("111"); + } + } + } + } +} From 2e853bb77cbe205d0dc3b4a72be2fc7c869e28fe Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 25 Nov 2019 15:52:23 +0800 Subject: [PATCH 4/8] 2.5.4 --- .../com/gitee/sop/websiteserver/config/WebsiteConfig.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java index 335e0efb..388c7fca 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java @@ -3,6 +3,7 @@ package com.gitee.sop.websiteserver.config; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; +import com.gitee.sop.gatewaycommon.manager.EnvironmentContext; import com.gitee.sop.gatewaycommon.route.EurekaRegistryListener; import com.gitee.sop.gatewaycommon.route.NacosRegistryListener; import com.gitee.sop.gatewaycommon.route.RegistryListener; @@ -20,6 +21,7 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; +import org.springframework.core.env.Environment; /** * @author tanghc @@ -33,6 +35,9 @@ public class WebsiteConfig implements ApplicationRunner { @Autowired private RegistryListener registryListener; + @Autowired + private Environment environment; + /** * 使用fastjson代替jackson * @return @@ -84,6 +89,6 @@ public class WebsiteConfig implements ApplicationRunner { */ @Override public void run(ApplicationArguments args) throws Exception { -// docManager.load(null); + EnvironmentContext.setEnvironment(environment); } } From 3cf806cc90575ad8a0583255eb5da41c8159edc2 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 25 Nov 2019 16:11:39 +0800 Subject: [PATCH 5/8] 2.5.4 --- .../com/gitee/sop/websiteserver/config/WebsiteConfig.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java index 388c7fca..0142a941 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/config/WebsiteConfig.java @@ -23,6 +23,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.event.EventListener; import org.springframework.core.env.Environment; +import javax.annotation.PostConstruct; + /** * @author tanghc */ @@ -89,6 +91,11 @@ public class WebsiteConfig implements ApplicationRunner { */ @Override public void run(ApplicationArguments args) throws Exception { + + } + + @PostConstruct + public void after() { EnvironmentContext.setEnvironment(environment); } } From 94d9b0da90428b1ad504f07192fd5733afeb0f9e Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 25 Nov 2019 19:53:32 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitee/sop/gatewaycommon/bean/SopConstants.java | 2 ++ .../gatewaycommon/manager/ServiceRoutesLoader.java | 5 ++--- .../gatewaycommon/route/ServiceRouteListener.java | 5 ++--- .../sop/servercommon/bean/OpenContextImpl.java | 2 ++ .../mapping/ApiMappingHandlerMapping.java | 13 ------------- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java index d4c7856c..54a2fd60 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java @@ -22,6 +22,8 @@ public class SopConstants { public static final String REDIRECT_PATH_KEY = "r-path"; + public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; + /** * 在拦截器中调用获取参数: * String cachedBody = (String)exchange.getAttribute(SopConstants.CACHE_REQUEST_BODY_OBJECT_KEY); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java index c80158fd..1391bfa7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java @@ -9,6 +9,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.gitee.sop.gatewaycommon.bean.NacosConfigs; import com.gitee.sop.gatewaycommon.bean.ServiceRouteInfo; +import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.bean.TargetRoute; import com.gitee.sop.gatewaycommon.route.RegistryListener; import lombok.extern.slf4j.Slf4j; @@ -46,8 +47,6 @@ public class ServiceRoutesLoader { private static final int FIVE_SECONDS = 1000 * 5; - private static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; - private static final String METADATA_SOP_ROUTES_PATH = "sop.routes.path"; @Autowired @@ -159,7 +158,7 @@ public class ServiceRoutesLoader { } else { // 默认处理 homeUrl = getHomeUrl(instance); - String contextPath = metadata.getOrDefault(METADATA_SERVER_CONTEXT_PATH, ""); + String contextPath = metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); servletPath = contextPath + SOP_ROUTES_PATH; } if (StringUtils.isNotBlank(servletPath) && !servletPath.startsWith("/")) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceRouteListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceRouteListener.java index 3a0f81d9..5c7b0a80 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceRouteListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceRouteListener.java @@ -3,6 +3,7 @@ package com.gitee.sop.gatewaycommon.route; import com.alibaba.fastjson.JSON; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; import com.gitee.sop.gatewaycommon.bean.ServiceRouteInfo; +import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.manager.BaseRouteCache; import com.gitee.sop.gatewaycommon.manager.EnvironmentKeys; import lombok.extern.slf4j.Slf4j; @@ -23,8 +24,6 @@ public class ServiceRouteListener extends BaseServiceListener { private static final String SOP_ROUTES_PATH = "/sop/routes"; - private static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; - private static final String METADATA_SOP_ROUTES_PATH = "sop.routes.path"; private static final String HEADER_RESTFUL = "restful"; @@ -89,7 +88,7 @@ public class ServiceRouteListener extends BaseServiceListener { } else { // 默认处理 homeUrl = getHomeUrl(instance); - String contextPath = metadata.getOrDefault(METADATA_SERVER_CONTEXT_PATH, ""); + String contextPath = metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); servletPath = contextPath + SOP_ROUTES_PATH; } if (StringUtils.isNotBlank(servletPath) && !servletPath.startsWith("/")) { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java index 93efe8ea..a71bf45b 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/OpenContextImpl.java @@ -39,6 +39,8 @@ public class OpenContextImpl implements OpenContext { String bizContent = getBizContent(); if (bizContent != null) { bizObject = (T) JSON.parseObject(bizContent, bizClass); + } else { + bizObject = (T) rootJsonObject.toJavaObject(bizClass); } } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java index 506704b6..8506f015 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java @@ -17,19 +17,6 @@ import java.lang.reflect.Method; */ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping implements PriorityOrdered { - private static StringValueResolver stringValueResolver = new ApiMappingStringValueResolver(); - - @Override - protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) { - ApiMapping apiMapping = method.getAnnotation(ApiMapping.class); - StringValueResolver valueResolver = null; - if (apiMapping != null) { - valueResolver = stringValueResolver; - } - this.setEmbeddedValueResolver(valueResolver); - return super.getMappingForMethod(method, handlerType); - } - @Override protected RequestCondition getCustomMethodCondition(Method method) { method.setAccessible(true); From 75d5d9b101265a88226f9491d718702be8cdbf16 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 25 Nov 2019 19:54:19 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BC=98=E5=8C=96Springmvc=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-example/sop-springmvc/pom.xml | 12 ++++++++++ .../java/com/gitee/app/config/Swagger2.java | 21 +++++++++++++++++ .../app/{ => controller}/HomeController.java | 6 ++++- .../spring/appServlet/servlet-context.xml | 5 ++++ .../src/main/webapp/WEB-INF/web.xml | 23 +++++++++++++++++++ .../listener/ServiceDocListener.java | 4 +++- 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 sop-example/sop-springmvc/src/main/java/com/gitee/app/config/Swagger2.java rename sop-example/sop-springmvc/src/main/java/com/gitee/app/{ => controller}/HomeController.java (87%) diff --git a/sop-example/sop-springmvc/pom.xml b/sop-example/sop-springmvc/pom.xml index 363c5058..2e6a2013 100644 --- a/sop-example/sop-springmvc/pom.xml +++ b/sop-example/sop-springmvc/pom.xml @@ -91,6 +91,18 @@ 1.2.17 + + + io.springfox + springfox-swagger2 + 2.9.2 + + + com.github.xiaoymin + swagger-bootstrap-ui + 1.9.5 + + diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/Swagger2.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/Swagger2.java new file mode 100644 index 00000000..d09fd292 --- /dev/null +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/Swagger2.java @@ -0,0 +1,21 @@ +package com.gitee.app.config; + +import com.gitee.sop.servercommon.swagger.SwaggerSupport; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * 开启文档,本地微服务文档地址:http://localhost:2222/doc.html + * http://ip:port/v2/api-docs + */ +@EnableSwagger2 +public class Swagger2 extends SwaggerSupport { + @Override + protected String getDocTitle() { + return "MVC_API"; + } + + @Override + protected boolean swaggerAccessProtected() { + return false; + } +} \ No newline at end of file diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/controller/HomeController.java similarity index 87% rename from sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java rename to sop-example/sop-springmvc/src/main/java/com/gitee/app/controller/HomeController.java index 04a5d638..7028c51b 100644 --- a/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/controller/HomeController.java @@ -1,7 +1,9 @@ -package com.gitee.app; +package com.gitee.app.controller; import com.gitee.app.model.Goods; import com.gitee.sop.servercommon.annotation.ApiMapping; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; @@ -18,6 +20,7 @@ import java.util.Locale; /** * Handles requests for the application home page. */ +@Api(tags = "MVC接口") @Controller public class HomeController { @@ -41,6 +44,7 @@ public class HomeController { } + @ApiOperation(value="获取商品", notes = "获取商品说明") @ApiMapping(value = "springmvc.goods.get") @ResponseBody public Goods getGoods(Goods param) { diff --git a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml index 2956e9bb..e27b63be 100644 --- a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml +++ b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -34,6 +34,11 @@ + + + + + diff --git a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml index b7dfa271..630564eb 100644 --- a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml +++ b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml @@ -29,6 +29,29 @@ 1 + + + + appServlet + /v2/api-docs + + + appServlet + /swagger-resources + + + appServlet + /swagger-resources/configuration/ui + + + appServlet + /swagger-resources/configuration/security + + + + appServlet + /v2/api-docs-ext + appServlet diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/listener/ServiceDocListener.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/listener/ServiceDocListener.java index dd3eed10..9583c7c7 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/listener/ServiceDocListener.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/listener/ServiceDocListener.java @@ -1,6 +1,7 @@ package com.gitee.sop.websiteserver.listener; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; +import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.route.BaseServiceListener; import com.gitee.sop.websiteserver.manager.DocManager; import lombok.extern.slf4j.Slf4j; @@ -45,6 +46,7 @@ public class ServiceDocListener extends BaseServiceListener { private static String getRouteRequestUrl(InstanceDefinition instance) { String query = buildQuery(SECRET); - return "http://" + instance.getIp() + ":" + instance.getPort() + "/v2/api-docs" + query; + String contextPath = instance.getMetadata().getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); + return "http://" + instance.getIp() + ":" + instance.getPort() + contextPath + "/v2/api-docs" + query; } } From 7a3960117a592d888d7d73e976f6ea1a041d3fe9 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 25 Nov 2019 19:55:35 +0800 Subject: [PATCH 8/8] 2.5.5 --- changelog.md | 4 ++++ sop-auth/pom.xml | 2 +- sop-common/pom.xml | 2 +- sop-common/sop-gateway-common/pom.xml | 4 ++-- sop-common/sop-service-common/pom.xml | 4 ++-- sop-example/sop-book/sop-book-web/pom.xml | 2 +- sop-example/sop-easyopen/pom.xml | 2 +- sop-example/sop-springmvc/pom.xml | 2 +- sop-example/sop-story/sop-story-web/pom.xml | 2 +- sop-gateway/pom.xml | 2 +- sop-website/pom.xml | 2 +- 11 files changed, 16 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index 57611f50..edb75ef8 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 2.5.5 + +- 优化文档显示 + ## 2.5.4 - 可排除其它服务(`sop.service.exclude=your-serviceId1,your-serviceId2`) diff --git a/sop-auth/pom.xml b/sop-auth/pom.xml index 927d9033..66b23fbd 100644 --- a/sop-auth/pom.xml +++ b/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 0dd3fcfc..3e8e634c 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.sop sop-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 19b577f0..c8f6c86a 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,11 +5,11 @@ com.gitee.sop sop-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT ../pom.xml sop-gateway-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT jar sop-gateway-common diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index bcacbec4..747d9757 100644 --- a/sop-common/sop-service-common/pom.xml +++ b/sop-common/sop-service-common/pom.xml @@ -6,11 +6,11 @@ com.gitee.sop sop-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT ../pom.xml sop-service-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT jar sop-service-common diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index 71c92579..0a91c573 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 0df4715f..71ffdf9c 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-service-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 2aad25ee..0f067edd 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -28,7 +28,7 @@ com.gitee.sop sop-service-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT com.gitee.sop diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index cc672283..2ce59323 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-gateway-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT diff --git a/sop-website/pom.xml b/sop-website/pom.xml index f35effa7..c496a577 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -35,7 +35,7 @@ com.gitee.sop sop-gateway-common - 2.5.4-SNAPSHOT + 2.5.5-SNAPSHOT