Commit acbf93e3 by Nepxion

增加Zone路由功能

parent 872bc230
......@@ -21,6 +21,7 @@ public class DiscoveryConstant {
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String GROUP = "group";
public static final String ZONE = "zone";
public static final String SERVICE_ID = "serviceId";
public static final String HOST = "host";
public static final String PORT = "port";
......
......@@ -15,9 +15,16 @@ import com.nepxion.discovery.plugin.strategy.extension.gateway.context.GatewaySt
public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdapter {
@Override
protected String getVersionJson() {
protected String getVersionValue() {
GatewayStrategyContext context = GatewayStrategyContext.getCurrentContext();
return context.getExchange().getRequest().getHeaders().getFirst(DiscoveryConstant.VERSION);
}
@Override
protected String getZoneValue() {
GatewayStrategyContext context = GatewayStrategyContext.getCurrentContext();
return context.getExchange().getRequest().getHeaders().getFirst(DiscoveryConstant.ZONE);
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ import com.nepxion.discovery.plugin.strategy.adapter.AbstractDiscoveryEnabledAda
public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdapter {
@Override
protected String getVersionJson() {
protected String getVersionValue() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return null;
......@@ -25,4 +25,14 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return attributes.getRequest().getHeader(DiscoveryConstant.VERSION);
}
@Override
protected String getZoneValue() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return null;
}
return attributes.getRequest().getHeader(DiscoveryConstant.ZONE);
}
}
\ No newline at end of file
......@@ -15,9 +15,16 @@ import com.netflix.zuul.context.RequestContext;
public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdapter {
@Override
protected String getVersionJson() {
protected String getVersionValue() {
RequestContext context = RequestContext.getCurrentContext();
return context.getRequest().getHeader(DiscoveryConstant.VERSION);
}
@Override
protected String getZoneValue() {
RequestContext context = RequestContext.getCurrentContext();
return context.getRequest().getHeader(DiscoveryConstant.ZONE);
}
}
\ No newline at end of file
......@@ -29,23 +29,28 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return false;
}
enabled = applyZone(server, metadata);
if (!enabled) {
return false;
}
return applyStrategy(server, metadata);
}
@SuppressWarnings("unchecked")
private boolean applyVersion(Server server, Map<String, String> metadata) {
String versionJson = getVersionJson();
if (StringUtils.isEmpty(versionJson)) {
String versionValue = getVersionValue();
if (StringUtils.isEmpty(versionValue)) {
return true;
}
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String version = metadata.get(DiscoveryConstant.VERSION);
if (StringUtils.isEmpty(version)) {
return true;
return false;
}
Map<String, String> versionMap = JsonUtil.fromJson(versionJson, Map.class);
Map<String, String> versionMap = JsonUtil.fromJson(versionValue, Map.class);
String versions = versionMap.get(serviceId);
if (versions == null) {
return true;
......@@ -58,6 +63,24 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return false;
}
private boolean applyZone(Server server, Map<String, String> metadata) {
String zoneValue = getZoneValue();
if (StringUtils.isEmpty(zoneValue)) {
return true;
}
String zone = metadata.get(DiscoveryConstant.ZONE);
if (StringUtils.isEmpty(zone)) {
return false;
}
if (StringUtils.equals(zoneValue, zone)) {
return true;
}
return false;
}
private boolean applyStrategy(Server server, Map<String, String> metadata) {
if (discoveryEnabledStrategy == null) {
return true;
......@@ -66,5 +89,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return discoveryEnabledStrategy.apply(server, metadata);
}
protected abstract String getVersionJson();
protected abstract String getVersionValue();
protected abstract String getZoneValue();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment