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:
@@ -50,5 +50,5 @@ public class HomeController {
|
||||
goods.setPrice(new BigDecimal(5000));
|
||||
return goods;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
|
||||
}
|
@@ -13,6 +13,11 @@
|
||||
<listener>
|
||||
<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>
|
||||
|
Reference in New Issue
Block a user