起因
更换电脑,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);
}
}