修复restful负载均衡问题

eureka
tanghc 4 years ago
parent 8d071e2ac3
commit d642a4f9c6
  1. 5
      sop-common/sop-gateway-common/pom.xml
  2. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java
  3. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/AlipayGatewayConfiguration.java
  4. 50
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/controller/RestfulController.java
  5. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/IndexFilter.java
  6. 6
      sop-example/sop-story/src/main/java/com/gitee/sop/storyweb/controller/Example1008_RestfulController.java
  7. 5
      sop-example/sop-story/src/main/resources/application-dev.properties

@ -54,6 +54,11 @@
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-webflux</artifactId>
</dependency>
<!-- optional -->
<dependency>
<groupId>com.alibaba.cloud</groupId>

@ -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();
/**
* 验证

@ -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());
}
}

@ -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<ResponseEntity<byte[]>> proxy(ProxyExchange<byte[]> 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);
}
}

@ -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) {

@ -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;
}

@ -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
Loading…
Cancel
Save