提交 6ea4cfc6 编写于 作者: yanshaowei's avatar yanshaowei

Merge branch 'dev' of http://demo.ibizlab.cn/ibizr7sfstdtempl/ibiz4jr7 into dev

<#ibiztemplate>
TARGET=PSSYSAPP
</#ibiztemplate>
<#assign packageType="">
<#comment>前端应用微服务平台配置</#comment>
<#if sys.getAllPSDevSlnMSDepApps()??>
<#list sys.getAllPSDevSlnMSDepApps() as depApp>
<#if depApp.getUserParam("ibiz.deploy.packagetype","")?? && depApp.getUserParam("ibiz.deploy.packagetype","")!=''>
<#assign packageType = depApp.getUserParam("ibiz.deploy.packagetype","")>
<#break>
</#if>
<#break>
</#list>
</#if>
<#if packageType=='war'>
package ${pub.getPKGCodeName()}.${app.getPKGCodeName()?lower_case}.config;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import ${pub.getPKGCodeName()}.util.errors.BadRequestAlertException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.Set;
/**
* 外部容器启动服务时,自动注册服务到nacos
*/
@Component
@Slf4j
public class NacosRegisterConfig implements ApplicationRunner {
@Autowired(required = false)
private NacosAutoServiceRegistration registration;
@Value("${r'${server.port:8080}'}")
Integer port;
@Override
public void run(ApplicationArguments args) {
log.info("正在尝试将应用程序注册到nacos");
if (registration != null && port != null) {
try {
String containerPort = getContainerPort();
if(!StringUtils.isEmpty(containerPort)){
registration.setPort(new Integer(containerPort));
}
else{
registration.setPort(port);
log.info("无法获取外部容器端口,将使用程序默认端口{}",port);
}
registration.start();
} catch (Exception e) {
throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"","");
}
log.info("已将应用程序成功注册到nacos");
}
else{
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略。");
}
}
/**
* 获取外部容器端口
*/
public String getContainerPort(){
String port = null;
try {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
if(!ObjectUtils.isEmpty(objectNames)){
port = objectNames.iterator().next().getKeyProperty("port");
}
}
catch (Exception e) {
log.error("获取外部容器端口失败!"+e);
}
return port;
}
}
</#if>
\ No newline at end of file
......@@ -5,32 +5,27 @@ TARGET=PSSYSTEM
<#comment>前端应用微服务平台配置</#comment>
<#if sys.getAllPSDevSlnMSDepApps()??>
<#list sys.getAllPSDevSlnMSDepApps() as depApp>
<#if depApp.getPSDCMSPlatform()??>
<#assign appPlatform=depApp.getPSDCMSPlatform()>
<#if appPlatform.getUserParam("ibiz.deploy.packagetype","")??>
<#assign packageType = appPlatform.getUserParam("ibiz.deploy.packagetype","")>
<#if depApp.getUserParam("ibiz.deploy.packagetype","")?? && depApp.getUserParam("ibiz.deploy.packagetype","")!=''>
<#assign packageType = depApp.getUserParam("ibiz.deploy.packagetype","")>
<#break>
</#if>
<#break>
</#if>
</#list>
</#if>
<#comment>服务接口微服务平台配置</#comment>
<#if sys.getAllPSDevSlnMSDepAPIs()??>
<#list sys.getAllPSDevSlnMSDepAPIs() as depSysApi>
<#if depSysApi.getPSDCMSPlatform()?? >
<#assign sysApiPlatform=depSysApi.getPSDCMSPlatform()>
<#if sysApiPlatform.getUserParam("ibiz.deploy.packagetype","")??>
<#assign packageType = sysApiPlatform.getUserParam("ibiz.deploy.packagetype","")>
<#if depSysApi.getUserParam("ibiz.deploy.packagetype","")?? && depSysApi.getUserParam("ibiz.deploy.packagetype","")!=''>
<#assign packageType = depSysApi.getUserParam("ibiz.deploy.packagetype","")>
<#break>
</#if>
<#break>
</#if>
</#list>
</#if>
<#if packageType=='war'>
package ${pub.getPKGCodeName()}.core.util.config;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.sa.ajgl.util.errors.BadRequestAlertException;
import ${pub.getPKGCodeName()}.util.errors.BadRequestAlertException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -61,29 +56,42 @@ public class NacosRegisterConfig implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
log.info("正在尝试将应用程序注册到nacos");
if (registration != null && port != null) {
try {
String containerPort = getContainerPort();
if(!StringUtils.isEmpty(containerPort)){
log.info("正在尝试将应用程序注册到nacos,端口号为:{}",containerPort);
registration.setPort(new Integer(containerPort));
registration.start();
}
else{
registration.setPort(port);
log.info("无法获取外部容器端口,将使用程序默认端口{}",port);
}
registration.start();
} catch (Exception e) {
throw new BadRequestAlertException("应用程序注册到nacos失败,"+e,"","");
}
log.info("已将应用程序成功注册到nacos");
}
else{
log.info("无法获取应用程序端口,将应用程序注册到nacos请求被忽略。");
}
}
/**
* 获取外部容器端口
*/
public String getContainerPort() throws Exception {
public String getContainerPort(){
String port = null;
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
if(!ObjectUtils.isEmpty(objectNames)){
port = objectNames.iterator().next().getKeyProperty("port");
try {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
if(!ObjectUtils.isEmpty(objectNames)){
port = objectNames.iterator().next().getKeyProperty("port");
}
}
catch (Exception e) {
log.error("获取外部容器端口失败!"+e);
}
return port;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册