注册配置
一般的集成相关的这里不再赘述,网上一大堆
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${dubboRegistry}" group="${dubbo.group}"/>
<!-- 用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="${dubboPort}" host="${dubboHost}"/>
address 代表zk地址
group 代表注册在zk的路径分组
name 注册的名字
prot 注册到dubbo的端口, 本机与dubbo之间通信的端口
host 注册到dubbo的ip
注册参数源码解析
dubbo配置参数集成统一在ProtocolConfig.class类中,具体参数直接看对应类
本文主要说绑定ip地址相关,即dubbo之间通讯的ip端口绑定
ServiceConfig.findConfigHosts() 这个方法就是解析注册到zk上的提供者ip
这里是引用DUBBO_IP_TO_BIND 如果tomcat级别的系统配置有这个参数就去对应的ip,并会校验配置的ip是不是本机ip,通常我们不这么配置
那么接下来就会自动获取绑定的ip,即从配置文件中获取配置的ip地址,并判断如果是本机ip则通过方法获取本机ip进行覆盖,这块都没什么问题,都是绑定本机ip
DUBBO_IP_TO_REGISTRY 如果系统配置了这个参数,并且不是本机ip那么就会覆盖上面的配置,这个参数通常是docker的时候使用,为了防止网络原因导致dubbo不通
接下来贴一段源码,如果发现注册的ip与预期不符可以看源码在找问题
private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> registryURLs, Map<String, String> map) {
boolean anyhost = false;
// 读取环境变量对应的绑定ip配置
String hostToBind = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND);
if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) {
// 配置校验是否是本机ip
throw new IllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind);
}
// 没有配置则解析本机IP;如果本机多ip这里就会有问题
if (hostToBind == null || hostToBind.length() == 0) {
hostToBind = protocolConfig.getHost();
if (provider != null && (hostToBind == null || hostToBind.length() == 0)) {
hostToBind = provider.getHost();
}
if (isInvalidLocalHost(hostToBind)) {
anyhost = true;
try {
hostToBind = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.warn(e.getMessage(), e);
}
if (isInvalidLocalHost(hostToBind)) {
if (registryURLs != null && registryURLs.size() > 0) {
for (URL registryURL : registryURLs) {
try {
Socket socket = new Socket();
try {
SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort());
socket.connect(addr, 1000);
hostToBind = socket.getLocalAddress().getHostAddress();
break;
} finally {
try {
socket.close();
} catch (Throwable e) {
}
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
}
if (isInvalidLocalHost(hostToBind)) {
hostToBind = getLocalHost();
}
}
}
}
map.put(Constants.BIND_IP_KEY, hostToBind);
// 这里的环境配置就是强行修改dubbo注册到zk的IP,需要改就在这里加对应配置
String hostToRegistry = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY);
if (hostToRegistry != null && hostToRegistry.length() > 0 && isInvalidLocalHost(hostToRegistry)) {
throw new IllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry);
} else if (hostToRegistry == null || hostToRegistry.length() == 0) {
// bind ip is used as registry ip by default
hostToRegistry = hostToBind;
}
map.put(Constants.ANYHOST_KEY, String.valueOf(anyhost));
return hostToRegistry;
}