修复路由404问题

1.x
tanghc 5 years ago
parent 9525e2bbba
commit 8a27cccdc5
  1. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java
  2. 37
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java

@ -12,7 +12,9 @@ import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory;
import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration; import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
import com.gitee.sop.gatewaycommon.manager.RouteManager; import com.gitee.sop.gatewaycommon.manager.RouteManager;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.param.ParamBuilder;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
@ -21,6 +23,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -54,6 +57,12 @@ public class BaseGatewayConfiguration extends AbstractConfiguration {
return jsonExceptionHandler; return jsonExceptionHandler;
} }
@Bean
@ConditionalOnMissingBean
ParamBuilder<ServerWebExchange> paramBuilder() {
return ApiConfig.getInstance().getGatewayParamBuilder();
}
/** /**
* 处理返回结果 * 处理返回结果
*/ */

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.gitee.sop.servercommon.bean.ServiceApiInfo; import com.gitee.sop.servercommon.bean.ServiceApiInfo;
import com.gitee.sop.servercommon.bean.ZookeeperTool; import com.gitee.sop.servercommon.bean.ZookeeperTool;
import com.gitee.sop.servercommon.exception.ZookeeperPathNotExistException; import com.gitee.sop.servercommon.exception.ZookeeperPathNotExistException;
import com.gitee.sop.servercommon.route.GatewayPredicateDefinition;
import com.gitee.sop.servercommon.route.GatewayRouteDefinition; import com.gitee.sop.servercommon.route.GatewayRouteDefinition;
import com.gitee.sop.servercommon.route.ServiceRouteInfo; import com.gitee.sop.servercommon.route.ServiceRouteInfo;
import lombok.Getter; import lombok.Getter;
@ -17,6 +18,7 @@ import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -39,6 +41,15 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
private static final String DEFAULT_CONTEXT_PATH = "/"; private static final String DEFAULT_CONTEXT_PATH = "/";
/**
* NameVersion=alipay.story.get1.0
* see com.gitee.sop.gatewaycommon.routeDefinition.NameVersionRoutePredicateFactory
*/
private static String QUERY_PREDICATE_DEFINITION_TPL = "NameVersion=%s";
private static ServiceApiInfo.ApiMeta FIRST_API_META = new ServiceApiInfo.ApiMeta("_first.route_", "/", "v_000");
private Environment environment; private Environment environment;
private ZookeeperTool zookeeperTool; private ZookeeperTool zookeeperTool;
@ -94,6 +105,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
protected ServiceRouteInfo buildServiceGatewayInfo(ServiceApiInfo serviceApiInfo) { protected ServiceRouteInfo buildServiceGatewayInfo(ServiceApiInfo serviceApiInfo) {
List<ServiceApiInfo.ApiMeta> apis = serviceApiInfo.getApis(); List<ServiceApiInfo.ApiMeta> apis = serviceApiInfo.getApis();
List<GatewayRouteDefinition> routeDefinitionList = new ArrayList<>(apis.size()); List<GatewayRouteDefinition> routeDefinitionList = new ArrayList<>(apis.size());
routeDefinitionList.add(this.buildReadBodyRouteDefinition(serviceApiInfo));
for (ServiceApiInfo.ApiMeta apiMeta : apis) { for (ServiceApiInfo.ApiMeta apiMeta : apis) {
GatewayRouteDefinition gatewayRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, apiMeta); GatewayRouteDefinition gatewayRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, apiMeta);
routeDefinitionList.add(gatewayRouteDefinition); routeDefinitionList.add(gatewayRouteDefinition);
@ -136,6 +148,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
BeanUtils.copyProperties(apiMeta, gatewayRouteDefinition); BeanUtils.copyProperties(apiMeta, gatewayRouteDefinition);
gatewayRouteDefinition.setId(routeId); gatewayRouteDefinition.setId(routeId);
gatewayRouteDefinition.setFilters(Collections.emptyList()); gatewayRouteDefinition.setFilters(Collections.emptyList());
gatewayRouteDefinition.setPredicates(this.buildPredicates(apiMeta));
String uri = this.buildUri(serviceApiInfo, apiMeta); String uri = this.buildUri(serviceApiInfo, apiMeta);
String path = this.buildServletPath(serviceApiInfo, apiMeta); String path = this.buildServletPath(serviceApiInfo, apiMeta);
gatewayRouteDefinition.setUri(uri); gatewayRouteDefinition.setUri(uri);
@ -143,6 +156,30 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
return gatewayRouteDefinition; return gatewayRouteDefinition;
} }
protected List<GatewayPredicateDefinition> buildPredicates(ServiceApiInfo.ApiMeta apiMeta) {
GatewayPredicateDefinition gatewayPredicateDefinition = new GatewayPredicateDefinition();
gatewayPredicateDefinition.setName("ReadBody");
return Arrays.asList(gatewayPredicateDefinition, this.buildNameVersionPredicateDefinition(apiMeta));
}
protected GatewayPredicateDefinition buildNameVersionPredicateDefinition(ServiceApiInfo.ApiMeta apiMeta) {
return new GatewayPredicateDefinition(String.format(QUERY_PREDICATE_DEFINITION_TPL, apiMeta.fetchNameVersion()));
}
/**
* 添加com.gitee.sop.gatewaycommon.routeDefinition.ReadBodyRoutePredicateFactory,解决form表单获取不到问题
*
* @return 返回路由定义
*/
protected GatewayRouteDefinition buildReadBodyRouteDefinition(ServiceApiInfo serviceApiInfo) {
GatewayRouteDefinition readBodyRouteDefinition = this.buildGatewayRouteDefinition(serviceApiInfo, FIRST_API_META);
readBodyRouteDefinition.setOrder(Integer.MIN_VALUE);
readBodyRouteDefinition.setPredicates(this.buildPredicates(FIRST_API_META));
return readBodyRouteDefinition;
}
protected String buildUri(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) { protected String buildUri(ServiceApiInfo serviceApiInfo, ServiceApiInfo.ApiMeta apiMeta) {
return PROTOCOL_LOAD_BALANCE + serviceApiInfo.getServiceId(); return PROTOCOL_LOAD_BALANCE + serviceApiInfo.getServiceId();
} }

Loading…
Cancel
Save