diff --git a/changelog.md b/changelog.md index 82aef8b8..0bd2bfda 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ - 支持分布式限流(redis实现) - 可调整JSR-303校验顺序 +- 优化springmvc注册到nacos ## 2.0.0 diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteController.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteController.java index 50ddca39..8074d2f1 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteController.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteController.java @@ -46,6 +46,9 @@ public class ServiceRouteController { protected ServiceRouteInfo getServiceRouteInfo(HttpServletRequest request, HttpServletResponse response) { String serviceId = environment.getProperty("spring.application.name"); + if (serviceId == null) { + throw new IllegalArgumentException("未指定spring.application.name参数"); + } ApiMetaBuilder apiMetaBuilder = getApiMetaBuilder(); ServiceApiInfo serviceApiInfo = apiMetaBuilder.getServiceApiInfo(serviceId, requestMappingHandlerMapping); ServiceRouteInfoBuilder serviceRouteInfoBuilder = new ServiceRouteInfoBuilder(environment); diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java index 4253fcaa..04a5d638 100644 --- a/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java @@ -50,5 +50,5 @@ public class HomeController { goods.setPrice(new BigDecimal(5000)); return goods; } - + } 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 1167125b..d8c70906 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 @@ -18,6 +18,12 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) public class OpenServiceConfig extends SpringMvcServiceConfiguration { + + + public static final String SPRING_APPLICATION_NAME = "spring.application.name"; + public static final String SERVER_IP = "server.ip"; + public static final String SERVER_PORT = "server.port"; + static { ServiceConfig.getInstance().setDefaultVersion("1.0"); } @@ -33,12 +39,16 @@ 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(SERVER_IP, ip); + System.setProperty(SERVER_PORT, String.valueOf(port)); log.info("注册到nacos, serviceId:{}, ip:{}, port:{}", serviceId, ip, port); } catch (NacosException e) { log.error("注册nacos失败", e); throw new RuntimeException("注册nacos失败", e); } } + } diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServletContextListener.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServletContextListener.java new file mode 100644 index 00000000..fc99a94f --- /dev/null +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServletContextListener.java @@ -0,0 +1,41 @@ +package com.gitee.app.config; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * 容器销毁注销nacos,配置见web.xml + */ +@Slf4j +public class OpenServletContextListener implements ServletContextListener { + + private static WebApplicationContext webApplicationContext; + + @Override + public void contextDestroyed(ServletContextEvent sce) { + String serviceId = System.getProperty(OpenServiceConfig.SPRING_APPLICATION_NAME); + String ip = System.getProperty(OpenServiceConfig.SERVER_IP); + String port = System.getProperty(OpenServiceConfig.SERVER_PORT); + + log.info("注销nacos,serviceId:{}, ip:{}, port:{}", serviceId, ip, port); + + NamingService namingService = webApplicationContext.getBean(NamingService.class); + try { + namingService.deregisterInstance(serviceId, ip, Integer.valueOf(port)); + } catch (NacosException e) { + log.error("注销nacos服务失败,serviceId:{}, ip:{}, port:{}", serviceId, ip, port); + } + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); + } + +} \ No newline at end of file diff --git a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml index de2d58b7..b7dfa271 100644 --- a/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml +++ b/sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml @@ -13,6 +13,11 @@ org.springframework.web.context.ContextLoaderListener + + + + com.gitee.app.config.OpenServletContextListener +