路由添加

1.x
tanghc 6 years ago
parent eeeb0525c6
commit f6857f21b8
  1. 29
      sop-admin/sop-admin-server/pom.xml
  2. 3
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/demo/param/GoodsParam.java
  3. 33
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java
  4. 3
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java
  5. 2
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/RouteParam.java
  6. 30
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ZookeeperContext.java
  7. 10
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java
  8. 3
      sop-admin/sop-admin-server/src/main/resources/application-dev.yml
  9. 1
      sop-gateway-common/pom.xml

@ -21,6 +21,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<zookeeper.version>3.4.12</zookeeper.version>
<curator-recipes.version>4.0.1</curator-recipes.version>
</properties> </properties>
<dependencies> <dependencies>
@ -36,16 +38,31 @@
<artifactId>easyopen-spring-boot-starter</artifactId> <artifactId>easyopen-spring-boot-starter</artifactId>
<version>1.16.0</version> <version>1.16.0</version>
</dependency> </dependency>
<!-- zookeeper客户端 --> <!-- zookeeper客户端针对zookeeper-3.4.x
如果zookeeper使用3.5.x,可以直接使用curator-recipes最高版本
详情:http://curator.apache.org/zk-compatibility.html
-->
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId> <artifactId>curator-recipes</artifactId>
<version>2.13.0</version> <version>${curator-recipes.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.zookeeper</groupId>
<artifactId>curator-recipes</artifactId> <artifactId>zookeeper</artifactId>
<version>2.13.0</version> <version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>

@ -2,7 +2,8 @@ package com.gitee.sop.adminserver.api.demo.param;
import com.gitee.easyopen.doc.annotation.ApiDocField; import com.gitee.easyopen.doc.annotation.ApiDocField;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotEmpty;
public class GoodsParam { public class GoodsParam {

@ -5,16 +5,19 @@ import com.gitee.easyopen.annotation.Api;
import com.gitee.easyopen.annotation.ApiService; import com.gitee.easyopen.annotation.ApiService;
import com.gitee.easyopen.doc.annotation.ApiDoc; import com.gitee.easyopen.doc.annotation.ApiDoc;
import com.gitee.easyopen.doc.annotation.ApiDocMethod; import com.gitee.easyopen.doc.annotation.ApiDocMethod;
import com.gitee.easyopen.exception.ApiException;
import com.gitee.sop.adminserver.api.service.param.RouteSearchParam; import com.gitee.sop.adminserver.api.service.param.RouteSearchParam;
import com.gitee.sop.adminserver.api.service.param.UpdateRouteParam; import com.gitee.sop.adminserver.api.service.param.RouteParam;
import com.gitee.sop.adminserver.api.service.result.ServiceInfo;
import com.gitee.sop.adminserver.bean.GatewayRouteDefinition; import com.gitee.sop.adminserver.bean.GatewayRouteDefinition;
import com.gitee.sop.adminserver.bean.SopAdminConstants;
import com.gitee.sop.adminserver.bean.ZookeeperContext; import com.gitee.sop.adminserver.bean.ZookeeperContext;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.ChildData;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -23,7 +26,7 @@ import java.util.stream.Collectors;
*/ */
@ApiService @ApiService
@ApiDoc("服务管理") @ApiDoc("服务管理")
public class RouteListApi { public class RouteApi {
@Api(name = "route.list") @Api(name = "route.list")
@ApiDocMethod(description = "路由列表", elementClass = GatewayRouteDefinition.class) @ApiDocMethod(description = "路由列表", elementClass = GatewayRouteDefinition.class)
@ -58,13 +61,33 @@ public class RouteListApi {
@Api(name = "route.update") @Api(name = "route.update")
@ApiDocMethod(description = "修改路由") @ApiDocMethod(description = "修改路由")
void updateRoute(UpdateRouteParam param) throws Exception { void updateRoute(RouteParam param) throws Exception {
String serviceIdPath = ZookeeperContext.getSopRouteRootPath(param.getProfile()) + "/" + param.getServiceId(); String serviceIdPath = ZookeeperContext.getSopRouteRootPath(param.getProfile()) + "/" + param.getServiceId();
String zookeeperRoutePath = serviceIdPath + "/" + param.getId(); String zookeeperRoutePath = serviceIdPath + "/" + param.getId();
String data = ZookeeperContext.getData(zookeeperRoutePath); String data = ZookeeperContext.getData(zookeeperRoutePath);
GatewayRouteDefinition routeDefinition = JSON.parseObject(data, GatewayRouteDefinition.class); GatewayRouteDefinition routeDefinition = JSON.parseObject(data, GatewayRouteDefinition.class);
BeanUtils.copyProperties(param, routeDefinition); BeanUtils.copyProperties(param, routeDefinition);
ZookeeperContext.setData(zookeeperRoutePath, JSON.toJSONString(routeDefinition)); ZookeeperContext.updatePathData(zookeeperRoutePath, JSON.toJSONString(routeDefinition));
}
@Api(name = "route.add")
@ApiDocMethod(description = "新增路由")
void addRoute(RouteParam param) throws Exception {
String serviceIdPath = ZookeeperContext.getSopRouteRootPath(param.getProfile()) + "/" + param.getServiceId();
String zookeeperRoutePath = serviceIdPath + "/" + param.getId();
if (ZookeeperContext.isPathExist(zookeeperRoutePath)) {
throw new ApiException("id已存在");
}
GatewayRouteDefinition routeDefinition = new GatewayRouteDefinition();
BeanUtils.copyProperties(param, routeDefinition);
ZookeeperContext.createNewData(zookeeperRoutePath, JSON.toJSONString(routeDefinition));
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setServiceId(param.getServiceId());
serviceInfo.setDescription(param.getServiceId());
String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
serviceInfo.setCreateTime(now);
serviceInfo.setUpdateTime(now);
ZookeeperContext.updatePathData(serviceIdPath, JSON.toJSONString(serviceInfo));
} }
} }

@ -17,8 +17,6 @@ import org.springframework.core.env.Environment;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.gitee.sop.adminserver.bean.SopAdminConstants.SOP_SERVICE_ROUTE_PATH;
/** /**
* @author tanghc * @author tanghc
*/ */
@ -37,6 +35,7 @@ public class ServiceApi {
String routeRootPath = ZookeeperContext.getSopRouteRootPath(param.getProfile()); String routeRootPath = ZookeeperContext.getSopRouteRootPath(param.getProfile());
List<ChildData> childDataList = ZookeeperContext.getChildrenData(routeRootPath); List<ChildData> childDataList = ZookeeperContext.getChildrenData(routeRootPath);
List<ServiceInfo> serviceInfoList = childDataList.stream() List<ServiceInfo> serviceInfoList = childDataList.stream()
.filter(childData -> childData.getData() != null && childData.getData().length > 0)
.map(childData -> { .map(childData -> {
String serviceNodeData = new String(childData.getData()); String serviceNodeData = new String(childData.getData());
ServiceInfo serviceInfo = JSON.parseObject(serviceNodeData, ServiceInfo.class); ServiceInfo serviceInfo = JSON.parseObject(serviceNodeData, ServiceInfo.class);

@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull;
* @author tanghc * @author tanghc
*/ */
@Data @Data
public class UpdateRouteParam { public class RouteParam {
@NotBlank(message = "profile不能为空") @NotBlank(message = "profile不能为空")
@ApiDocField(description = "profile") @ApiDocField(description = "profile")

@ -1,5 +1,6 @@
package com.gitee.sop.adminserver.bean; package com.gitee.sop.adminserver.bean;
import com.gitee.easyopen.exception.ApiException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
@ -69,10 +70,36 @@ public class ZookeeperContext {
} }
} }
public static Stat setData(String path, String data) throws Exception { /**
* 对已存在的path赋值
*
* @param path 已存在的
* @param data
* @return
* @throws Exception
*/
public static Stat updatePathData(String path, String data) throws Exception {
if (!isPathExist(path)) {
throw new ApiException("path " + path + " 不存在");
}
return getClient().setData().forPath(path, data.getBytes()); return getClient().setData().forPath(path, data.getBytes());
} }
/**
* 创建新的path并赋值
* @param path 待创建的path
* @param data
*/
public static String createNewData(String path, String data) throws Exception {
if (isPathExist(path)) {
throw new ApiException("path " + path + " 已存在");
}
return getClient().create()
// 如果指定节点的父节点不存在,则Curator将会自动级联创建父节点
.creatingParentContainersIfNeeded()
.forPath(path, data.getBytes());
}
public static String getData(String path) throws Exception { public static String getData(String path) throws Exception {
if (!isPathExist(path)) { if (!isPathExist(path)) {
return null; return null;
@ -83,6 +110,7 @@ public class ZookeeperContext {
/** /**
* 获取子节点数据 * 获取子节点数据
*
* @param parentPath 父节点 * @param parentPath 父节点
* @return * @return
* @throws Exception * @throws Exception

@ -1,23 +1,17 @@
package com.gitee.sop.adminserver.config; package com.gitee.sop.adminserver.config;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/** /**
* @author thc * @author thc
*/ */
@Configuration @Configuration
public class WebConfig extends WebMvcConfigurerAdapter { public class WebConfig extends WebMvcConfigurationSupport {

@ -5,17 +5,20 @@ spring:
application: application:
name: sop-admin name: sop-admin
# zookeeper客户端连接
cloud: cloud:
zookeeper: zookeeper:
connect-string: localhost:2181 connect-string: localhost:2181
baseSleepTimeMs: 3000 baseSleepTimeMs: 3000
maxRetries: 3 maxRetries: 3
# 固定不用改
easyopen: easyopen:
show-doc: true show-doc: true
mono: false mono: false
ignore-validate: true ignore-validate: true
# 默认profile列表,新增可以在后面加
sop-admin: sop-admin:
profiles: default,prod,dev,test profiles: default,prod,dev,test

@ -85,6 +85,7 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</exclusion> </exclusion>
<!-- 这里为什么要排除,详见:http://curator.apache.org/zk-compatibility.html -->
<exclusion> <exclusion>
<groupId>org.apache.zookeeper</groupId> <groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId> <artifactId>zookeeper</artifactId>

Loading…
Cancel
Save