diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml index 6fcdfb39..838b39bc 100644 --- a/sop-common/sop-gateway-common/pom.xml +++ b/sop-common/sop-gateway-common/pom.xml @@ -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..84440aac --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/RestfulController.java @@ -0,0 +1,50 @@ +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(); + } + + private String getServiceId(String path) { + // /rest/story-service/food/getFoodById + 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-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 5ce2d493..b54bc292 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,9 @@ server.port=2222 spring.application.name=story-service register.url=127.0.0.1:8848 -# 注册中心 +# \u6CE8\u518C\u4E2D\u5FC3 spring.cloud.nacos.discovery.server-addr=${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