Commit 4bf2cd9e by Nepxion

元数据增加服务名

parent 5012a387
......@@ -36,6 +36,7 @@ public class ConsulApplicationContextInitializer extends PluginApplicationContex
consulDiscoveryProperties.setPreferIpAddress(true);
List<String> tags = consulDiscoveryProperties.getTags();
tags.add(DiscoveryConstant.SPRING_APPLICATION_NAME + "=" + PluginContextAware.getApplicationName(environment));
tags.add(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_PLUGIN + "=" + ConsulConstant.DISCOVERY_PLUGIN);
tags.add(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_VERSION + "=" + DiscoveryConstant.DISCOVERY_VERSION);
tags.add(DiscoveryConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED + "=" + PluginContextAware.isRegisterControlEnabled(environment));
......
......@@ -36,6 +36,7 @@ public class EurekaApplicationContextInitializer extends PluginApplicationContex
eurekaInstanceConfig.setPreferIpAddress(true);
Map<String, String> metadataMap = eurekaInstanceConfig.getMetadataMap();
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_NAME, PluginContextAware.getApplicationName(environment));
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_PLUGIN, EurekaConstant.DISCOVERY_PLUGIN);
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_VERSION, DiscoveryConstant.DISCOVERY_VERSION);
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString());
......
......@@ -35,6 +35,7 @@ public class NacosApplicationContextInitializer extends PluginApplicationContext
NacosDiscoveryProperties nacosDiscoveryProperties = (NacosDiscoveryProperties) bean;
Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
metadata.put(DiscoveryConstant.SPRING_APPLICATION_NAME, PluginContextAware.getApplicationName(environment));
metadata.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_PLUGIN, NacosConstant.DISCOVERY_PLUGIN);
metadata.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_VERSION, DiscoveryConstant.DISCOVERY_VERSION);
metadata.put(DiscoveryConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString());
......
......@@ -36,6 +36,7 @@ public class ZookeeperApplicationContextInitializer extends PluginApplicationCon
zookeeperDiscoveryProperties.setPreferIpAddress(true);
Map<String, String> metadata = zookeeperDiscoveryProperties.getMetadata();
metadata.put(DiscoveryConstant.SPRING_APPLICATION_NAME, PluginContextAware.getApplicationName(environment));
metadata.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_PLUGIN, ZookeeperConstant.DISCOVERY_PLUGIN);
metadata.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_VERSION, DiscoveryConstant.DISCOVERY_VERSION);
metadata.put(DiscoveryConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, PluginContextAware.isRegisterControlEnabled(environment).toString());
......
......@@ -148,6 +148,11 @@ public abstract class AbstractPluginAdapter implements PluginAdapter {
}
@Override
public String getServerServiceId(Server server) {
return getServerMetadata(server).get(DiscoveryConstant.SPRING_APPLICATION_NAME).toLowerCase();
}
@Override
public String getServerVersion(Server server) {
return getServerMetadata(server).get(DiscoveryConstant.VERSION);
}
......
......@@ -53,6 +53,8 @@ public interface PluginAdapter {
Map<String, String> getServerMetadata(Server server);
String getServerServiceId(Server server);
String getServerVersion(Server server);
String getServerRegion(Server server);
......
......@@ -133,6 +133,10 @@ public class PluginContextAware implements ApplicationContextAware {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_CONFIG_PATH, String.class, StringUtils.equals(getConfigFormat(environment), DiscoveryConstant.XML_FORMAT) ? DiscoveryConstant.PREFIX_CLASSPATH + DiscoveryConstant.RULE + "." + DiscoveryConstant.XML_FORMAT : DiscoveryConstant.PREFIX_CLASSPATH + DiscoveryConstant.RULE + "." + DiscoveryConstant.JSON_FORMAT);
}
public static String getApplicationName(Environment environment) {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_NAME);
}
public static String getGroupKey(Environment environment) {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY, String.class, DiscoveryConstant.GROUP);
}
......
......@@ -96,7 +96,7 @@ public class WeightRandomLoadBalance {
Map<String, List<WeightEntity>> weightEntityMap = weightFilterEntity.getWeightEntityMap();
RegionWeightEntity regionWeightEntity = weightFilterEntity.getRegionWeightEntity();
String providerServiceId = server.getMetaInfo().getAppName();
String providerServiceId = pluginAdapter.getServerServiceId(server);
String providerVersion = pluginAdapter.getServerVersion(server);
String providerRegion = pluginAdapter.getServerRegion(server);
......
......@@ -29,7 +29,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getVersionValue(Server server) {
ServerWebExchange exchange = gatewayStrategyContextHolder.getExchange();
if (exchange == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServerWebExchange object is null, ignore to do version filter for service={}...", serviceId);
......@@ -43,7 +43,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getRegionValue(Server server) {
ServerWebExchange exchange = gatewayStrategyContextHolder.getExchange();
if (exchange == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServerWebExchange object is null, ignore to do region filter for service={}...", serviceId);
......@@ -57,7 +57,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getAddressValue(Server server) {
ServerWebExchange exchange = gatewayStrategyContextHolder.getExchange();
if (exchange == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServerWebExchange object is null, ignore to do region filter for service={}...", serviceId);
......
......@@ -29,7 +29,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getVersionValue(Server server) {
ServletRequestAttributes attributes = serviceStrategyContextHolder.getRestAttributes();
if (attributes == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServletRequestAttributes object is null, ignore to do version filter for service={}...", serviceId);
......@@ -43,7 +43,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getRegionValue(Server server) {
ServletRequestAttributes attributes = serviceStrategyContextHolder.getRestAttributes();
if (attributes == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServletRequestAttributes object is null, ignore to do region filter for service={}...", serviceId);
......@@ -57,7 +57,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getAddressValue(Server server) {
ServletRequestAttributes attributes = serviceStrategyContextHolder.getRestAttributes();
if (attributes == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The ServletRequestAttributes object is null, ignore to do region filter for service={}...", serviceId);
......
......@@ -30,7 +30,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getVersionValue(Server server) {
HttpServletRequest request = zuulStrategyContextHolder.getRequest();
if (request == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The HttpServletRequest object is null, ignore to do version filter for service={}...", serviceId);
......@@ -44,7 +44,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getRegionValue(Server server) {
HttpServletRequest request = zuulStrategyContextHolder.getRequest();
if (request == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The HttpServletRequest object is null, ignore to do region filter for service={}...", serviceId);
......@@ -58,7 +58,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
protected String getAddressValue(Server server) {
HttpServletRequest request = zuulStrategyContextHolder.getRequest();
if (request == null) {
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.warn("The HttpServletRequest object is null, ignore to do region filter for service={}...", serviceId);
......
......@@ -28,7 +28,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
private DiscoveryEnabledStrategy discoveryEnabledStrategy;
@Autowired
private PluginAdapter pluginAdapter;
protected PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server, Map<String, String> metadata) {
......@@ -75,7 +75,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
String versions = null;
try {
Map<String, String> versionMap = JsonUtil.fromJson(versionValue, Map.class);
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
versions = versionMap.get(serviceId);
} catch (Exception e) {
versions = versionValue;
......@@ -118,7 +118,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
String regions = null;
try {
Map<String, String> regionMap = JsonUtil.fromJson(regionValue, Map.class);
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
regions = regionMap.get(serviceId);
} catch (Exception e) {
regions = regionValue;
......@@ -154,7 +154,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
}
Map<String, String> addressMap = JsonUtil.fromJson(addressValue, Map.class);
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
String addresses = addressMap.get(serviceId);
if (StringUtils.isEmpty(addresses)) {
return true;
......
......@@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.server.ServerWebExchange;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.strategy.gateway.context.GatewayStrategyContextHolder;
import com.netflix.loadbalancer.Server;
......@@ -28,6 +29,9 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
@Autowired
private GatewayStrategyContextHolder gatewayStrategyContextHolder;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server, Map<String, String> metadata) {
// 对Rest调用传来的Header参数(例如Token)做策略
......@@ -44,7 +48,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
String token = exchange.getRequest().getHeaders().getFirst("token");
// String value = exchange.getRequest().getQueryParams().getFirst("value");
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.info("Gateway端负载均衡用户定制触发:serviceId={}, host={}, metadata={}", serviceId, server.toString(), metadata);
......
......@@ -18,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.strategy.service.constant.ServiceStrategyConstant;
import com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder;
......@@ -30,6 +31,9 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
@Autowired
private ServiceStrategyContextHolder serviceStrategyContextHolder;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server, Map<String, String> metadata) {
// 对Rest调用传来的Header参数(例如Token)做策略
......@@ -52,7 +56,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
String token = attributes.getRequest().getHeader("token");
// String value = attributes.getRequest().getParameter("value");
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.info("Serivice端负载均衡用户定制触发:serviceId={}, host={}, metadata={}, attributes={}", serviceId, server.toString(), metadata, attributes);
......@@ -72,7 +76,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
private boolean applyFromMethod(Server server, Map<String, String> metadata) {
Map<String, Object> attributes = serviceStrategyContextHolder.getRpcAttributes();
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
String version = metadata.get(DiscoveryConstant.VERSION);
LOG.info("Serivice端负载均衡用户定制触发:serviceId={}, host={}, metadata={}, attributes={}", serviceId, server.toString(), metadata, attributes);
......
......@@ -18,6 +18,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.strategy.zuul.context.ZuulStrategyContextHolder;
import com.netflix.loadbalancer.Server;
......@@ -29,6 +30,9 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
@Autowired
private ZuulStrategyContextHolder zuulStrategyContextHolder;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server, Map<String, String> metadata) {
// 对Rest调用传来的Header参数(例如Token)做策略
......@@ -45,7 +49,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
String token = request.getHeader("token");
// String value = request.getParameter("value");
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
String serviceId = pluginAdapter.getServerServiceId(server);
LOG.info("Zuul端负载均衡用户定制触发:serviceId={}, host={}, metadata={}", serviceId, server.toString(), metadata);
......
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