You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/03/15 14:27:17 UTC

[GitHub] [dubbo] VirensCn edited a comment on issue #9737: 在安装了docker的机器上启动dubbo服务,provider取到的host始终是docker网段的ip

VirensCn edited a comment on issue #9737:
URL: https://github.com/apache/dubbo/issues/9737#issuecomment-1059686917


   是他们改了代码,去掉了这部分[ServiceConfig.java](https://github.com/apache/dubbo/commit/901cf6f60c1c72ebb0659d77f4569b87f51bad40#diff-b90db27a4d125d825b93979c962b2d2e7d8c69d2528c0404d4a6ea7cd03169b0)
   
   ![image](https://user-images.githubusercontent.com/9736815/156868360-a77349ad-9d7a-4fab-adc5-79d2b825d48d.png)
   
   以前会尝试连接注册中心,然后从Socket中获取本地IP,现在直接获取网卡的了。。
   
   我现在是在启动时,先获取IP然后存环境变量后正常启动
   ![image](https://user-images.githubusercontent.com/9736815/156868533-43603ab5-dd5a-444e-8d92-4849abcbdb36.png)
   
   我发现在docker环境中都有环境变量`HOSTNAME`,那么可以根据HOSTNAME获取对应的IP即可。
   ```	
   	/** 初始化dubbo默认绑定的IP地址(是否覆盖环境变量) */
   	public static void initDubboIpToBind(boolean replace) throws APIException {
   		String last = System.getProperty(DUBBO_IP_TO_BIND);
   		if (last == null || last.isEmpty() || replace) {
   			setDubboIpToBind(getIpByDocker());
   		}
   	}
   
   	/** 如果在docker环境就根据hostname获取对应ip,否则按正常方式获取 */
   	private static String getIpByDocker() throws APIException {
   		String hostname = getHostNameByDocker();
   
   		if (hostname != null && !hostname.isEmpty()) {
   			return NetUtil.getIpByHost(hostname);//
   		} else {
   			return NetUtils.getLocalHost();
   		}
   	}
   
   	/** 获取在docker环境中的hostname */
   	private static String getHostNameByDocker() throws APIException {
   		String hostname = System.getProperty("HOSTNAME");
   		if (hostname == null || hostname.isEmpty()) {
   			return System.getenv("HOSTNAME");
   		} else {
   			return hostname;
   		}
   	}
   
   	/** 必须要在dubbo应用启动之前调用 */
   	private static void setDubboIpToBind(String host) throws APIException {
   		System.setProperty(DUBBO_IP_TO_BIND, host);
   	}
   ```
    
    1. `NetUtil`=`cn.hutool.core.net.NetUtil`
    2. `NetUtils`=`org.apache.dubbo.common.utils.NetUtils`
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org