Commit 5012a387 by Nepxion

增加全链路路由的配置中心配置方式

parent f309179b
......@@ -21,6 +21,7 @@ public class RuleEntity implements Serializable {
private RegisterEntity registerEntity;
private DiscoveryEntity discoveryEntity;
private StrategyEntity strategyEntity;
private CustomizationEntity customizationEntity;
private String content;
......@@ -48,6 +49,14 @@ public class RuleEntity implements Serializable {
this.customizationEntity = customizationEntity;
}
public StrategyEntity getStrategyEntity() {
return strategyEntity;
}
public void setStrategyEntity(StrategyEntity strategyEntity) {
this.strategyEntity = strategyEntity;
}
public String getContent() {
return content;
}
......
package com.nepxion.discovery.common.entity;
import java.io.Serializable;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class StrategyEntity implements Serializable {
private static final long serialVersionUID = -588258474716367451L;
private String versionValue;
private String regionValue;
private String addressValue;
public String getVersionValue() {
return versionValue;
}
public void setVersionValue(String versionValue) {
this.versionValue = versionValue;
}
public String getRegionValue() {
return regionValue;
}
public void setRegionValue(String regionValue) {
this.regionValue = regionValue;
}
public String getAddressValue() {
return addressValue;
}
public void setAddressValue(String addressValue) {
this.addressValue = addressValue;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ public class ConfigConstant {
public static final String RULE_ELEMENT_NAME = "rule";
public static final String REGISTER_ELEMENT_NAME = "register";
public static final String DISCOVERY_ELEMENT_NAME = "discovery";
public static final String STRATEGY_ELEMENT_NAME = "strategy";
public static final String CUSTOMIZATION_ELEMENT_NAME = "customization";
public static final String SERVICE_ELEMENT_NAME = "service";
public static final String REGION_ELEMENT_NAME = "region";
......@@ -21,6 +22,7 @@ public class ConfigConstant {
public static final String COUNT_ELEMENT_NAME = "count";
public static final String VERSION_ELEMENT_NAME = "version";
public static final String WEIGHT_ELEMENT_NAME = "weight";
public static final String ADDRESS_ELEMENT_NAME = "address";
public static final String FILTER_VALUE_ATTRIBUTE_NAME = "filter-value";
public static final String SERVICE_NAME_ATTRIBUTE_NAME = "service-name";
public static final String CONSUMER_SERVICE_NAME_ATTRIBUTE_NAME = "consumer-service-name";
......
......@@ -32,6 +32,7 @@ import com.nepxion.discovery.common.entity.HostFilterEntity;
import com.nepxion.discovery.common.entity.RegionWeightEntity;
import com.nepxion.discovery.common.entity.RegisterEntity;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.common.entity.VersionEntity;
import com.nepxion.discovery.common.entity.VersionFilterEntity;
import com.nepxion.discovery.common.entity.WeightEntity;
......@@ -83,6 +84,7 @@ public class XmlConfigParser implements PluginConfigParser {
RegisterEntity registerEntity = null;
DiscoveryEntity discoveryEntity = null;
StrategyEntity strategyEntity = null;
CustomizationEntity customizationEntity = null;
for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
Object childElementObject = elementIterator.next();
......@@ -95,6 +97,9 @@ public class XmlConfigParser implements PluginConfigParser {
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.DISCOVERY_ELEMENT_NAME)) {
discoveryEntity = new DiscoveryEntity();
parseDiscovery(childElement, discoveryEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.STRATEGY_ELEMENT_NAME)) {
strategyEntity = new StrategyEntity();
parseStrategy(childElement, strategyEntity);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.CUSTOMIZATION_ELEMENT_NAME)) {
customizationEntity = new CustomizationEntity();
parseCustomization(childElement, customizationEntity);
......@@ -105,6 +110,7 @@ public class XmlConfigParser implements PluginConfigParser {
RuleEntity ruleEntity = new RuleEntity();
ruleEntity.setRegisterEntity(registerEntity);
ruleEntity.setDiscoveryEntity(discoveryEntity);
ruleEntity.setStrategyEntity(strategyEntity);
ruleEntity.setCustomizationEntity(customizationEntity);
ruleEntity.setContent(config);
......@@ -152,6 +158,27 @@ public class XmlConfigParser implements PluginConfigParser {
}
@SuppressWarnings("rawtypes")
private void parseStrategy(Element element, StrategyEntity strategyEntity) {
for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
Object childElementObject = elementIterator.next();
if (childElementObject instanceof Element) {
Element childElement = (Element) childElementObject;
if (StringUtils.equals(childElement.getName(), ConfigConstant.VERSION_ELEMENT_NAME)) {
String versionValue = childElement.getTextTrim();
strategyEntity.setVersionValue(versionValue);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.REGION_ELEMENT_NAME)) {
String regionValue = childElement.getTextTrim();
strategyEntity.setRegionValue(regionValue);
} else if (StringUtils.equals(childElement.getName(), ConfigConstant.ADDRESS_ELEMENT_NAME)) {
String addressValue = childElement.getTextTrim();
strategyEntity.setAddressValue(addressValue);
}
}
}
}
@SuppressWarnings("rawtypes")
private void parseCustomization(Element element, CustomizationEntity customizationEntity) {
Map<String, Map<String, String>> customizationMap = customizationEntity.getCustomizationMap();
for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) {
......
......@@ -16,14 +16,20 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.common.util.StringUtil;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.netflix.loadbalancer.Server;
public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter {
@Autowired(required = false)
private DiscoveryEnabledStrategy discoveryEnabledStrategy;
@Autowired
private PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server, Map<String, String> metadata) {
boolean enabled = applyVersion(server, metadata);
......@@ -48,6 +54,16 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
private boolean applyVersion(Server server, Map<String, String> metadata) {
String versionValue = getVersionValue(server);
if (StringUtils.isEmpty(versionValue)) {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
versionValue = strategyEntity.getVersionValue();
}
}
}
if (StringUtils.isEmpty(versionValue)) {
return true;
}
......@@ -81,6 +97,16 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
private boolean applyRegion(Server server, Map<String, String> metadata) {
String regionValue = getRegionValue(server);
if (StringUtils.isEmpty(regionValue)) {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
regionValue = strategyEntity.getRegionValue();
}
}
}
if (StringUtils.isEmpty(regionValue)) {
return true;
}
......@@ -114,6 +140,16 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
private boolean applyAddress(Server server, Map<String, String> metadata) {
String addressValue = getAddressValue(server);
if (StringUtils.isEmpty(addressValue)) {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
addressValue = strategyEntity.getAddressValue();
}
}
}
if (StringUtils.isEmpty(addressValue)) {
return true;
}
......
......@@ -112,6 +112,11 @@
}
}
},
"strategyEntity": {
"versionValue": "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}",
"regionValue": "{\"discovery-springcloud-example-a\":\"qa;dev\", \"discovery-springcloud-example-b\":\"dev\", \"discovery-springcloud-example-c\":\"qa\"}",
"addressValue": "{\"discovery-springcloud-example-a\":\"192.168.43.101:1100\", \"discovery-springcloud-example-b\":\"192.168.43.101:1201\", \"discovery-springcloud-example-c\":\"192.168.43.101:1300\"}"
},
"customizationEntity": {
"customizationMap": {
"discovery-springcloud-example-a": {
......
......@@ -88,6 +88,14 @@
</weight>
</discovery>
<strategy>
<!-- <version>{"discovery-springcloud-example-a":"1.0", "discovery-springcloud-example-b":"1.0", "discovery-springcloud-example-c":"1.0;1.2"}</version> -->
<!-- <version>1.0</version> -->
<!-- <region>{"discovery-springcloud-example-a":"qa;dev", "discovery-springcloud-example-b":"dev", "discovery-springcloud-example-c":"qa"}</region> -->
<!-- <region>dev</region> -->
<!-- <address>{"discovery-springcloud-example-a":"192.168.43.101:1100", "discovery-springcloud-example-b":"192.168.43.101:1201", "discovery-springcloud-example-c":"192.168.43.101:1300"}</address> -->
</strategy>
<!-- 客户定制化控制,由远程推送客户化参数的改变,实现一些特色化的灰度发布,例如,基于数据库的灰度发布 -->
<customization>
<!-- 服务a和c分别有两个库的配置,分别是测试数据库(database的value为qa)和生产数据库(database的value为prod) -->
......
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