diff --git a/changelog.md b/changelog.md index 49e32814..2d94d5e8 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # changelog +## 2.5.9 + +- 优化获取context-path +- 修复结果返回产生的NPE问题 + ## 2.5.8 - 优化参数绑定 diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md index 9399505a..65b54b8f 100644 --- a/doc/docs/files/90100_常见问题.md +++ b/doc/docs/files/90100_常见问题.md @@ -108,7 +108,18 @@ ribbon.ReadTimeout: 60000 微服务项目定义了`server.servlet.context-path=/story-service`,同时必须指定: ```properties -spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servlet.context-path} +spring.cloud.nacos.discovery.metadata.context-path=${server.servlet.context-path} +``` + +yml配置如下: + +```yaml +spring: + cloud: + nacos: + discovery: + metadata: + context-path: ${server.servlet.context-path} ``` 不然网关无法拉取路由信息 diff --git a/sop-auth/pom.xml b/sop-auth/pom.xml index 9097898a..4245a5ec 100644 --- a/sop-auth/pom.xml +++ b/sop-auth/pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 2.5.8-SNAPSHOT + 2.5.9-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index dc84cd09..a12af096 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.8-SNAPSHOT + 2.5.9-SNAPSHOT pom diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index e5de7f61..a900714e 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.8-SNAPSHOT + 2.5.9-SNAPSHOT ../pom.xml sop-gateway-common - 2.5.8-SNAPSHOT + 2.5.9-SNAPSHOT jar sop-gateway-common 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 54a2fd60..fa09436f 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 @@ -24,6 +24,8 @@ public class SopConstants { public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; + public static final String METADATA_SERVER_CONTEXT_PATH_COMPATIBILITY = "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 1391bfa7..e825925f 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 @@ -11,6 +11,7 @@ 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.RegistryMetadata; import com.gitee.sop.gatewaycommon.route.RegistryListener; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -39,7 +40,7 @@ import java.util.Objects; */ @Deprecated @Slf4j -public class ServiceRoutesLoader { +public class ServiceRoutesLoader implements RegistryMetadata { private static final String SOP_ROUTES_PATH = "/sop/routes"; @@ -140,7 +141,7 @@ public class ServiceRoutesLoader { * @param instance 服务实例 * @return 返回最终url */ - private static String getRouteRequestUrl(Instance instance) { + private String getRouteRequestUrl(Instance instance) { Map metadata = instance.getMetadata(); String customPath = metadata.get(METADATA_SOP_ROUTES_PATH); String homeUrl; @@ -158,7 +159,7 @@ public class ServiceRoutesLoader { } else { // 默认处理 homeUrl = getHomeUrl(instance); - String contextPath = metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); + String contextPath = this.getContextPath(metadata); 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/result/BaseExecutorAdapter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java index ac25fb66..c986bd2f 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java @@ -261,7 +261,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor return null; } Isv isvInfo = isvManager.getIsv(appKey); - String privateKeyPlatform = isvInfo.getPrivateKeyPlatform(); + String privateKeyPlatform = isvInfo == null ? null : isvInfo.getPrivateKeyPlatform(); if (StringUtils.isEmpty(privateKeyPlatform)) { return null; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseServiceListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseServiceListener.java index cb1f5339..0ec8e5ac 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseServiceListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseServiceListener.java @@ -8,7 +8,7 @@ import org.springframework.web.client.RestTemplate; /** * @author tanghc */ -public abstract class BaseServiceListener implements ServiceListener { +public abstract class BaseServiceListener implements ServiceListener, RegistryMetadata { private static final String SECRET = "a3d9sf!1@odl90zd>fkASwq"; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/RegistryMetadata.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/RegistryMetadata.java new file mode 100644 index 00000000..82e5f58a --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/RegistryMetadata.java @@ -0,0 +1,17 @@ +package com.gitee.sop.gatewaycommon.route; + +import com.gitee.sop.gatewaycommon.bean.SopConstants; + +import java.util.Map; + +/** + * @author tanghc + */ +public interface RegistryMetadata { + + default String getContextPath(Map metadata) { + return metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH + , metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH_COMPATIBILITY, "")); + } + +} 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 5c7b0a80..c4db5311 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 @@ -70,7 +70,7 @@ public class ServiceRouteListener extends BaseServiceListener { * @param instance 服务实例 * @return 返回最终url */ - private static String getRouteRequestUrl(InstanceDefinition instance) { + private String getRouteRequestUrl(InstanceDefinition instance) { Map metadata = instance.getMetadata(); String customPath = metadata.get(METADATA_SOP_ROUTES_PATH); String homeUrl; @@ -88,7 +88,7 @@ public class ServiceRouteListener extends BaseServiceListener { } else { // 默认处理 homeUrl = getHomeUrl(instance); - String contextPath = metadata.getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); + String contextPath = this.getContextPath(metadata); servletPath = contextPath + SOP_ROUTES_PATH; } if (StringUtils.isNotBlank(servletPath) && !servletPath.startsWith("/")) { diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index 372dfb50..17760a18 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.8-SNAPSHOT + 2.5.9-SNAPSHOT ../pom.xml sop-service-common - 2.5.8-SNAPSHOT + 2.5.9-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 ebe29901..da06e4f9 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.8-SNAPSHOT + 2.5.9-SNAPSHOT com.gitee.sop diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 0b169726..c1515e53 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.8-SNAPSHOT + 2.5.9-SNAPSHOT diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index f3e19aad..ce015116 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.8-SNAPSHOT + 2.5.9-SNAPSHOT com.gitee.sop diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index a5638d88..78546036 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -29,7 +29,7 @@ com.gitee.sop sop-gateway-common - 2.5.8-SNAPSHOT + 2.5.9-SNAPSHOT diff --git a/sop-website/pom.xml b/sop-website/pom.xml index b1e519ad..12b7b82d 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -35,7 +35,7 @@ com.gitee.sop sop-gateway-common - 2.5.8-SNAPSHOT + 2.5.9-SNAPSHOT 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 9583c7c7..0f270a78 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 @@ -44,9 +44,9 @@ public class ServiceDocListener extends BaseServiceListener { } } - private static String getRouteRequestUrl(InstanceDefinition instance) { + private String getRouteRequestUrl(InstanceDefinition instance) { String query = buildQuery(SECRET); - String contextPath = instance.getMetadata().getOrDefault(SopConstants.METADATA_SERVER_CONTEXT_PATH, ""); + String contextPath = this.getContextPath(instance.getMetadata()); return "http://" + instance.getIp() + ":" + instance.getPort() + contextPath + "/v2/api-docs" + query; } }