首页 > 其他分享 >[Dubbo] 多网卡问题

[Dubbo] 多网卡问题

时间:2022-11-17 13:58:21浏览次数:30  
标签:Dubbo String 网卡 host provider protocolConfig hostToBind 问题 port

Dubbo启动时会获取设备网卡地址,可能会从设备上的以太网卡,虚拟网卡中选一个。

Dubbo源码

private URL buildUrl(ProtocolConfig protocolConfig, Map<String, String> params) {
        String name = protocolConfig.getName();
        if (StringUtils.isEmpty(name)) {
            name = "dubbo";
        }
        //获取host
        String host = findConfiguredHosts(protocolConfig, this.provider, params);
        //获取port
        Integer port = findConfiguredPort(protocolConfig, this.provider, this.getExtensionLoader(Protocol.class), name, params);
        URL url = new ServiceConfigURL(name, (String)null, (String)null, host, port, (String)this.getContextPath(protocolConfig).map((p) -> {
            return p + "/" + this.path;
        }).orElse(this.path), params);
        if (this.getExtensionLoader(ConfiguratorFactory.class).hasExtension(((URL)url).getProtocol())) {
            url = ((ConfiguratorFactory)this.getExtensionLoader(ConfiguratorFactory.class).getExtension(((URL)url).getProtocol())).getConfigurator((URL)url).configure((URL)url);
        }

        URL url = ((URL)url).setScopeModel(this.getScopeModel());
        url = url.setServiceModel(this.providerModel);
        return url;
    }
View Code

获取host分别有三个源头 : host的获取顺序优先级 分别是 protocolConfig >> provider >> 本地网卡地址

 1 private static String findConfiguredHosts(ProtocolConfig protocolConfig, ProviderConfig provider, Map<String, String> map) {
 2         boolean anyhost = false;
 3         //从系统环境变量去获取host ,key为DUBBO_IP_TO_BIND
 4         String hostToBind = getValueFromConfig(protocolConfig, "DUBBO_IP_TO_BIND");
 5         //注意这里 host不能为 127.0.0.1 和 localhost ,至于为什么不能为环回地址 此处不是很了解为啥这样设计???
 6         if (hostToBind != null && hostToBind.length() > 0 && NetUtils.isInvalidLocalHost(hostToBind)) {
 7             throw new IllegalArgumentException("Specified invalid bind ip from property:DUBBO_IP_TO_BIND, value:" + hostToBind);
 8         } else {
 9             // host的获取顺序优先级 分别是 protocolConfig >> provider >> 本地网卡地址
10             if (StringUtils.isEmpty(hostToBind)) {
11                 //protocolConfig 中获取
12                 hostToBind = protocolConfig.getHost();
13                 if (provider != null && StringUtils.isEmpty(hostToBind)) {
14                     //provider中获取
15                     hostToBind = provider.getHost();
16                 }
17 
18                 if (NetUtils.isInvalidLocalHost(hostToBind)) {
19                     anyhost = true;
20                     if (logger.isDebugEnabled()) {
21                         logger.info("No valid ip found from environment, try to get local host.");
22                     }
23                     //获取网卡ip
24                     hostToBind = NetUtils.getLocalHost();
25                 }
26             }
27 
28             map.put("bind.ip", hostToBind);
29             String hostToRegistry = getValueFromConfig(protocolConfig, "DUBBO_IP_TO_REGISTRY");
30             if (StringUtils.isNotEmpty(hostToRegistry) && NetUtils.isInvalidLocalHost(hostToRegistry)) {
31                 throw new IllegalArgumentException("Specified invalid registry ip from property:DUBBO_IP_TO_REGISTRY, value:" + hostToRegistry);
32             } else {
33                 if (StringUtils.isEmpty(hostToRegistry)) {
34                     hostToRegistry = hostToBind;
35                 }
36 
37                 map.put("anyhost", String.valueOf(anyhost));
38                 return hostToRegistry;
39             }
40         }
41     }
View Code 获取port也分别有三个源头 : 分别是 protocolConfig >> provider >> 扩展协议Protocol >> 本地随机可用 port
private static synchronized Integer findConfiguredPort(ProtocolConfig protocolConfig, ProviderConfig provider, ExtensionLoader<Protocol> extensionLoader, String name, Map<String, String> map) {
        String port = getValueFromConfig(protocolConfig, "DUBBO_PORT_TO_BIND");
        Integer portToBind = parsePort(port);
        if (portToBind == null) {
            portToBind = protocolConfig.getPort();
            if (provider != null && (portToBind == null || portToBind == 0)) {
                portToBind = provider.getPort();
            }
            
            //此处通过SPI可以扩展自定义的协议类型
            int defaultPort = ((Protocol)extensionLoader.getExtension(name)).getDefaultPort();
            if (portToBind == null || portToBind == 0) {
                portToBind = defaultPort;
            }
            
            //获取一个随机可用的port
            if (portToBind <= 0) {
                portToBind = getRandomPort(name);
                if (portToBind == null || portToBind < 0) {
                    portToBind = NetUtils.getAvailablePort(defaultPort);
                    putRandomPort(name, portToBind);
                }
            }
        }

        map.put("bind.port", String.valueOf(portToBind));
        String portToRegistryStr = getValueFromConfig(protocolConfig, "DUBBO_PORT_TO_REGISTRY");
        Integer portToRegistry = parsePort(portToRegistryStr);
        if (portToRegistry == null) {
            portToRegistry = portToBind;
        }

        return portToRegistry;
    }
