|
@@ -0,0 +1,64 @@
|
|
|
+package com.dragoninfo.dcuc.authweb;
|
|
|
+
|
|
|
+import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
|
|
|
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+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 javax.management.MBeanServer;
|
|
|
+import javax.management.ObjectName;
|
|
|
+import javax.management.Query;
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class NacosPortConfig implements ApplicationRunner {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(NacosPortConfig.class);
|
|
|
+ @Autowired
|
|
|
+ private NacosAutoServiceRegistration registration;
|
|
|
+ @Value("${spring.application.name}")
|
|
|
+ private String appName;
|
|
|
+ @Value("${server.port}")
|
|
|
+ private int port;
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
+ int port = this.getTomcatPort();
|
|
|
+ if (registration != null) {
|
|
|
+ registration.setPort(port);
|
|
|
+ registration.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取容器端口号
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws Exception 容器端口号未获取到
|
|
|
+ */
|
|
|
+ public int getTomcatPort() throws Exception {
|
|
|
+ String port = this.getTomcatPortValue();
|
|
|
+ if (!StringUtils.isEmpty(port)) {
|
|
|
+ return Integer.valueOf(port);
|
|
|
+ }
|
|
|
+ return this.port;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取容器端口号(解决使用外部容器部署,nacos无法注册问题)
|
|
|
+ *
|
|
|
+ * @return port
|
|
|
+ */
|
|
|
+ private String getTomcatPortValue() throws Exception {
|
|
|
+ MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
|
|
|
+ Set<ObjectName> objectNames = beanServer.queryNames
|
|
|
+ (new ObjectName("*:type=Connector,*"),
|
|
|
+ Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
|
|
|
+ for (ObjectName objectName : objectNames) {
|
|
|
+ return objectName.getKeyProperty("port");
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|