dubbo注册服务IP解析异常及IP解析源码分析

  categories:资料  author:

在使用dubbo注册服务时会遇到IP解析错误导致无法正常访问.

错误信息如下:

Could not get local host ip address, will use 127.0.0.1 instead., dubbo version: 2.0.1, current hos

解决方案一:

绑定hostname+ip
先执行 :

vi /etc/hostname
假定hostname 为: zzf

获取hostname , 在hosts 中绑定

vim /etc/hosts
添加如下 :
10.2.3.4 zzf

解决方案2:(不建议使用)

在dubbo的配置文件中写死host

在协议上绑定
<dubbo:protocol host="172.16.11.111"/>

或者在每个provider中绑定host
<dubbo:provider host="172.16.11.111">

源码部分

报错提示部分的源代码:

private static InetAddress getLocalAddress0() {
     InetAddress localAddress = null;
     try {
         localAddress = InetAddress.getLocalHost();
         if (isValidAddress(localAddress)) {
             return localAddress;
         }
     } catch (Throwable e) {
         logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
     }
     try {
         Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
         if (interfaces != null) {
             while (interfaces.hasMoreElements()) {
                 try {
                     NetworkInterface network = interfaces.nextElement();
                     Enumeration<InetAddress> addresses = network.getInetAddresses();
                     if (addresses != null) {
                         while (addresses.hasMoreElements()) {
                             try {
                                 InetAddress address = addresses.nextElement();
                                 if (isValidAddress(address)) {
                                     return address;
                                 }
                             } catch (Throwable e) {
                                 logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
                             }
                         }
                     }
                 } catch (Throwable e) {
                     logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
                 }
             }
         }
     } catch (Throwable e) {
         logger.warn("Failed to retrieving ip address, " + e.getMessage(), e);
     }
     logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
     return localAddress;
 }


快乐成长 每天进步一点点      京ICP备18032580号-1