View Code

 

解决方案:
通过对上面的源码分析,可以得出结论:

1.protocolConfig 我们可以通过配置设置 host和port信息。
xml配置文件方式
<dubbo:protocol name=“dubbo” port=“20880” host=“192.168.1.8” accesslog=“true”/>

SpringBoot 注入方式


2.provider 直接设置host,port 默认采用 上面protocol设置的port。
xml配置文件方式
<dubbo:provider host=“192.168.1.8” protocol=“dubbo”/>

通过Api代码设置
ServiceConfig service = new ServiceConfig();否则可能造成内存和连接泄漏
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion(“1.0.0”);

 

https://blog.csdn.net/z_Jimmy/article/details/126846473

 

标签:Dubbo,String,网卡,host,provider,protocolConfig,hostToBind,问题,port
From: https://www.cnblogs.com/NetUSA/p/16888639.html

相关文章

  • [SpringBoot-Dubbo] 报错:NoClassDefFoundError: org/apache/curator/framework/recipe
    NoClassDefFoundError:org/apache/curator/framework/recipes/cache/NodeCacheListener缺少curator依赖<dependency><groupId>org.apache.curator</groupId><ar......
  • URL传值带加号“+”的问题的解决方法
    当别人请求接口时,客户端向服务器传递参数时,参数中的“+”全部变成了空格,原因是URL中默认的将“+”号转义了。解决方法:方法一:前端传递参数时,将客户端带“+”的参数中的“+”......
  • Dubbo启动时的错误和缺少的依赖
    Causedby:java.lang.ClassNotFoundException:org.apache.curator.framework.CuratorFrameworkFactory<dependency><groupId>org.apache.curator</groupId><......
  • 本地源代码调试遇到的问题
    杨中科视频结业一、添加环境变量,配置数据库链接变量名:DefaultDB:ConnStr变量值:您的数据库链接字符串二、迁移数据库Infrastructure结尾的项目需要迁移数据库(SearchSer......
  • 想请教在座的各位关于校园网的问题
    说一下解决方法,首先打开设置,然后网络和internet,点击更多网络适配器选项,然后选中WLAN右击选择属性,在弹出来的对话框中点击配置,点击高级选项卡,在属性那一栏里,选择802.11ax/ac......
  • 16_Kafka高级_数据一致性问题
    (1)follower故障follower发生故障后会被临时踢出ISR,待该follower恢复后,follower会读取本地磁盘记录的上次的HW,并将log文件高于HW的部分截取掉,从HW开始向leader进行同步。等......
  • js中的模板字符串问题
    在写js的字符串时,虽然单双引号都用了,但是${}修饰的字符串却始终没有正确替换为变量,最后查了一下语法,发现和python中不同,js中的模板字符串是需要用反引号的,而不是一般引号,就......
  • UOS家庭版初步使用遇到得问题记录
    一、安装问题硬盘如果太小不要用自动分区方式这样根分区会很小(可能就10G多)二、安装坚果云问题1、应用商店自带的坚果云是安卓应用无法同步本地文件夹2、官方下载对应的......
  • vue 解决el-table 表体数据发生变化时,未重新渲染问题|页面不更新问题
    一、所遇到的问题在使用el-table组件时,数据已经发生了变化,但是页面显示的数据却没变化;二、解决办法在el-table中添加一个key,可以设置成boolean类型的,在数据更新后更新......
  • 三染色问题与零知识证明
    完整文件下载......