Просмотр исходного кода

update 优化 使用spring工具自定义dubbo ip获取方法(针对多网卡ip获取不正确问题)

疯狂的狮子Li 2 лет назад
Родитель
Сommit
838ac9d453

+ 5 - 0
ruoyi-common/ruoyi-common-dubbo/pom.xml

@@ -54,5 +54,10 @@
             </exclusions>
             </exclusions>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-commons</artifactId>
+        </dependency>
+
     </dependencies>
     </dependencies>
 </project>
 </project>

+ 42 - 0
ruoyi-common/ruoyi-common-dubbo/src/main/java/org/dromara/common/dubbo/config/CustomBeanFactoryPostProcessor.java

@@ -0,0 +1,42 @@
+package org.dromara.common.dubbo.config;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.cloud.commons.util.InetUtils;
+import org.springframework.core.Ordered;
+
+import java.net.Inet6Address;
+import java.net.InetAddress;
+
+/**
+ * dubbo自定义IP注入(避免IP不正确问题)
+ *
+ * @author Lion Li
+ */
+public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
+
+	@Override
+	public int getOrder() {
+		return Ordered.HIGHEST_PRECEDENCE;
+	}
+
+    @Override
+    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
+        InetUtils inetUtils = beanFactory.getBean(InetUtils.class);
+        String ip = "127.0.0.1";
+        InetAddress address = inetUtils.findFirstNonLoopbackAddress();
+        if (address != null) {
+            if (address instanceof Inet6Address) {
+                String ipv6AddressString = address.getHostAddress();
+                if (ipv6AddressString.contains("%")) {
+                    ipv6AddressString = ipv6AddressString.substring(0, ipv6AddressString.indexOf("%"));
+                }
+                ip = ipv6AddressString;
+            } else {
+                ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
+            }
+        }
+        System.setProperty("DUBBO_IP_TO_REGISTRY", ip);
+    }
+}

+ 6 - 0
ruoyi-common/ruoyi-common-dubbo/src/main/java/org/dromara/common/dubbo/config/DubboConfiguration.java

@@ -2,8 +2,10 @@ package org.dromara.common.dubbo.config;
 
 
 import org.dromara.common.core.factory.YmlPropertySourceFactory;
 import org.dromara.common.core.factory.YmlPropertySourceFactory;
 import org.dromara.common.dubbo.properties.DubboCustomProperties;
 import org.dromara.common.dubbo.properties.DubboCustomProperties;
+import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.boot.autoconfigure.AutoConfiguration;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.context.annotation.PropertySource;
 
 
 /**
 /**
@@ -14,4 +16,8 @@ import org.springframework.context.annotation.PropertySource;
 @PropertySource(value = "classpath:common-dubbo.yml", factory = YmlPropertySourceFactory.class)
 @PropertySource(value = "classpath:common-dubbo.yml", factory = YmlPropertySourceFactory.class)
 public class DubboConfiguration {
 public class DubboConfiguration {
 
 
+    @Bean
+    public BeanFactoryPostProcessor customBeanFactoryPostProcessor() {
+        return new CustomBeanFactoryPostProcessor();
+    }
 }
 }