Explorar el Código

feature: war部署
1. 新增war包注册nacos配置
2. 新增打包ingnore测试文件

fuzq hace 4 años
padre
commit
c31a909fa6
Se han modificado 2 ficheros con 71 adiciones y 0 borrados
  1. 7 0
      pom.xml
  2. 64 0
      src/main/java/com/dragoninfo/dcuc/authweb/NacosPortConfig.java

+ 7 - 0
pom.xml

@@ -395,6 +395,13 @@
                             <target>8</target>
                         </configuration>
                     </plugin>
+                    <plugin>
+                        <groupId>org.apache.maven.plugins</groupId>
+                        <artifactId>maven-surefire-plugin</artifactId>
+                        <configuration>
+                            <testFailureIgnore>true</testFailureIgnore>
+                        </configuration>
+                    </plugin>
                 </plugins>
             </build>
         </profile>

+ 64 - 0
src/main/java/com/dragoninfo/dcuc/authweb/NacosPortConfig.java

@@ -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 "";
+    }
+}