mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.1.0
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
- 支持分布式限流(redis实现)
|
||||
- 可调整JSR-303校验顺序
|
||||
- 优化springmvc注册到nacos
|
||||
|
||||
## 2.0.0
|
||||
|
||||
|
@@ -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);
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
|
||||
}
|
@@ -14,6 +14,11 @@
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- SOP监听 -->
|
||||
<listener>
|
||||
<listener-class>com.gitee.app.config.OpenServletContextListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Processes application requests -->
|
||||
<servlet>
|
||||
<servlet-name>appServlet</servlet-name>
|
||||
|
Reference in New Issue
Block a user