路由修改

1.x
tanghc 6 years ago
parent 48b0ed87a1
commit eeeb0525c6
  1. 4
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/LoadBalancerClientExtFilter.java
  2. 16
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java
  3. 6
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulZookeeperRouteManager.java

@ -1,6 +1,6 @@
package com.gitee.sop.gatewaycommon.gateway.filter; package com.gitee.sop.gatewaycommon.gateway.filter;
import com.gitee.sop.gatewaycommon.util.RoutePathUtil; import com.gitee.sop.gatewaycommon.util.RouteUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
@ -47,7 +47,7 @@ public class LoadBalancerClientExtFilter implements GlobalFilter, Ordered {
protected String findPath(Route route) { protected String findPath(Route route) {
URI routeUri = route.getUri(); URI routeUri = route.getUri();
String uriStr = routeUri.toString(); String uriStr = routeUri.toString();
return RoutePathUtil.findPath(uriStr); return RouteUtil.findPath(uriStr);
} }
} }

@ -1,16 +1,14 @@
package com.gitee.sop.gatewaycommon.util; package com.gitee.sop.gatewaycommon.util;
import org.springframework.cloud.gateway.route.Route;
import java.net.URI;
/** /**
* @author tanghc * @author tanghc
*/ */
public class RoutePathUtil { public class RouteUtil {
public static final String REGEX = "\\#"; public static final String REGEX = "\\#";
public static final String PROTOCOL_LOAD_BALANCE = "lb://";
public static String findPath(String uri) { public static String findPath(String uri) {
// #后面是对应的path // #后面是对应的path
String[] uriArr = uri.split(REGEX); String[] uriArr = uri.split(REGEX);
@ -20,4 +18,12 @@ public class RoutePathUtil {
return null; return null;
} }
} }
public static String getZuulLocation(String uri) {
if (uri.toLowerCase().startsWith(PROTOCOL_LOAD_BALANCE)) {
return uri.substring(PROTOCOL_LOAD_BALANCE.length());
}
return uri;
}
} }

@ -2,7 +2,7 @@ package com.gitee.sop.gatewaycommon.zuul.route;
import com.gitee.sop.gatewaycommon.manager.BaseRouteManager; import com.gitee.sop.gatewaycommon.manager.BaseRouteManager;
import com.gitee.sop.gatewaycommon.manager.RouteRepository; import com.gitee.sop.gatewaycommon.manager.RouteRepository;
import com.gitee.sop.gatewaycommon.util.RoutePathUtil; import com.gitee.sop.gatewaycommon.util.RouteUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.Route; import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -31,7 +31,9 @@ public class ZuulZookeeperRouteManager extends BaseRouteManager<ZuulServiceRoute
@Override @Override
protected ZuulTargetRoute buildRouteDefinition(ZuulServiceRouteInfo serviceRouteInfo, ZuulRouteDefinition routeDefinition) { protected ZuulTargetRoute buildRouteDefinition(ZuulServiceRouteInfo serviceRouteInfo, ZuulRouteDefinition routeDefinition) {
Route route = new Route(routeDefinition.getId(), routeDefinition.getPath(), serviceRouteInfo.getServiceId(), null, false, null); // 路由重试
String retry = environment.getProperty("sop.route.retry", "true");
Route route = new Route(routeDefinition.getId(), routeDefinition.getPath(), RouteUtil.getZuulLocation(routeDefinition.getUri()), null, Boolean.valueOf(retry), null);
return new ZuulTargetRoute(serviceRouteInfo, routeDefinition, route); return new ZuulTargetRoute(serviceRouteInfo, routeDefinition, route);
} }
} }

Loading…
Cancel
Save