Commit b3999a2f by Nepxion

增加IP地址和端口去路由的功能

parent 56176321
......@@ -25,6 +25,7 @@ public class DiscoveryConstant {
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String GROUP = "group";
public static final String REGION = "region";
public static final String ADDRESS = "address";
public static final String SERVICE_ID = "serviceId";
public static final String HOST = "host";
public static final String PORT = "port";
......
......@@ -52,4 +52,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return exchange.getRequest().getHeaders().getFirst(DiscoveryConstant.REGION);
}
@Override
protected String getAddressValue(Server server) {
ServerWebExchange exchange = gatewayStrategyContextHolder.getExchange();
if (exchange == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
LOG.warn("The ServerWebExchange object is null, ignore to do region filter for service={}...", serviceId);
return null;
}
return exchange.getRequest().getHeaders().getFirst(DiscoveryConstant.ADDRESS);
}
}
\ No newline at end of file
......@@ -52,4 +52,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return attributes.getRequest().getHeader(DiscoveryConstant.REGION);
}
@Override
protected String getAddressValue(Server server) {
ServletRequestAttributes attributes = serviceStrategyContextHolder.getRestAttributes();
if (attributes == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
LOG.warn("The ServletRequestAttributes object is null, ignore to do region filter for service={}...", serviceId);
return null;
}
return attributes.getRequest().getHeader(DiscoveryConstant.ADDRESS);
}
}
\ No newline at end of file
......@@ -53,4 +53,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return request.getHeader(DiscoveryConstant.REGION);
}
@Override
protected String getAddressValue(Server server) {
HttpServletRequest request = zuulStrategyContextHolder.getRequest();
if (request == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
LOG.warn("The HttpServletRequest object is null, ignore to do region filter for service={}...", serviceId);
return null;
}
return request.getHeader(DiscoveryConstant.ADDRESS);
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ package com.nepxion.discovery.plugin.strategy.adapter;
* @version 1.0
*/
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
......@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.common.util.StringUtil;
import com.netflix.loadbalancer.Server;
public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter {
......@@ -34,6 +36,11 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return false;
}
enabled = applyAddress(server, metadata);
if (!enabled) {
return false;
}
return applyStrategy(server, metadata);
}
......@@ -81,6 +88,28 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return false;
}
@SuppressWarnings("unchecked")
private boolean applyAddress(Server server, Map<String, String> metadata) {
String addressValue = getAddressValue(server);
if (StringUtils.isEmpty(addressValue)) {
return true;
}
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
Map<String, String> addressMap = JsonUtil.fromJson(addressValue, Map.class);
String addresses = addressMap.get(serviceId);
if (addresses == null) {
return true;
}
List<String> addressList = StringUtil.splitToList(addresses, DiscoveryConstant.SEPARATE);
if (addressList.contains(server.getHostPort()) || addressList.contains(server.getHost())) {
return true;
}
return false;
}
private boolean applyStrategy(Server server, Map<String, String> metadata) {
if (discoveryEnabledStrategy == null) {
return true;
......@@ -92,4 +121,6 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
protected abstract String getVersionValue(Server server);
protected abstract String getRegionValue(Server server);
protected abstract String getAddressValue(Server server);
}
\ No newline at end of file
......@@ -93,7 +93,7 @@ spring.application.strategy.scan.packages=com.nepxion.discovery.plugin.example.s
# 启动和关闭用户自定义和编程灰度路由策略的时候,对REST方式的调用拦截。缺失则默认为false
spring.application.strategy.rest.intercept.enabled=true
# 用户自定义和编程灰度路由策略的时候,对REST方式调用拦截的时候(支持Feign或者RestTemplate调用),需要把来自外部的指定Header参数传递到服务里,如果多个用“;”分隔,不允许出现空格。该项配置只对服务有效,对网关无效
spring.application.strategy.request.headers=version;region;token
spring.application.strategy.request.headers=version;region;address;token
# 启动和关闭用户自定义和编程灰度路由策略的时候日志打印,注意每调用一次都会打印一次,会对性能有所影响,建议压测环境和生产环境关闭。缺失则默认为false
spring.application.strategy.intercept.log.print=true
# 开启服务端实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失
......
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