路由添加

This commit is contained in:
tanghc
2019-03-19 21:09:09 +08:00
parent eeeb0525c6
commit f6857f21b8
9 changed files with 91 additions and 25 deletions

View File

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

View File

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

View File

@@ -5,16 +5,19 @@ import com.gitee.easyopen.annotation.Api;
import com.gitee.easyopen.annotation.ApiService;
import com.gitee.easyopen.doc.annotation.ApiDoc;
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.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.SopAdminConstants;
import com.gitee.sop.adminserver.bean.ZookeeperContext;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.springframework.beans.BeanUtils;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@@ -23,7 +26,7 @@ import java.util.stream.Collectors;
*/
@ApiService
@ApiDoc("服务管理")
public class RouteListApi {
public class RouteApi {
@Api(name = "route.list")
@ApiDocMethod(description = "路由列表", elementClass = GatewayRouteDefinition.class)
@@ -58,13 +61,33 @@ public class RouteListApi {
@Api(name = "route.update")
@ApiDocMethod(description = "修改路由")
void updateRoute(UpdateRouteParam param) throws Exception {
void updateRoute(RouteParam param) throws Exception {
String serviceIdPath = ZookeeperContext.getSopRouteRootPath(param.getProfile()) + "/" + param.getServiceId();
String zookeeperRoutePath = serviceIdPath + "/" + param.getId();
String data = ZookeeperContext.getData(zookeeperRoutePath);
GatewayRouteDefinition routeDefinition = JSON.parseObject(data, GatewayRouteDefinition.class);
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));
}
}

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
package com.gitee.sop.adminserver.bean;
import com.gitee.easyopen.exception.ApiException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
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());
}
/**
* 创建新的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 {
if (!isPathExist(path)) {
return null;
@@ -83,6 +110,7 @@ public class ZookeeperContext {
/**
* 获取子节点数据
*
* @param parentPath 父节点
* @return
* @throws Exception

View File

@@ -1,23 +1,17 @@
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.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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
*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
public class WebConfig extends WebMvcConfigurationSupport {

View File

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

View File

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