• dubbo服务多网卡IP问题


    起因

    更换电脑,dubbo服务不能调试,win7电脑好使,win10不行

    分析

    经过调试发现注册的ip地址,不是VPN分配的地址,多方面查找资料说ip排序的问题,尝试一下方法:

    • 网络连接重新命名成一样的
    • 设置网络活跃点数
    • 2台电脑java版本安装一致

    以上测试都不能解决,于是调试代码,发现NetUtils获取网卡是直接取的第一块网卡的地址,由于是静态对象,这样启动的时候,根据自己的规则先获取下,赋个初值就可以了,代码如下:

    package com.masg.test;
    
    import com.alibaba.dubbo.common.utils.NetUtils;
    import com.alibaba.dubbo.container.Main;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    import java.util.regex.Pattern;
    
    public class StartMain {
    
        public static void setFinalStatic(Field field, Object newValue) throws Exception {
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, newValue);
        }
    
    
        public static InetAddress getLocalHost() throws UnknownHostException {
            Pattern IP_PATTERN = Pattern.compile("\\d{1,2}(\\.\\d{1,3}){3,5}$");
            try {
                Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                if (interfaces != null) {
                    while (interfaces.hasMoreElements()) {
                        NetworkInterface network = (NetworkInterface) interfaces.nextElement();
                        Enumeration<InetAddress> addresses = network.getInetAddresses();
                        if (addresses != null) {
                            while (addresses.hasMoreElements()) {
                                InetAddress address = (InetAddress) addresses.nextElement();
                                if (address != null && !address.isLoopbackAddress()) {
                                    String name = address.getHostAddress();
                                    if (name != null && !"0.0.0.0".equals(name) && !"127.0.0.1".equals(name) && IP_PATTERN.matcher(name).matches()) {
                                        return address;
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Throwable var8) {
            }
            return null;
        }
    
        public static void main(String[] args) throws Exception {
            NetUtils net = new NetUtils();
            InetAddress address = getLocalHost();
    
            Class<NetUtils> netUtilsClass = (Class<NetUtils>) net.getClass();
            Field LOCAL_ADDRESS = netUtilsClass.getDeclaredField("LOCAL_ADDRESS");
            setFinalStatic(LOCAL_ADDRESS, address);
    
            System.setProperty("dubbo.protocol.dubbo.host", address.getHostAddress()); 
     
    
            Main.main(args);
        }
    }
    
  • 相关阅读:
    【数据结构】线性表&&顺序表详解和代码实例
    【智能算法】超详细的遗传算法(Genetic Algorithm)解析和TSP求解代码详解
    【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
    【智能算法】迭代局部搜索(Iterated Local Search, ILS)详解
    10. js时间格式转换
    2. 解决svn working copy locked问题
    1. easyui tree 初始化的两种方式
    10. js截取最后一个斜杠后面的字符串
    2. apache整合tomcat部署集群
    1. apache如何启动
  • 原文地址:https://www.cnblogs.com/masg/p/16330070.html
Copyright © 2020-2023  润新知