From 99fd427eaad064f46f04438f6ace522cbf06dc9c Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 12 Sep 2019 15:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dspringmvc=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/ServiceRoutesLoader.java | 17 ++++++++++----- .../manager/ServiceRouteInfoBuilder.java | 10 +++++++-- .../gitee/app/config/OpenServiceConfig.java | 21 ++++++++++++++++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java index 0e6beec1..87e50b8a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java @@ -34,6 +34,8 @@ import java.util.Objects; @Slf4j public class ServiceRoutesLoader { + private static final String SERVER_CONTEXT_PATH = "server.servlet.context-path"; + private static final String SECRET = "a3d9sf!1@odl90zd>fkASwq"; private static final int FIVE_SECONDS = 1000 * 5; @@ -95,35 +97,40 @@ public class ServiceRoutesLoader { List allInstances = namingService.getAllInstances(serviceName); if (CollectionUtils.isEmpty(allInstances)) { // 如果没有服务列表,则删除所有路由信息 + log.info("服务下线,删除路由配置,serviceId: {}", serviceName); baseRouteCache.remove(serviceName); configService.removeConfig(dataId, groupId); } else { for (Instance instance : allInstances) { String url = getRouteRequestUrl(instance); + log.info("拉取路由配置,serviceId: {}, url: {}", serviceName, url); ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); if (responseEntity.getStatusCode() == HttpStatus.OK) { String body = responseEntity.getBody(); ServiceRouteInfo serviceRouteInfo = JSON.parseObject(body, ServiceRouteInfo.class); baseRouteCache.load(serviceRouteInfo, callback -> { try { - log.info("推送路由配置到nacos,dataId={}, groupId={}",dataId, groupId); + log.info("推送路由配置到nacos,dataId: {}, groupId: {}", dataId, groupId); configService.publishConfig(dataId, groupId, body); } catch (NacosException e) { - log.error("nacos推送失败,serviceId:{}, instance:{}",serviceName, instance); + log.error("nacos推送失败,serviceId: {}, instance: {}", serviceName, instance); } }); + } else { + log.error("拉取路由配置异常,url: {}, status: {}, body: {}", url, responseEntity.getStatusCodeValue(), responseEntity.getBody()); } } } - } catch (NacosException e) { - log.error("选择服务实例失败,serviceName:{}", serviceName, e); + } catch (Exception e) { + log.error("选择服务实例失败,serviceName: {}", serviceName, e); } } } private static String getRouteRequestUrl(Instance instance) { String query = buildQuery(SECRET); - return "http://" + instance.getIp() + ":" + instance.getPort() + "/sop/routes" + query; + String contextPath = instance.getMetadata().getOrDefault(SERVER_CONTEXT_PATH, ""); + return "http://" + instance.getIp() + ":" + instance.getPort() + contextPath + "/sop/routes" + query; } private static String buildQuery(String secret) { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java index 13784204..27d1b453 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java @@ -118,8 +118,14 @@ public class ServiceRouteInfoBuilder { if (servletPath == null) { servletPath = ""; } - servletPath = StringUtils.trimLeadingCharacter(servletPath, '/'); - return contextPath + servletPath; + if (!servletPath.startsWith(PATH_SPLIT)) { + servletPath = PATH_SPLIT + servletPath; + } + if (DEFAULT_CONTEXT_PATH.equals(contextPath)) { + return servletPath; + } else { + return contextPath + servletPath; + } } private void checkPath(String path, String errorMsg) { diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java index d8c70906..4056236a 100644 --- a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java @@ -4,6 +4,7 @@ import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.annotation.NacosProperties; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.utils.NetUtils; import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery; import com.gitee.sop.servercommon.bean.ServiceConfig; @@ -21,6 +22,7 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration { public static final String SPRING_APPLICATION_NAME = "spring.application.name"; + public static final String SERVER_CONTEXT_PATH = "server.servlet.context-path"; public static final String SERVER_IP = "server.ip"; public static final String SERVER_PORT = "server.port"; @@ -28,7 +30,8 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration { ServiceConfig.getInstance().setDefaultVersion("1.0"); } - // 这两个参数需要从配置文件中获取 + /** 对应tomcat中的contextPath */ + private String contextPath = "/sop-springmvc"; private String serviceId = "sop-springmvc"; private int port = 2223; @@ -39,11 +42,14 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration { protected void doAfter() { super.doAfter(); try { - System.setProperty(SPRING_APPLICATION_NAME, serviceId); String ip = NetUtils.localIP(); - namingService.registerInstance(serviceId, ip, port); + System.setProperty(SPRING_APPLICATION_NAME, serviceId); System.setProperty(SERVER_IP, ip); System.setProperty(SERVER_PORT, String.valueOf(port)); + System.setProperty(SERVER_CONTEXT_PATH, contextPath); + + Instance instance = this.getInstance(serviceId, ip, port, contextPath); + namingService.registerInstance(serviceId, instance); log.info("注册到nacos, serviceId:{}, ip:{}, port:{}", serviceId, ip, port); } catch (NacosException e) { log.error("注册nacos失败", e); @@ -51,4 +57,13 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration { } } + private Instance getInstance(String serviceId, String ip, int port, String contextPath) { + Instance instance = new Instance(); + instance.setServiceName(serviceId); + instance.setIp(ip); + instance.setPort(port); + instance.getMetadata().put(SERVER_CONTEXT_PATH, contextPath); + return instance; + } + }