From 57d0ccbedefd0ea053bf51b695947b334276196f Mon Sep 17 00:00:00 2001 From: tanghc Date: Fri, 3 Jul 2020 17:32:50 +0800 Subject: [PATCH 1/4] 3.2.1 --- .../result/BaseExecutorAdapter.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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 795e9e25..f3b3f329 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 @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.result; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.DefaultRouteInterceptorContext; import com.gitee.sop.gatewaycommon.bean.Isv; @@ -20,6 +21,7 @@ import org.springframework.http.HttpStatus; import org.springframework.util.CollectionUtils; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; @@ -94,7 +96,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor this.doAfterRoute(serviceResult, responseStatus, request); String finalResult; if (isMergeResult) { - JSONObject responseData = this.parseServiceResult(serviceResult, responseStatus, request); + Map responseData = this.parseServiceResult(serviceResult, responseStatus, request); finalResult = this.merge(request, responseData); } else { finalResult = serviceResult; @@ -141,7 +143,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor * @param request 请求 * @return 返回JSONObject */ - protected JSONObject parseServiceResult(String serviceResult, int responseStatus, T request) { + protected Map parseServiceResult(String serviceResult, int responseStatus, T request) { ErrorEnum errorEnum = HTTP_STATUS_ERROR_ENUM_MAP.get(responseStatus); if (errorEnum == null) { // 其它异常不应该把异常信息告诉给客户端,将微服务内容设置成空的json @@ -149,15 +151,17 @@ public abstract class BaseExecutorAdapter implements ResultExecutor errorEnum = ErrorEnum.ISP_UNKNOWN_ERROR; } ErrorMeta errorMeta = errorEnum.getErrorMeta(); - JSONObject responseData = JSON.parseObject(serviceResult); + Map serviceData = new LinkedHashMap<>(); ApiParam apiParam = this.getApiParam(request); if (apiParam != null) { // 全局请求id,方便追踪定位 - responseData.put("request_id", apiParam.fetchRequestId()); + serviceData.put("request_id", apiParam.fetchRequestId()); } - responseData.put(GATEWAY_CODE_NAME, errorMeta.getCode()); - responseData.put(GATEWAY_MSG_NAME, errorMeta.getError(getLocale(request)).getMsg()); - return responseData; + serviceData.put(GATEWAY_CODE_NAME, errorMeta.getCode()); + serviceData.put(GATEWAY_MSG_NAME, errorMeta.getError(getLocale(request)).getMsg()); + JSONObject serviceObj = JSON.parseObject(serviceResult, Feature.OrderedField); + serviceData.putAll(serviceObj); + return serviceData; } @@ -183,7 +187,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor return serviceResult; } - public String merge(T exchange, JSONObject responseData) { + public String merge(T exchange, Map responseData) { JSONObject finalData = new JSONObject(true); ApiParam params = this.getApiParam(exchange); if (params == null) { From 6df91ddfdab96e1c16a321fb4c5c5f5cf51e64f5 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 6 Jul 2020 13:57:38 +0800 Subject: [PATCH 2/4] 3.2.1 --- .../com/gitee/sop/adminserver/SopAdminServerApplication.java | 2 -- .../src/main/resources/META-INF/sop-admin.properties | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/SopAdminServerApplication.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/SopAdminServerApplication.java index 52bf77ac..e388660d 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/SopAdminServerApplication.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/SopAdminServerApplication.java @@ -2,9 +2,7 @@ package com.gitee.sop.adminserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -@EnableDiscoveryClient @SpringBootApplication public class SopAdminServerApplication { diff --git a/sop-admin/sop-admin-server/src/main/resources/META-INF/sop-admin.properties b/sop-admin/sop-admin-server/src/main/resources/META-INF/sop-admin.properties index a98965a0..f182a180 100644 --- a/sop-admin/sop-admin-server/src/main/resources/META-INF/sop-admin.properties +++ b/sop-admin/sop-admin-server/src/main/resources/META-INF/sop-admin.properties @@ -12,6 +12,8 @@ sop.sign-type=rsa # nacos配置 nacos.config.server-addr=${nacos.url} nacos.discovery.server-addr=${nacos.url} +# nacos cloud配置 +spring.cloud.nacos.discovery.server-addr=${nacos.url} # 数据库配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver From a68ac1a10cd5d67a53f1751f83cb605301b464ca Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 9 Jul 2020 15:36:43 +0800 Subject: [PATCH 3/4] 3.2.1 --- .../configuration/BaseServiceConfiguration.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java index 87c26fde..fcd56bfe 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/BaseServiceConfiguration.java @@ -26,8 +26,8 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl import javax.annotation.PostConstruct; import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; import java.util.List; +import java.util.Map; /** * @author tanghc @@ -39,7 +39,7 @@ public class BaseServiceConfiguration implements WebMvcConfigurer, WebMvcRegistr ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror"); } - private ApiMappingHandlerMapping apiMappingHandlerMapping = new ApiMappingHandlerMapping(); + private final ApiMappingHandlerMapping apiMappingHandlerMapping = new ApiMappingHandlerMapping(); @Autowired private Environment environment; @@ -93,9 +93,15 @@ public class BaseServiceConfiguration implements WebMvcConfigurer, WebMvcRegistr @Bean @ConditionalOnMissingBean @ConditionalOnProperty("spring.cloud.nacos.discovery.server-addr") - public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties, ObjectProvider taskScheduler) { + public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties, ObjectProvider taskScheduler, Environment environment) { + Map metadata = nacosDiscoveryProperties.getMetadata(); + String contextPath = environment.getProperty("server.servlet.context-path"); + // 将context-path信息加入到metadata中 + if (contextPath != null) { + metadata.put("context-path", contextPath); + } // 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题 - nacosDiscoveryProperties.getMetadata().put("time.startup", String.valueOf(System.currentTimeMillis())); + metadata.put("time.startup", String.valueOf(System.currentTimeMillis())); return new NacosWatch(nacosDiscoveryProperties, taskScheduler); } From ee30f18d53354cee52c23580c9ba1d3a586a690d Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 27 Jul 2020 14:45:53 +0800 Subject: [PATCH 4/4] 3.2.1 --- doc/docs/_sidebar.md | 74 +++++++++---------- doc/docs/files/10011_项目接入到SOP.md | 42 ++++++----- .../gateway/route/GatewayRouteRepository.java | 8 +- .../zuul/route/ZuulRouteRepository.java | 4 +- .../main/resources/application-dev.properties | 11 --- 5 files changed, 66 insertions(+), 73 deletions(-) diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index a6b48b7e..d510aa50 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,39 +1,39 @@ -* [首页](/?t=1591927043815) +* [首页](/?t=1595832340981) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1591927043817) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1591927043835) - * [新增接口](files/10020_新增接口.md?t=1591927043836) - * [开发流程](files/10021_开发流程.md?t=1591927043836) - * [业务参数校验](files/10030_业务参数校验.md?t=1591927043836) - * [错误处理](files/10040_错误处理.md?t=1591927043836) - * [编写文档](files/10041_编写文档.md?t=1591927043836) - * [接口交互详解](files/10050_接口交互详解.md?t=1591927043836) - * [easyopen支持](files/10070_easyopen支持.md?t=1591927043836) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1591927043836) - * [ISV管理](files/10085_ISV管理.md?t=1591927043836) - * [自定义返回结果](files/10087_自定义返回结果.md?t=1591927043837) - * [自定义过滤器](files/10088_自定义过滤器.md?t=1591927043837) - * [自定义校验token](files/10089_自定义校验token.md?t=1591927043837) - * [网关拦截器](files/10090_网关拦截器.md?t=1591927043837) - * [路由授权](files/10090_路由授权.md?t=1591927043837) - * [接口限流](files/10092_接口限流.md?t=1591927043837) - * [路由监控](files/10093_路由监控.md?t=1591927043837) - * [SDK开发](files/10095_SDK开发.md?t=1591927043837) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1591927043837) - * [应用授权](files/10097_应用授权.md?t=1591927043837) - * [提供restful接口](files/10100_提供restful接口.md?t=1591927043838) - * [文件上传](files/10104_文件上传.md?t=1591927043838) - * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1591927043838) - * [预发布灰度发布](files/10110_预发布灰度发布.md?t=1591927043838) - * [动态修改请求参数](files/10111_动态修改请求参数.md?t=1591927043838) - * [使用eureka](files/10112_使用eureka.md?t=1591927043838) - * [扩展其它注册中心](files/10113_扩展其它注册中心.md?t=1591927043838) + * [快速体验](files/10010_快速体验.md?t=1595832340983) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1595832341000) + * [新增接口](files/10020_新增接口.md?t=1595832341000) + * [开发流程](files/10021_开发流程.md?t=1595832341001) + * [业务参数校验](files/10030_业务参数校验.md?t=1595832341001) + * [错误处理](files/10040_错误处理.md?t=1595832341001) + * [编写文档](files/10041_编写文档.md?t=1595832341001) + * [接口交互详解](files/10050_接口交互详解.md?t=1595832341001) + * [easyopen支持](files/10070_easyopen支持.md?t=1595832341001) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1595832341001) + * [ISV管理](files/10085_ISV管理.md?t=1595832341001) + * [自定义返回结果](files/10087_自定义返回结果.md?t=1595832341001) + * [自定义过滤器](files/10088_自定义过滤器.md?t=1595832341001) + * [自定义校验token](files/10089_自定义校验token.md?t=1595832341002) + * [网关拦截器](files/10090_网关拦截器.md?t=1595832341002) + * [路由授权](files/10090_路由授权.md?t=1595832341002) + * [接口限流](files/10092_接口限流.md?t=1595832341002) + * [路由监控](files/10093_路由监控.md?t=1595832341002) + * [SDK开发](files/10095_SDK开发.md?t=1595832341002) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1595832341002) + * [应用授权](files/10097_应用授权.md?t=1595832341003) + * [提供restful接口](files/10100_提供restful接口.md?t=1595832341003) + * [文件上传](files/10104_文件上传.md?t=1595832341003) + * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1595832341003) + * [预发布灰度发布](files/10110_预发布灰度发布.md?t=1595832341003) + * [动态修改请求参数](files/10111_动态修改请求参数.md?t=1595832341003) + * [使用eureka](files/10112_使用eureka.md?t=1595832341003) + * [扩展其它注册中心](files/10113_扩展其它注册中心.md?t=1595832341003) * 原理分析 - * [网关性能测试](files/90001_网关性能测试.md?t=1591927043838) - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1591927043838) - * [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1591927043838) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1591927043839) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1591927043839) - * [原理分析之预发布灰度发布](files/90014_原理分析之预发布灰度发布.md?t=1591927043839) - * [2.x升3.x注意事项](files/90099_2.x升3.x注意事项.md?t=1591927043839) - * [常见问题](files/90100_常见问题.md?t=1591927043839) + * [网关性能测试](files/90001_网关性能测试.md?t=1595832341004) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1595832341004) + * [原理分析之如何存储路由](files/90011_原理分析之如何存储路由.md?t=1595832341004) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1595832341004) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1595832341004) + * [原理分析之预发布灰度发布](files/90014_原理分析之预发布灰度发布.md?t=1595832341004) + * [2.x升3.x注意事项](files/90099_2.x升3.x注意事项.md?t=1595832341004) + * [常见问题](files/90100_常见问题.md?t=1595832341004) diff --git a/doc/docs/files/10011_项目接入到SOP.md b/doc/docs/files/10011_项目接入到SOP.md index b0e6af8b..7cadcae9 100644 --- a/doc/docs/files/10011_项目接入到SOP.md +++ b/doc/docs/files/10011_项目接入到SOP.md @@ -6,11 +6,13 @@ ```xml - Greenwich.RELEASE - - 0.9.0.RELEASE - - 1.1.3 + + 2.2.5.RELEASE + + Hoxton.SR3 + + + 2.2.1.RELEASE ``` @@ -19,10 +21,24 @@ ```xml + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + org.springframework.cloud spring-cloud-dependencies - Greenwich.RELEASE + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} pom import @@ -40,28 +56,16 @@ 最新版本 - - org.springframework.cloud + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery - ${spring-cloud-alibaba-nacos.version} - - - com.alibaba.nacos - nacos-client - ${nacos-client.version} - ``` - application.properties配置文件添加 ```properties server.port=2222 -# 服务名称 spring.application.name=story-service # nacos注册中心 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java index 3f19bddb..b1aceee3 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java @@ -32,9 +32,9 @@ import static java.util.Collections.synchronizedMap; @Slf4j public class GatewayRouteRepository implements RouteRepository, RouteLocator { - private PathMatcher pathMatcher = new AntPathMatcher(); + private final PathMatcher pathMatcher = new AntPathMatcher(); - private final Map routes = synchronizedMap(new LinkedHashMap<>()); + private static final Map routes = synchronizedMap(new LinkedHashMap<>()); @Autowired private RouteLocatorBuilder routeLocatorBuilder; @@ -54,7 +54,7 @@ public class GatewayRouteRepository implements RouteRepository routeDefinitionList = this.routes.values() + List routeDefinitionList = routes.values() .stream() .map(AbstractTargetRoute::getRouteDefinition) .collect(Collectors.toList()); @@ -135,7 +135,7 @@ public class GatewayRouteRepository implements RouteRepository idList = this.routes.values().stream() + List idList = routes.values().stream() .filter(zuulTargetRoute -> StringUtils.equalsIgnoreCase(serviceId, zuulTargetRoute.getServiceRouteInfo().getServiceId())) .map(zuulTargetRoute -> zuulTargetRoute.getRouteDefinition().getId()) .collect(Collectors.toList()); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java index e25f5aac..632cdb83 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java @@ -18,12 +18,12 @@ import java.util.stream.Collectors; */ public class ZuulRouteRepository implements RouteRepository { - private PathMatcher pathMatcher = new AntPathMatcher(); + private final PathMatcher pathMatcher = new AntPathMatcher(); /** * key:nameVersion */ - private Map nameVersionTargetRouteMap = new ConcurrentHashMap<>(128); + private static final Map nameVersionTargetRouteMap = new ConcurrentHashMap<>(128); @Override public ZuulTargetRoute get(String id) { diff --git a/sop-example/sop-story/src/main/resources/application-dev.properties b/sop-example/sop-story/src/main/resources/application-dev.properties index a6277955..8957bc3f 100644 --- a/sop-example/sop-story/src/main/resources/application-dev.properties +++ b/sop-example/sop-story/src/main/resources/application-dev.properties @@ -1,15 +1,4 @@ server.port=2222 spring.application.name=story-service -#server.servlet.context-path=/story-service -# 如果有context-path,必须配下面这句 -#spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servlet.context-path} # nacos注册中心 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 - -# zipkin服务跟踪 -#spring.zipkin.base-url=http://127.0.0.1:9411/ -## 设置sleuth收集信息的比率,默认0.1,最大是1,数字越大越耗性能 -#spring.sleuth.sampler.probability=1 -## dubbo使用zipkin过滤器 -#dubbo.provider.filter=tracing -#dubbo.consumer.filter=tracing