pull/1/head
tanghc 5 years ago
parent d32d3931a1
commit cb03f5a554
  1. 1
      changelog.md
  2. 3
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteController.java
  3. 2
      sop-example/sop-springmvc/src/main/java/com/gitee/app/HomeController.java
  4. 10
      sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java
  5. 41
      sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServletContextListener.java
  6. 5
      sop-example/sop-springmvc/src/main/webapp/WEB-INF/web.xml

@ -4,6 +4,7 @@
- 支持分布式限流(redis实现) - 支持分布式限流(redis实现)
- 可调整JSR-303校验顺序 - 可调整JSR-303校验顺序
- 优化springmvc注册到nacos
## 2.0.0 ## 2.0.0

@ -46,6 +46,9 @@ public class ServiceRouteController {
protected ServiceRouteInfo getServiceRouteInfo(HttpServletRequest request, HttpServletResponse response) { protected ServiceRouteInfo getServiceRouteInfo(HttpServletRequest request, HttpServletResponse response) {
String serviceId = environment.getProperty("spring.application.name"); String serviceId = environment.getProperty("spring.application.name");
if (serviceId == null) {
throw new IllegalArgumentException("未指定spring.application.name参数");
}
ApiMetaBuilder apiMetaBuilder = getApiMetaBuilder(); ApiMetaBuilder apiMetaBuilder = getApiMetaBuilder();
ServiceApiInfo serviceApiInfo = apiMetaBuilder.getServiceApiInfo(serviceId, requestMappingHandlerMapping); ServiceApiInfo serviceApiInfo = apiMetaBuilder.getServiceApiInfo(serviceId, requestMappingHandlerMapping);
ServiceRouteInfoBuilder serviceRouteInfoBuilder = new ServiceRouteInfoBuilder(environment); ServiceRouteInfoBuilder serviceRouteInfoBuilder = new ServiceRouteInfoBuilder(environment);

@ -50,5 +50,5 @@ public class HomeController {
goods.setPrice(new BigDecimal(5000)); goods.setPrice(new BigDecimal(5000));
return goods; return goods;
} }
} }

@ -18,6 +18,12 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) @EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
public class OpenServiceConfig extends SpringMvcServiceConfiguration { 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 { static {
ServiceConfig.getInstance().setDefaultVersion("1.0"); ServiceConfig.getInstance().setDefaultVersion("1.0");
} }
@ -33,12 +39,16 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration {
protected void doAfter() { protected void doAfter() {
super.doAfter(); super.doAfter();
try { try {
System.setProperty(SPRING_APPLICATION_NAME, serviceId);
String ip = NetUtils.localIP(); String ip = NetUtils.localIP();
namingService.registerInstance(serviceId, ip, port); 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); log.info("注册到nacos, serviceId:{}, ip:{}, port:{}", serviceId, ip, port);
} catch (NacosException e) { } catch (NacosException e) {
log.error("注册nacos失败", e); log.error("注册nacos失败", e);
throw new RuntimeException("注册nacos失败", e); throw new RuntimeException("注册nacos失败", e);
} }
} }
} }

@ -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());
}
}

@ -13,6 +13,11 @@
<listener> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </listener>
<!-- SOP监听 -->
<listener>
<listener-class>com.gitee.app.config.OpenServletContextListener</listener-class>
</listener>
<!-- Processes application requests --> <!-- Processes application requests -->
<servlet> <servlet>

Loading…
Cancel
Save