提交 ab91e362 编写于 作者: zhouweidong's avatar zhouweidong

项目支持打war包

上级 bf7844c4
...@@ -51,11 +51,13 @@ public class NacosRegisterConfig implements ApplicationRunner { ...@@ -51,11 +51,13 @@ public class NacosRegisterConfig implements ApplicationRunner {
if (registration != null && port != null) { if (registration != null && port != null) {
try { try {
String containerPort = getContainerPort(); String containerPort = getContainerPort();
if(StringUtils.isEmpty(containerPort)){ if(!StringUtils.isEmpty(containerPort)){
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略"); registration.setPort(new Integer(containerPort));
return; }
else{
registration.setPort(port);
log.info("无法获取外部容器端口,将使用程序默认端口{}",port);
} }
registration.setPort(new Integer(containerPort));
registration.start(); registration.start();
} catch (Exception e) { } catch (Exception e) {
throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"",""); throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"","");
...@@ -63,21 +65,26 @@ public class NacosRegisterConfig implements ApplicationRunner { ...@@ -63,21 +65,26 @@ public class NacosRegisterConfig implements ApplicationRunner {
log.info("已将应用程序成功注册到nacos"); log.info("已将应用程序成功注册到nacos");
} }
else{ else{
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略"); log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略");
} }
} }
/** /**
* 获取外部容器端口 * 获取外部容器端口
*/ */
public String getContainerPort() throws Exception { public String getContainerPort(){
String port = null; String port = null;
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); try {
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))); MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
if(!ObjectUtils.isEmpty(objectNames)){ Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
port = objectNames.iterator().next().getKeyProperty("port"); if(!ObjectUtils.isEmpty(objectNames)){
port = objectNames.iterator().next().getKeyProperty("port");
}
}
catch (Exception e) {
log.error("获取外部容器端口失败!"+e);
} }
return port; return port;
} }
} }
</#if> </#if>
\ No newline at end of file
...@@ -60,11 +60,13 @@ public class NacosRegisterConfig implements ApplicationRunner { ...@@ -60,11 +60,13 @@ public class NacosRegisterConfig implements ApplicationRunner {
if (registration != null && port != null) { if (registration != null && port != null) {
try { try {
String containerPort = getContainerPort(); String containerPort = getContainerPort();
if(StringUtils.isEmpty(containerPort)){ if(!StringUtils.isEmpty(containerPort)){
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略"); registration.setPort(new Integer(containerPort));
return; }
else{
registration.setPort(port);
log.info("无法获取外部容器端口,将使用程序默认端口{}",port);
} }
registration.setPort(new Integer(containerPort));
registration.start(); registration.start();
} catch (Exception e) { } catch (Exception e) {
throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"",""); throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"","");
...@@ -72,19 +74,24 @@ public class NacosRegisterConfig implements ApplicationRunner { ...@@ -72,19 +74,24 @@ public class NacosRegisterConfig implements ApplicationRunner {
log.info("已将应用程序成功注册到nacos"); log.info("已将应用程序成功注册到nacos");
} }
else{ else{
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略"); log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略");
} }
} }
/** /**
* 获取外部容器端口 * 获取外部容器端口
*/ */
public String getContainerPort() throws Exception { public String getContainerPort(){
String port = null; String port = null;
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); try {
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))); MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
if(!ObjectUtils.isEmpty(objectNames)){ Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
port = objectNames.iterator().next().getKeyProperty("port"); if(!ObjectUtils.isEmpty(objectNames)){
port = objectNames.iterator().next().getKeyProperty("port");
}
}
catch (Exception e) {
log.error("获取外部容器端口失败!"+e);
} }
return port; return port;
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册