Commit bb04aed3 by Nepxion

增加路由中心

parent 4b62dce0
...@@ -12,13 +12,10 @@ package com.nepxion.discovery.plugin.framework.configuration; ...@@ -12,13 +12,10 @@ package com.nepxion.discovery.plugin.framework.configuration;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler; import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.controller.PluginRouterController;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.listener.DiscoveryListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.DiscoveryListenerExecutor;
...@@ -28,7 +25,6 @@ import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterRegis ...@@ -28,7 +25,6 @@ import com.nepxion.discovery.plugin.framework.listener.impl.IpAddressFilterRegis
import com.nepxion.discovery.plugin.framework.listener.impl.VersionFilterDiscoveryListener; import com.nepxion.discovery.plugin.framework.listener.impl.VersionFilterDiscoveryListener;
@Configuration @Configuration
@ComponentScan(basePackages = { "com.nepxion.discovery.plugin.framework.controller" })
public class PluginAutoConfiguration { public class PluginAutoConfiguration {
static { static {
System.out.println(""); System.out.println("");
...@@ -45,11 +41,6 @@ public class PluginAutoConfiguration { ...@@ -45,11 +41,6 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public RestTemplate pluginRestTemplate() {
return new RestTemplate();
}
@Bean
public PluginContainerInitializedHandler pluginContainerInitializedHandler() { public PluginContainerInitializedHandler pluginContainerInitializedHandler() {
return new PluginContainerInitializedHandler(); return new PluginContainerInitializedHandler();
} }
...@@ -65,11 +56,6 @@ public class PluginAutoConfiguration { ...@@ -65,11 +56,6 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public PluginRouterController pluginRouterController() {
return new PluginRouterController();
}
@Bean
public RuleEntity ruleEntity() { public RuleEntity ruleEntity() {
return new RuleEntity(); return new RuleEntity();
} }
......
...@@ -20,7 +20,7 @@ import org.apache.commons.lang3.builder.ToStringStyle; ...@@ -20,7 +20,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
public class RouteEntity implements Serializable { public class RouterEntity implements Serializable {
private static final long serialVersionUID = -4480475963615166799L; private static final long serialVersionUID = -4480475963615166799L;
private String serviceId; private String serviceId;
...@@ -28,7 +28,7 @@ public class RouteEntity implements Serializable { ...@@ -28,7 +28,7 @@ public class RouteEntity implements Serializable {
private String host; private String host;
private int port; private int port;
private List<RouteEntity> nexts = new ArrayList<RouteEntity>(); private List<RouterEntity> nexts = new ArrayList<RouterEntity>();
public String getServiceId() { public String getServiceId() {
return serviceId; return serviceId;
...@@ -62,11 +62,11 @@ public class RouteEntity implements Serializable { ...@@ -62,11 +62,11 @@ public class RouteEntity implements Serializable {
this.port = port; this.port = port;
} }
public List<RouteEntity> getNexts() { public List<RouterEntity> getNexts() {
return nexts; return nexts;
} }
public void setNexts(List<RouteEntity> nexts) { public void setNexts(List<RouterEntity> nexts) {
this.nexts = nexts; this.nexts = nexts;
} }
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>discovery-plugin-router-center</artifactId>
<name>Nepxion Discovery Plugin Router Center</name>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
<description>Nepxion Discovery is an enhancement for Spring Cloud Discovery</description>
<url>http://www.nepxion.com</url>
<parent>
<groupId>com.nepxion</groupId>
<artifactId>discovery</artifactId>
<version>2.0.10</version>
</parent>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-framework</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.nepxion.discovery.plugin.routercenter.configuration;
/**
* <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.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.plugin.routercenter.controller.RouterController;
@Configuration
@ComponentScan(basePackages = { "com.nepxion.discovery.plugin.routercenter.controller" })
public class RouterAutoConfiguration {
@Bean
public RestTemplate routerRestTemplate() {
return new RestTemplate();
}
@Bean
public RouterController routerController() {
return new RouterController();
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.framework.controller; package com.nepxion.discovery.plugin.routercenter.controller;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -30,12 +30,12 @@ import org.springframework.web.client.RestTemplate; ...@@ -30,12 +30,12 @@ import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler; import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RouteEntity; import com.nepxion.discovery.plugin.framework.entity.RouterEntity;
import com.nepxion.discovery.plugin.framework.exception.PluginException; import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.eventbus.util.HostUtil; import com.nepxion.eventbus.util.HostUtil;
@RestController @RestController
public class PluginRouterController { public class RouterController {
@Autowired @Autowired
private PluginContainerInitializedHandler pluginContainerInitializedHandler; private PluginContainerInitializedHandler pluginContainerInitializedHandler;
...@@ -43,7 +43,7 @@ public class PluginRouterController { ...@@ -43,7 +43,7 @@ public class PluginRouterController {
private PluginContextAware pluginContextAware; private PluginContextAware pluginContextAware;
@Autowired @Autowired
private RestTemplate pluginRestTemplate; private RestTemplate routerRestTemplate;
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
...@@ -56,25 +56,25 @@ public class PluginRouterController { ...@@ -56,25 +56,25 @@ public class PluginRouterController {
// 获取本地节点的路由信息 // 获取本地节点的路由信息
@RequestMapping(path = "/info", method = RequestMethod.GET) @RequestMapping(path = "/info", method = RequestMethod.GET)
public RouteEntity info() { public RouterEntity info() {
return getRouteEntity(); return getRouterEntity();
} }
// 获取本地节点可访问其他节点(根据服务名)的路由信息列表 // 获取本地节点可访问其他节点(根据服务名)的路由信息列表
@RequestMapping(path = "/routes/{routeServiceId}", method = RequestMethod.GET) @RequestMapping(path = "/routes/{routeServiceId}", method = RequestMethod.GET)
public List<RouteEntity> routes(@PathVariable(value = "routeServiceId") String routeServiceId) { public List<RouterEntity> routes(@PathVariable(value = "routeServiceId") String routeServiceId) {
return getRouteEntityList(routeServiceId); return getRouterEntityList(routeServiceId);
} }
// 获取指定节点(根据IP和端口)可访问其他节点(根据服务名)的路由信息列表 // 获取指定节点(根据IP和端口)可访问其他节点(根据服务名)的路由信息列表
@RequestMapping(path = "/routes/{routeServiceId}/{routeHost}/{routePort}", method = RequestMethod.GET) @RequestMapping(path = "/routes/{routeServiceId}/{routeHost}/{routePort}", method = RequestMethod.GET)
public List<RouteEntity> routes(@PathVariable(value = "routeServiceId") String routeServiceId, @PathVariable(value = "routeHost") String routeHost, @PathVariable(value = "routePort") int routePort) { public List<RouterEntity> routes(@PathVariable(value = "routeServiceId") String routeServiceId, @PathVariable(value = "routeHost") String routeHost, @PathVariable(value = "routePort") int routePort) {
return getRouteEntityList(routeServiceId, routeHost, routePort); return getRouterEntityList(routeServiceId, routeHost, routePort);
} }
// 获取全路径的路由信息 // 获取全路径的路由信息
@RequestMapping(path = "/routeAll", method = RequestMethod.POST) @RequestMapping(path = "/routeAll", method = RequestMethod.POST)
public RouteEntity routeAll(@RequestBody String serviceIds) { public RouterEntity routeAll(@RequestBody String serviceIds) {
return route(serviceIds); return route(serviceIds);
} }
...@@ -82,22 +82,22 @@ public class PluginRouterController { ...@@ -82,22 +82,22 @@ public class PluginRouterController {
return discoveryClient.getInstances(serviceId); return discoveryClient.getInstances(serviceId);
} }
public RouteEntity getRouteEntity() { public RouterEntity getRouterEntity() {
String serviceId = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_NAME); String serviceId = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_NAME);
String version = pluginContextAware.getEnvironment().getProperty(PluginConstant.EUREKA_METADATA_VERSION); String version = pluginContextAware.getEnvironment().getProperty(PluginConstant.EUREKA_METADATA_VERSION);
String host = HostUtil.getLocalhost(); String host = HostUtil.getLocalhost();
int port = pluginContainerInitializedHandler.getPort(); int port = pluginContainerInitializedHandler.getPort();
RouteEntity routeEntity = new RouteEntity(); RouterEntity routerEntity = new RouterEntity();
routeEntity.setServiceId(serviceId); routerEntity.setServiceId(serviceId);
routeEntity.setVersion(version); routerEntity.setVersion(version);
routeEntity.setHost(host); routerEntity.setHost(host);
routeEntity.setPort(port); routerEntity.setPort(port);
return routeEntity; return routerEntity;
} }
public List<RouteEntity> getRouteEntityList(String routeServiceId) { public List<RouterEntity> getRouterEntityList(String routeServiceId) {
List<ServiceInstance> instanceList = null; List<ServiceInstance> instanceList = null;
try { try {
...@@ -110,32 +110,32 @@ public class PluginRouterController { ...@@ -110,32 +110,32 @@ public class PluginRouterController {
return null; return null;
} }
List<RouteEntity> routeEntityList = new ArrayList<RouteEntity>(); List<RouterEntity> routerEntityList = new ArrayList<RouterEntity>();
for (ServiceInstance instance : instanceList) { for (ServiceInstance instance : instanceList) {
String serviceId = instance.getServiceId().toLowerCase(); String serviceId = instance.getServiceId().toLowerCase();
String version = instance.getMetadata().get(PluginConstant.VERSION); String version = instance.getMetadata().get(PluginConstant.VERSION);
String host = instance.getHost(); String host = instance.getHost();
int port = instance.getPort(); int port = instance.getPort();
RouteEntity routeEntity = new RouteEntity(); RouterEntity routerEntity = new RouterEntity();
routeEntity.setServiceId(serviceId); routerEntity.setServiceId(serviceId);
routeEntity.setVersion(version); routerEntity.setVersion(version);
routeEntity.setHost(host); routerEntity.setHost(host);
routeEntity.setPort(port); routerEntity.setPort(port);
routeEntityList.add(routeEntity); routerEntityList.add(routerEntity);
} }
return routeEntityList; return routerEntityList;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<RouteEntity> getRouteEntityList(String routeServiceId, String routeHost, int routePort) { public List<RouterEntity> getRouterEntityList(String routeServiceId, String routeHost, int routePort) {
String url = "http://" + routeHost + ":" + routePort + "/" + PluginConstant.INSTANCES + "/" + routeServiceId; String url = "http://" + routeHost + ":" + routePort + "/" + PluginConstant.INSTANCES + "/" + routeServiceId;
List<Map<String, ?>> instanceList = null; List<Map<String, ?>> instanceList = null;
try { try {
instanceList = pluginRestTemplate.getForEntity(url, List.class).getBody(); instanceList = routerRestTemplate.getForEntity(url, List.class).getBody();
} catch (RestClientException e) { } catch (RestClientException e) {
throw new PluginException("Get instance list for serviceId=" + routeServiceId + " with url=" + url + " failed", e); throw new PluginException("Get instance list for serviceId=" + routeServiceId + " with url=" + url + " failed", e);
} }
...@@ -144,26 +144,26 @@ public class PluginRouterController { ...@@ -144,26 +144,26 @@ public class PluginRouterController {
return null; return null;
} }
List<RouteEntity> routeEntityList = new ArrayList<RouteEntity>(); List<RouterEntity> routerEntityList = new ArrayList<RouterEntity>();
for (Map<String, ?> instance : instanceList) { for (Map<String, ?> instance : instanceList) {
String serviceId = instance.get(PluginConstant.SERVICE_ID).toString().toLowerCase(); String serviceId = instance.get(PluginConstant.SERVICE_ID).toString().toLowerCase();
String version = ((Map<String, String>) instance.get(PluginConstant.METADATA)).get(PluginConstant.VERSION); String version = ((Map<String, String>) instance.get(PluginConstant.METADATA)).get(PluginConstant.VERSION);
String host = instance.get(PluginConstant.HOST).toString(); String host = instance.get(PluginConstant.HOST).toString();
Integer port = (Integer) instance.get(PluginConstant.PORT); Integer port = (Integer) instance.get(PluginConstant.PORT);
RouteEntity routeEntity = new RouteEntity(); RouterEntity routerEntity = new RouterEntity();
routeEntity.setServiceId(serviceId); routerEntity.setServiceId(serviceId);
routeEntity.setVersion(version); routerEntity.setVersion(version);
routeEntity.setHost(host); routerEntity.setHost(host);
routeEntity.setPort(port); routerEntity.setPort(port);
routeEntityList.add(routeEntity); routerEntityList.add(routerEntity);
} }
return routeEntityList; return routerEntityList;
} }
public RouteEntity route(String serviceIds) { public RouterEntity route(String serviceIds) {
if (StringUtils.isEmpty(serviceIds)) { if (StringUtils.isEmpty(serviceIds)) {
throw new PluginException("Service ids is empty"); throw new PluginException("Service ids is empty");
} }
...@@ -175,55 +175,56 @@ public class PluginRouterController { ...@@ -175,55 +175,56 @@ public class PluginRouterController {
throw new PluginException("Service ids must be separated with '" + PluginConstant.SEPARATE + "'", e); throw new PluginException("Service ids must be separated with '" + PluginConstant.SEPARATE + "'", e);
} }
RouteEntity firstRouteEntity = getRouteEntity(); RouterEntity firstRouterEntity = getRouterEntity();
HashMap<String, List<RouteEntity>> routeEntityMap = new HashMap<String, List<RouteEntity>>(); // 路由深度为Key
String previousServiceId = null; HashMap<Integer, List<RouterEntity>> routerEntityMap = new HashMap<Integer, List<RouterEntity>>();
int routerDepth = 0;
for (String serviceId : serviceIdArray) { for (String serviceId : serviceIdArray) {
serviceId = serviceId.toLowerCase().trim(); serviceId = serviceId.toLowerCase().trim();
if (previousServiceId == null) { if (routerDepth == 0) {
routeFirst(firstRouteEntity, serviceId); routeFirst(firstRouterEntity, serviceId);
retrieveRouteEntityList(routeEntityMap, serviceId).addAll(firstRouteEntity.getNexts()); retrieveRouterEntityList(routerEntityMap, routerDepth).addAll(firstRouterEntity.getNexts());
} else { } else {
List<RouteEntity> routeEntityList = retrieveRouteEntityList(routeEntityMap, previousServiceId); List<RouterEntity> routerEntityList = retrieveRouterEntityList(routerEntityMap, routerDepth - 1);
for (RouteEntity routeEntity : routeEntityList) { for (RouterEntity routerEntity : routerEntityList) {
String routeHost = routeEntity.getHost(); String routeHost = routerEntity.getHost();
int routePort = routeEntity.getPort(); int routePort = routerEntity.getPort();
route(routeEntity, serviceId, routeHost, routePort); route(routerEntity, serviceId, routeHost, routePort);
retrieveRouteEntityList(routeEntityMap, serviceId).addAll(routeEntity.getNexts()); retrieveRouterEntityList(routerEntityMap, routerDepth).addAll(routerEntity.getNexts());
} }
} }
previousServiceId = serviceId; routerDepth++;
} }
return firstRouteEntity; return firstRouterEntity;
} }
private void routeFirst(RouteEntity routeEntity, String routeServiceId) { private void routeFirst(RouterEntity routerEntity, String routeServiceId) {
List<RouteEntity> routeEntityList = getRouteEntityList(routeServiceId); List<RouterEntity> routerEntityList = getRouterEntityList(routeServiceId);
if (CollectionUtils.isNotEmpty(routeEntityList)) { if (CollectionUtils.isNotEmpty(routerEntityList)) {
routeEntity.getNexts().addAll(routeEntityList); routerEntity.getNexts().addAll(routerEntityList);
} }
} }
private void route(RouteEntity routeEntity, String routeServiceId, String routeHost, int routePort) { private void route(RouterEntity routerEntity, String routeServiceId, String routeHost, int routePort) {
List<RouteEntity> routeEntityList = getRouteEntityList(routeServiceId, routeHost, routePort); List<RouterEntity> routerEntityList = getRouterEntityList(routeServiceId, routeHost, routePort);
if (CollectionUtils.isNotEmpty(routeEntityList)) { if (CollectionUtils.isNotEmpty(routerEntityList)) {
routeEntity.getNexts().addAll(routeEntityList); routerEntity.getNexts().addAll(routerEntityList);
} }
} }
private List<RouteEntity> retrieveRouteEntityList(HashMap<String, List<RouteEntity>> routeEntityMap, String serviceId) { private List<RouterEntity> retrieveRouterEntityList(HashMap<Integer, List<RouterEntity>> routerEntityMap, int routerDepth) {
List<RouteEntity> routeEntityList = routeEntityMap.get(serviceId); List<RouterEntity> routerEntityList = routerEntityMap.get(routerDepth);
if (routeEntityList == null) { if (routerEntityList == null) {
routeEntityList = new ArrayList<RouteEntity>(); routerEntityList = new ArrayList<RouterEntity>();
routeEntityMap.put(serviceId, routeEntityList); routerEntityMap.put(routerDepth, routerEntityList);
} }
return routeEntityList; return routerEntityList;
} }
} }
\ No newline at end of file
...@@ -24,5 +24,10 @@ ...@@ -24,5 +24,10 @@
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-admin-center</artifactId> <artifactId>discovery-plugin-admin-center</artifactId>
</dependency> </dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-router-center</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -4,4 +4,5 @@ com.nepxion.discovery.plugin.framework.context.PluginApplicationContextInitializ ...@@ -4,4 +4,5 @@ com.nepxion.discovery.plugin.framework.context.PluginApplicationContextInitializ
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.nepxion.discovery.plugin.framework.configuration.PluginAutoConfiguration,\ com.nepxion.discovery.plugin.framework.configuration.PluginAutoConfiguration,\
com.nepxion.discovery.plugin.configcenter.configuration.ConfigAutoConfiguration,\ com.nepxion.discovery.plugin.configcenter.configuration.ConfigAutoConfiguration,\
com.nepxion.discovery.plugin.admincenter.configuration.AdminAutoConfiguration com.nepxion.discovery.plugin.admincenter.configuration.AdminAutoConfiguration,\
\ No newline at end of file com.nepxion.discovery.plugin.routercenter.configuration.RouterAutoConfiguration
\ No newline at end of file
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
<module>discovery-plugin-framework</module> <module>discovery-plugin-framework</module>
<module>discovery-plugin-config-center</module> <module>discovery-plugin-config-center</module>
<module>discovery-plugin-admin-center</module> <module>discovery-plugin-admin-center</module>
<module>discovery-plugin-router-center</module>
<module>discovery-plugin-starter</module> <module>discovery-plugin-starter</module>
<module>discovery-springcloud-example-a</module> <module>discovery-springcloud-example-a</module>
<module>discovery-springcloud-example-b</module> <module>discovery-springcloud-example-b</module>
...@@ -57,6 +58,12 @@ ...@@ -57,6 +58,12 @@
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-router-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>discovery-plugin-starter</artifactId> <artifactId>discovery-plugin-starter</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
......
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