Commit bb04aed3 by Nepxion

增加路由中心

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