From f6857f21b8a1a59611ecde432e4e56ba7a5be437 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 19 Mar 2019 21:09:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-admin/sop-admin-server/pom.xml | 29 ++++++++++++---- .../api/demo/param/GoodsParam.java | 3 +- .../{RouteListApi.java => RouteApi.java} | 33 ++++++++++++++++--- .../adminserver/api/service/ServiceApi.java | 3 +- ...{UpdateRouteParam.java => RouteParam.java} | 2 +- .../adminserver/bean/ZookeeperContext.java | 30 ++++++++++++++++- .../sop/adminserver/config/WebConfig.java | 10 ++---- .../src/main/resources/application-dev.yml | 3 ++ sop-gateway-common/pom.xml | 1 + 9 files changed, 90 insertions(+), 24 deletions(-) rename sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/{RouteListApi.java => RouteApi.java} (64%) rename sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/{UpdateRouteParam.java => RouteParam.java} (97%) diff --git a/sop-admin/sop-admin-server/pom.xml b/sop-admin/sop-admin-server/pom.xml index 906848fc..4c8566b0 100644 --- a/sop-admin/sop-admin-server/pom.xml +++ b/sop-admin/sop-admin-server/pom.xml @@ -21,6 +21,8 @@ UTF-8 UTF-8 1.8 + 3.4.12 + 4.0.1 @@ -36,16 +38,31 @@ easyopen-spring-boot-starter 1.16.0 - + org.apache.curator - curator-framework - 2.13.0 + curator-recipes + ${curator-recipes.version} + + + org.apache.zookeeper + zookeeper + + - org.apache.curator - curator-recipes - 2.13.0 + org.apache.zookeeper + zookeeper + ${zookeeper.version} + + + org.slf4j + slf4j-log4j12 + + diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/demo/param/GoodsParam.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/demo/param/GoodsParam.java index 13798f4c..d5f099e4 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/demo/param/GoodsParam.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/demo/param/GoodsParam.java @@ -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 { diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteListApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java similarity index 64% rename from sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteListApi.java rename to sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java index 2a5321fd..d763792f 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteListApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java @@ -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)); } } diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java index 2598bc86..c1f2cfea 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/ServiceApi.java @@ -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 childDataList = ZookeeperContext.getChildrenData(routeRootPath); List 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); diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/UpdateRouteParam.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/RouteParam.java similarity index 97% rename from sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/UpdateRouteParam.java rename to sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/RouteParam.java index f70bf6bb..73e95d83 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/UpdateRouteParam.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/param/RouteParam.java @@ -10,7 +10,7 @@ import javax.validation.constraints.NotNull; * @author tanghc */ @Data -public class UpdateRouteParam { +public class RouteParam { @NotBlank(message = "profile不能为空") @ApiDocField(description = "profile") diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ZookeeperContext.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ZookeeperContext.java index 0898f5d4..625f25ae 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ZookeeperContext.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/bean/ZookeeperContext.java @@ -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 diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java index e051661b..e96f7e79 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java @@ -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 { diff --git a/sop-admin/sop-admin-server/src/main/resources/application-dev.yml b/sop-admin/sop-admin-server/src/main/resources/application-dev.yml index 9afe91d4..69dd6fe9 100644 --- a/sop-admin/sop-admin-server/src/main/resources/application-dev.yml +++ b/sop-admin/sop-admin-server/src/main/resources/application-dev.yml @@ -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 diff --git a/sop-gateway-common/pom.xml b/sop-gateway-common/pom.xml index 6a4d2c6f..b861d3ae 100644 --- a/sop-gateway-common/pom.xml +++ b/sop-gateway-common/pom.xml @@ -85,6 +85,7 @@ org.springframework.cloud spring-cloud-starter-zookeeper-discovery + org.apache.zookeeper zookeeper