Commit e36f6fb3 by Nepxion

增加基于随机权重的平滑灰度发布功能

parent 74992ca5
...@@ -29,5 +29,10 @@ ...@@ -29,5 +29,10 @@
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -25,6 +25,7 @@ public class RouterEntity implements Serializable { ...@@ -25,6 +25,7 @@ public class RouterEntity implements Serializable {
private String version; private String version;
private String host; private String host;
private int port; private int port;
private int weight = -1;
private String contextPath; private String contextPath;
private List<RouterEntity> nexts = new ArrayList<RouterEntity>(); private List<RouterEntity> nexts = new ArrayList<RouterEntity>();
...@@ -61,6 +62,14 @@ public class RouterEntity implements Serializable { ...@@ -61,6 +62,14 @@ public class RouterEntity implements Serializable {
this.port = port; this.port = port;
} }
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getContextPath() { public String getContextPath() {
return contextPath; return contextPath;
} }
......
package com.nepxion.discovery.console.desktop.serializer; package com.nepxion.discovery.common.util;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -17,7 +17,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; ...@@ -17,7 +17,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonSerializer { public class JsonUtil {
private static ObjectMapper objectMapper; private static ObjectMapper objectMapper;
static { static {
......
...@@ -50,11 +50,6 @@ ...@@ -50,11 +50,6 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId> <groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId> <artifactId>disruptor</artifactId>
<version>${disruptor.version}</version> <version>${disruptor.version}</version>
......
...@@ -21,10 +21,10 @@ import org.springframework.web.client.RestTemplate; ...@@ -21,10 +21,10 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.entity.ResultEntity; import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.entity.RouterEntity; import com.nepxion.discovery.common.entity.RouterEntity;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.common.util.UrlUtil; import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.console.desktop.context.PropertiesContext; import com.nepxion.discovery.console.desktop.context.PropertiesContext;
import com.nepxion.discovery.console.desktop.entity.Instance; import com.nepxion.discovery.console.desktop.entity.Instance;
import com.nepxion.discovery.console.desktop.serializer.JacksonSerializer;
public class ServiceController { public class ServiceController {
public static RestTemplate restTemplate; public static RestTemplate restTemplate;
...@@ -224,7 +224,7 @@ public class ServiceController { ...@@ -224,7 +224,7 @@ public class ServiceController {
private static <T> T convert(String result, TypeReference<T> typeReference) { private static <T> T convert(String result, TypeReference<T> typeReference) {
try { try {
return JacksonSerializer.fromJson(result, typeReference); return JsonUtil.fromJson(result, typeReference);
} catch (Exception e) { } catch (Exception e) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause(); result = errorHandler.getCause();
......
...@@ -142,10 +142,12 @@ public class RouterTopology extends AbstractTopology { ...@@ -142,10 +142,12 @@ public class RouterTopology extends AbstractTopology {
typeComboBox.setSelectedIndex(2); typeComboBox.setSelectedIndex(2);
JPanel childPanel2 = (JPanel) layoutPanel.getComponent(1); JPanel childPanel2 = (JPanel) layoutPanel.getComponent(1);
JSlider yOffsetSlider = (JSlider) childPanel2.getComponent(11);
yOffsetSlider.setValue(0);
JSlider xGapSlider = (JSlider) childPanel2.getComponent(13); JSlider xGapSlider = (JSlider) childPanel2.getComponent(13);
xGapSlider.setValue(200); xGapSlider.setValue(200);
JSlider yGapSlider = (JSlider) childPanel2.getComponent(15); JSlider yGapSlider = (JSlider) childPanel2.getComponent(15);
yGapSlider.setValue(100); yGapSlider.setValue(150);
JPanel childPanel3 = (JPanel) layoutPanel.getComponent(2); JPanel childPanel3 = (JPanel) layoutPanel.getComponent(2);
JBasicButton runButton = (JBasicButton) childPanel3.getComponent(1); JBasicButton runButton = (JBasicButton) childPanel3.getComponent(1);
...@@ -184,7 +186,7 @@ public class RouterTopology extends AbstractTopology { ...@@ -184,7 +186,7 @@ public class RouterTopology extends AbstractTopology {
index++; index++;
} }
addLink(node, nextNode); addLink(node, nextNode, next);
route(next, nextNode, index); route(next, nextNode, index);
} }
...@@ -215,9 +217,17 @@ public class RouterTopology extends AbstractTopology { ...@@ -215,9 +217,17 @@ public class RouterTopology extends AbstractTopology {
return node; return node;
} }
private void addLink(TNode fromNode, TNode toNode) { private void addLink(TNode fromNode, TNode toNode, RouterEntity routerEntity) {
int weight = routerEntity.getWeight();
TLink link = createLink(fromNode, toNode, true); TLink link = createLink(fromNode, toNode, true);
link.putLinkToArrowColor(Color.yellow); link.putLinkToArrowColor(Color.yellow);
if (weight > -1) {
link.setName("weight=" + weight);
link.putLinkFlowing(true);
link.putLinkFlowingColor(new Color(255, 155, 85));
link.putLinkFlowingWidth(3);
}
dataBox.addElement(link); dataBox.addElement(link);
} }
......
...@@ -19,6 +19,7 @@ import java.util.List; ...@@ -19,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint; import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
...@@ -35,9 +36,15 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -35,9 +36,15 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.DiscoveryEntity;
import com.nepxion.discovery.common.entity.RouterEntity; import com.nepxion.discovery.common.entity.RouterEntity;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.entity.WeightEntity;
import com.nepxion.discovery.common.entity.WeightFilterEntity;
import com.nepxion.discovery.common.exception.DiscoveryException; import com.nepxion.discovery.common.exception.DiscoveryException;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.common.util.UrlUtil; import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
...@@ -149,6 +156,7 @@ public class RouterEndpoint { ...@@ -149,6 +156,7 @@ public class RouterEndpoint {
String version = metadata.get(DiscoveryConstant.VERSION); String version = metadata.get(DiscoveryConstant.VERSION);
String host = instance.getHost(); String host = instance.getHost();
int port = instance.getPort(); int port = instance.getPort();
int weight = getWeight(routeServiceId, version);
String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH); String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
RouterEntity routerEntity = new RouterEntity(); RouterEntity routerEntity = new RouterEntity();
...@@ -156,6 +164,7 @@ public class RouterEndpoint { ...@@ -156,6 +164,7 @@ public class RouterEndpoint {
routerEntity.setVersion(version); routerEntity.setVersion(version);
routerEntity.setHost(host); routerEntity.setHost(host);
routerEntity.setPort(port); routerEntity.setPort(port);
routerEntity.setWeight(weight);
routerEntity.setContextPath(contextPath); routerEntity.setContextPath(contextPath);
routerEntityList.add(routerEntity); routerEntityList.add(routerEntity);
...@@ -164,39 +173,22 @@ public class RouterEndpoint { ...@@ -164,39 +173,22 @@ public class RouterEndpoint {
return routerEntityList; return routerEntityList;
} }
@SuppressWarnings("unchecked")
public List<RouterEntity> getRouterEntityList(String routeServiceId, String routeHost, int routePort, String routeContextPath) { public List<RouterEntity> getRouterEntityList(String routeServiceId, String routeHost, int routePort, String routeContextPath) {
String url = "http://" + routeHost + ":" + routePort + UrlUtil.formatContextPath(routeContextPath) + "router/instances/" + routeServiceId; String url = "http://" + routeHost + ":" + routePort + UrlUtil.formatContextPath(routeContextPath) + "router/route/" + routeServiceId;
List<Map<String, ?>> instanceList = null; String result = null;
try { try {
instanceList = routerRestTemplate.getForEntity(url, List.class).getBody(); result = routerRestTemplate.getForEntity(url, String.class).getBody();
} catch (RestClientException e) { } catch (RestClientException e) {
throw new DiscoveryException("Get instance list for route serviceId=" + routeServiceId + " with url=" + url + " failed", e); throw new DiscoveryException("Get instance list for route serviceId=" + routeServiceId + " with url=" + url + " failed", e);
} }
if (CollectionUtils.isEmpty(instanceList)) { if (StringUtils.isEmpty(result)) {
return null; return null;
} }
List<RouterEntity> routerEntityList = new ArrayList<RouterEntity>(); List<RouterEntity> routerEntityList = JsonUtil.fromJson(result, new TypeReference<List<RouterEntity>>() {
for (Map<String, ?> instance : instanceList) { });
Map<String, String> metadata = (Map<String, String>) instance.get(DiscoveryConstant.METADATA);
String serviceId = instance.get(DiscoveryConstant.SERVICE_ID).toString().toLowerCase();
String version = metadata.get(DiscoveryConstant.VERSION);
String host = instance.get(DiscoveryConstant.HOST).toString();
Integer port = (Integer) instance.get(DiscoveryConstant.PORT);
String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
RouterEntity routerEntity = new RouterEntity();
routerEntity.setServiceId(serviceId);
routerEntity.setVersion(version);
routerEntity.setHost(host);
routerEntity.setPort(port);
routerEntity.setContextPath(contextPath);
routerEntityList.add(routerEntity);
}
return routerEntityList; return routerEntityList;
} }
...@@ -266,4 +258,46 @@ public class RouterEndpoint { ...@@ -266,4 +258,46 @@ public class RouterEndpoint {
return routerEntityList; return routerEntityList;
} }
private int getWeight(String providerServiceId, String providerVersion) {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity == null) {
return -1;
}
DiscoveryEntity discoveryEntity = ruleEntity.getDiscoveryEntity();
if (discoveryEntity == null) {
return -1;
}
WeightFilterEntity weightFilterEntity = discoveryEntity.getWeightFilterEntity();
if (weightFilterEntity == null) {
return -1;
}
Map<String, List<WeightEntity>> weightEntityMap = weightFilterEntity.getWeightEntityMap();
if (MapUtils.isEmpty(weightEntityMap)) {
return -1;
}
String serviceId = pluginAdapter.getServiceId();
List<WeightEntity> weightEntityList = weightEntityMap.get(serviceId);
if (CollectionUtils.isEmpty(weightEntityList)) {
return -1;
}
for (WeightEntity weightEntity : weightEntityList) {
String providerServiceName = weightEntity.getProviderServiceName();
if (StringUtils.equalsIgnoreCase(providerServiceName, providerServiceId)) {
Map<String, Integer> weightMap = weightEntity.getWeightMap();
Integer weight = weightMap.get(providerVersion);
if (weight != null) {
return weight;
}
}
}
return -1;
}
} }
\ 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