diff --git a/changelog.md b/changelog.md index 9965dbac..e44fb401 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # changelog +## 4.2.5 + +- 修复restful负载均衡问题 + ## 4.2.4 - 修复sop-auth模块启动失败问题 diff --git a/doc/docs/files/10020_新增接口.md b/doc/docs/files/10020_新增接口.md index 692608f5..bd31cc62 100644 --- a/doc/docs/files/10020_新增接口.md +++ b/doc/docs/files/10020_新增接口.md @@ -44,6 +44,8 @@ public StoryResult getStory() { 其中biz_content部分是我们想要的,在方法上申明一个对象,对应biz_content中的内容即可完成参数绑定,并且对参数进行JSR-303校验。 +**注意:接口方法必须有且只有一个对象参数,如果申明多个会出现参数绑定失败** + ```java @Open("goods.add") @RequestMapping("/goods/add") diff --git a/doc/docs/files/10087_自定义返回结果.md b/doc/docs/files/10087_自定义返回结果.md index 8269c67e..e5609bea 100644 --- a/doc/docs/files/10087_自定义返回结果.md +++ b/doc/docs/files/10087_自定义返回结果.md @@ -44,7 +44,7 @@ return method.replace('.', '_') + "_response"; ```java @Configuration -public class ZuulConfig extends AlipayZuulConfiguration { +public class MyConfig { static { ... @@ -84,23 +84,18 @@ public class ZuulConfig extends AlipayZuulConfiguration { 如果想要对微服务结果做更深一步处理,步骤如下: -1. 新增一个类,继承`ZuulResultExecutor.java`,并重写`public String merge(T exchange, JSONObject responseData)`方法 - -方法merge参数说明如下: - - exchange:RequestContext对象 - responseData:微服务端返回的结果 - -方法返回最终结果 +1. 新增一个类,继承`GatewayResultExecutor.java`,并重写`String mergeResult(T request, String serviceResult)`方法 2. 配置自定义类 ```java -public class SopGatewayApplication { - - public static void main(String[] args) { - ApiConfig.getInstance().setZuulResultExecutor(new MyzuulResultExecutor()); - SpringApplication.run(SopGatewayApplication.class, args); +@Configuration +public class MyConfig { + + static { + ... + ApiConfig.getInstance().setGatewayResultExecutor(new MyGatewayResultExecutor()); + ... } } diff --git a/doc/docs/files/10110_预发布灰度发布.md b/doc/docs/files/10110_预发布灰度发布.md index 6b469196..4d5a0cb3 100644 --- a/doc/docs/files/10110_预发布灰度发布.md +++ b/doc/docs/files/10110_预发布灰度发布.md @@ -40,6 +40,14 @@ pre.domain=openpre.domain.com ## 使用灰度发布 +- 灰度接口定义 + +接口名相同,版本号不同。比如接口`user.get`,version:1.0 + +新建一个接口`user.get`,version:2.0 + +那么这个2.0接口就是灰度接口 + 灰度发布可允许指定的用户访问灰度服务器,其它用户还是走正常流程。 登录SOP-Admin,前往`服务列表`。 diff --git a/doc/pom.xml b/doc/pom.xml index 16d2abba..3f273928 100644 --- a/doc/pom.xml +++ b/doc/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.gitee.sop doc - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/pom.xml b/pom.xml index f554358b..c507b1ff 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ 4.0.0 com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT pom 一个开放平台解决方案项目,基于Spring Cloud实现,目标是能够让用户快速得搭建起自己的开放平台 diff --git a/sop-admin/pom.xml b/sop-admin/pom.xml index 601957cf..390c6d7d 100644 --- a/sop-admin/pom.xml +++ b/sop-admin/pom.xml @@ -6,13 +6,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml 4.0.0 sop-admin - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT pom diff --git a/sop-admin/sop-admin-server/pom.xml b/sop-admin/sop-admin-server/pom.xml index 8f569086..575a8a77 100644 --- a/sop-admin/sop-admin-server/pom.xml +++ b/sop-admin/sop-admin-server/pom.xml @@ -5,13 +5,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 sop-admin-server - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT jar diff --git a/sop-auth/pom.xml b/sop-auth/pom.xml index 35bc30e6..4203c7da 100644 --- a/sop-auth/pom.xml +++ b/sop-auth/pom.xml @@ -5,7 +5,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml @@ -25,7 +25,7 @@ com.gitee.sop sop-service-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT @@ -37,7 +37,7 @@ com.gitee.sop sdk-java - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-common/pom.xml b/sop-common/pom.xml index 220a4744..185b01de 100644 --- a/sop-common/pom.xml +++ b/sop-common/pom.xml @@ -6,7 +6,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml diff --git a/sop-common/sop-bridge-eureka/pom.xml b/sop-common/sop-bridge-eureka/pom.xml index 80e8fe15..1378df0d 100644 --- a/sop-common/sop-bridge-eureka/pom.xml +++ b/sop-common/sop-bridge-eureka/pom.xml @@ -5,12 +5,12 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT sop-bridge-eureka @@ -18,7 +18,7 @@ com.gitee.sop sop-gateway-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-common/sop-bridge-nacos/pom.xml b/sop-common/sop-bridge-nacos/pom.xml index 66670910..60a5e3d9 100644 --- a/sop-common/sop-bridge-nacos/pom.xml +++ b/sop-common/sop-bridge-nacos/pom.xml @@ -5,12 +5,12 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT sop-bridge-nacos @@ -18,7 +18,7 @@ com.gitee.sop sop-gateway-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 6fcdfb39..005dfab8 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -5,13 +5,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 sop-gateway-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT jar @@ -54,6 +54,11 @@ fastjson + + org.springframework.cloud + spring-cloud-gateway-webflux + + com.alibaba.cloud diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java index 4607b73b..93659911 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java @@ -28,12 +28,12 @@ import com.gitee.sop.gatewaycommon.result.ResultExecutorForGateway; import com.gitee.sop.gatewaycommon.secret.CacheIsvManager; import com.gitee.sop.gatewaycommon.secret.IsvManager; import com.gitee.sop.gatewaycommon.validate.ApiEncrypter; -import com.gitee.sop.gatewaycommon.validate.ApiSigner; import com.gitee.sop.gatewaycommon.validate.ApiValidator; import com.gitee.sop.gatewaycommon.validate.Encrypter; import com.gitee.sop.gatewaycommon.validate.Signer; import com.gitee.sop.gatewaycommon.validate.TokenValidator; import com.gitee.sop.gatewaycommon.validate.Validator; +import com.gitee.sop.gatewaycommon.validate.alipay.AlipaySigner; import lombok.Data; import org.apache.commons.lang3.StringUtils; @@ -74,7 +74,7 @@ public class ApiConfig { /** * 签名工具 */ - private Signer signer = new ApiSigner(); + private Signer signer = new AlipaySigner(); /** * 验证 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/AlipayGatewayConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/AlipayGatewayConfiguration.java index b84b6670..33ceeef3 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/AlipayGatewayConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/AlipayGatewayConfiguration.java @@ -1,8 +1,5 @@ package com.gitee.sop.gatewaycommon.gateway.configuration; -import com.gitee.sop.gatewaycommon.bean.ApiContext; -import com.gitee.sop.gatewaycommon.validate.alipay.AlipaySigner; - /** * 具备支付宝开放平台能力配置 https://docs.open.alipay.com/api * @@ -10,7 +7,4 @@ import com.gitee.sop.gatewaycommon.validate.alipay.AlipaySigner; */ public class AlipayGatewayConfiguration extends BaseGatewayConfiguration { - static { - ApiContext.getApiConfig().setSigner(new AlipaySigner()); - } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/RestfulController.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/RestfulController.java new file mode 100644 index 00000000..dcad45ad --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/RestfulController.java @@ -0,0 +1,54 @@ +package com.gitee.sop.gatewaycommon.gateway.controller; + +import com.gitee.sop.gatewaycommon.bean.SpringContext; +import com.gitee.sop.gatewaycommon.gateway.loadbalancer.SopLoadBalancerClient; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.gateway.webflux.ProxyExchange; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * 处理restful请求 + * @author tanghc + */ +@Controller +public class RestfulController { + + private static final int PREFIX_LEN = "/rest/".length(); + + @RequestMapping("/rest/**") + public Mono> proxy(ProxyExchange proxy, ServerWebExchange exchange) { + String path = proxy.path(); + String serviceId = getServiceId(path); + String targetPath = getTargetPath(serviceId, path); + String rawQuery = exchange.getRequest().getURI().getRawQuery(); + if (StringUtils.hasLength(rawQuery)) { + targetPath = targetPath + "?" + rawQuery; + } + // 负载均衡 + ServiceInstance serviceInstance = SpringContext.getBean(SopLoadBalancerClient.class).choose(serviceId, exchange); + String uri = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + targetPath; + return proxy.uri(uri).forward(); + } + + /** + * 从path中解析出serviceId + * @param path 格式:/rest/ + * @return 返回serviceId + */ + private String getServiceId(String path) { + path = path.substring(PREFIX_LEN); + int index = path.indexOf('/'); + path = path.substring(0, index); + return path; + } + + private String getTargetPath(String serviceId, String path) { + int len = PREFIX_LEN + serviceId.length(); + return path.substring(len); + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java index 236752cc..583966a7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java @@ -72,11 +72,9 @@ public class IndexFilter implements WebFilter { return chain.filter(exchange); } // 如果是restful请求,直接转发 + // see:com.gitee.sop.gatewaycommon.gateway.controller.RestfulController if (enableRestful && path.startsWith(EnvironmentKeys.SOP_RESTFUL_PATH.getValue())) { - exchange.getAttributes().put(SopConstants.RESTFUL_REQUEST, true); - String restfulPath = ServerWebExchangeUtil.getRestfulPath(path, EnvironmentKeys.SOP_RESTFUL_PATH.getValue()); - ServerWebExchange newExchange = ServerWebExchangeUtil.getForwardExchange(exchange, restfulPath); - return chain.filter(newExchange); + return chain.filter(exchange); } if (Objects.equals(path, indexPath)) { if (request.getMethod() == HttpMethod.POST) { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/interceptor/RouteInterceptorContext.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/interceptor/RouteInterceptorContext.java index 6e1d9dcf..fbb3d307 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/interceptor/RouteInterceptorContext.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/interceptor/RouteInterceptorContext.java @@ -1,6 +1,5 @@ package com.gitee.sop.gatewaycommon.interceptor; -import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.param.ApiParam; import org.springframework.cloud.client.ServiceInstance; import org.springframework.http.HttpStatus; diff --git a/sop-common/sop-service-common/pom.xml b/sop-common/sop-service-common/pom.xml index 32701027..60f1dc07 100644 --- a/sop-common/sop-service-common/pom.xml +++ b/sop-common/sop-service-common/pom.xml @@ -6,13 +6,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 sop-service-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT jar diff --git a/sop-example/pom.xml b/sop-example/pom.xml index d4844a40..0c11d667 100644 --- a/sop-example/pom.xml +++ b/sop-example/pom.xml @@ -5,7 +5,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml diff --git a/sop-example/sop-springmvc/pom.xml b/sop-example/sop-springmvc/pom.xml index 8470b409..5e07ef3f 100644 --- a/sop-example/sop-springmvc/pom.xml +++ b/sop-example/sop-springmvc/pom.xml @@ -5,7 +5,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml @@ -26,7 +26,7 @@ com.gitee.sop sop-service-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-example/sop-story/pom.xml b/sop-example/sop-story/pom.xml index 87cefac1..11a21a5c 100644 --- a/sop-example/sop-story/pom.xml +++ b/sop-example/sop-story/pom.xml @@ -4,7 +4,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml @@ -19,7 +19,7 @@ com.gitee.sop sop-service-common - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1008_RestfulController.java b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1008_RestfulController.java index 36de18b0..2c7af9e7 100644 --- a/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1008_RestfulController.java +++ b/sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1008_RestfulController.java @@ -4,6 +4,8 @@ import com.gitee.sop.servercommon.util.UploadUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -25,6 +27,8 @@ import java.util.Collection; @Api(tags = "食物接口") public class Example1008_RestfulController { + @Autowired + private Environment environment; // http://localhost:8081/rest/story-service/food/getFoodById?id=1 网关入口 // http://localhost:2222/food/getFoodById/?id=12 本地入口 @@ -33,7 +37,7 @@ public class Example1008_RestfulController { public Food getFoodById(Integer id) { Food food = new Food(); food.setId(id); - food.setName("香蕉"); + food.setName("香蕉, " + environment.getProperty("server.port")); food.setPrice(new BigDecimal(20.00)); return food; } 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 ff5fc397..9af54438 100644 --- a/sop-example/sop-story/src/main/resources/application-dev.properties +++ b/sop-example/sop-story/src/main/resources/application-dev.properties @@ -1,6 +1,10 @@ server.port=2222 spring.application.name=story-service register.url=http://localhost:1111/eureka/ -# 注册中心 +# \u6CE8\u518C\u4E2D\u5FC3 eureka.client.serviceUrl.defaultZone=${register.url} + +# \u4E0A\u4F20\u6587\u4EF6\u6700\u5927\u503C +spring.servlet.multipart.max-file-size=20MB +spring.servlet.multipart.max-request-size=20MB \ No newline at end of file diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 93c97b2a..c5f8289a 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -4,7 +4,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml @@ -22,9 +22,8 @@ --> com.gitee.sop - sop-bridge-eureka - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-sdk/pom.xml b/sop-sdk/pom.xml index d3d9e571..fa2f495c 100644 --- a/sop-sdk/pom.xml +++ b/sop-sdk/pom.xml @@ -6,13 +6,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml 4.0.0 sop-sdk - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT pom diff --git a/sop-sdk/sdk-java/pom.xml b/sop-sdk/sdk-java/pom.xml index 0e80fd9a..4e3d28ad 100644 --- a/sop-sdk/sdk-java/pom.xml +++ b/sop-sdk/sdk-java/pom.xml @@ -4,13 +4,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../../pom.xml 4.0.0 sdk-java - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT diff --git a/sop-test/pom.xml b/sop-test/pom.xml index 35877fc3..9c11677a 100644 --- a/sop-test/pom.xml +++ b/sop-test/pom.xml @@ -5,7 +5,7 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml diff --git a/sop-website/pom.xml b/sop-website/pom.xml index a865561b..beb7cb26 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -6,13 +6,13 @@ com.gitee.sop sop-parent - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml 4.0.0 sop-website - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT pom diff --git a/sop-website/sop-website-server/pom.xml b/sop-website/sop-website-server/pom.xml index e8d1f34b..4eb19fec 100644 --- a/sop-website/sop-website-server/pom.xml +++ b/sop-website/sop-website-server/pom.xml @@ -5,7 +5,7 @@ com.gitee.sop sop-website - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT ../pom.xml @@ -23,9 +23,8 @@ --> com.gitee.sop - sop-bridge-eureka - 4.2.4-SNAPSHOT + 4.2.5-SNAPSHOT