Ver código fonte

添加配置ip的逻辑

tianboguang 3 anos atrás
pai
commit
8e236aa6d1

+ 14 - 2
platform-admin/src/main/java/com/platform/config/NacosConfiguration.java

@@ -7,10 +7,13 @@ import com.alibaba.nacos.api.exception.NacosException;
 import com.alibaba.nacos.api.naming.NamingService;
 import com.alibaba.nacos.spring.context.annotation.EnableNacos;
 import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.context.annotation.PropertySources;
+import org.springframework.util.ObjectUtils;
 
 import javax.annotation.PostConstruct;
 import java.net.InetAddress;
@@ -28,6 +31,8 @@ import java.net.UnknownHostException;
 @NacosPropertySource(dataId = "${nacos.config.data-id}", autoRefreshed = true,type = ConfigType.PROPERTIES,groupId = "${nacos.config.group}")
 public class NacosConfiguration {
 
+    protected Logger logger = LoggerFactory.getLogger(this.getClass());
+
     @NacosInjected
     private NamingService namingService;
 
@@ -40,11 +45,18 @@ public class NacosConfiguration {
     @Value("${server.port}")
     private Integer port;
 
+    @Value("${local.ip:#{null}}")
+    private String ip;
+
     @PostConstruct
     public void init() {
         try {
-            InetAddress address = InetAddress.getLocalHost();
-            namingService.registerInstance(applicationName, groupName, address.getHostAddress(), port);
+            if(ObjectUtils.isEmpty(ip)){
+                InetAddress address = InetAddress.getLocalHost();
+                ip = address.getHostAddress();
+            }
+            logger.info("service registed {}:{}",ip,port);
+            namingService.registerInstance(applicationName, groupName, ip, port);
         } catch (UnknownHostException | NacosException e) {
             e.printStackTrace();
         }