Dubbo服务暴露之注册地址和端口

Posted 后端进阶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo服务暴露之注册地址和端口相关的知识,希望对你有一定的参考价值。

我们直接就定位到dubbo服务暴露url组装的方法com.alibaba.dubbo.config.ServiceConfig#doExportUrlsFor1Protocol这个方法源码太多了, 其实查找Host和Port就两行调用代码:

 private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> registryURLs) {
     // ...
     // 查找host
     String host = this.findConfigedHosts(protocolConfig, registryURLs, map);
     // 查找port
     Integer port = this.findConfigedPorts(protocolConfig, name, map);
     // ...
 }

继续往下撸:

private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> registryURLs, Map<String, String> map) {
    boolean anyhost = false;

    String hostToBind = getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND);
    if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) {
        throw new IllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind);
    }

    // if bind ip is not found in environment, keep looking up
    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.isEmpty()) {
                    for (URL registryURL : registryURLs) {
                        if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) {
                            // skip multicast registry since we cannot connect to it via Socket
                            continue;
                        }
                        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);

    // registry ip is not used for bind ip by default
    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;
}
private String getValueFromConfig(ProtocolConfig protocolConfig, String key) {
    String protocolPrefix = protocolConfig.getName().toUpperCase() + "_";
    String port = ConfigUtils.getSystemProperty(protocolPrefix + key);
    if (port == null || port.length() == 0) {
        port = ConfigUtils.getSystemProperty(key);
    }
    return port;
}
/**
 * System environment -> System properties
 *
 * @param key key
 * @return value
 */

public static String getSystemProperty(String key) {
  String value = System.getenv(key);
  if (value == null || value.length() == 0) {
    value = System.getProperty(key);
  }
  return value;
}
public class Constants {
  public static final String DUBBO_IP_TO_REGISTRY = "DUBBO_IP_TO_REGISTRY";
  public static final String DUBBO_PORT_TO_REGISTRY = "DUBBO_PORT_TO_REGISTRY";
  public static final String DUBBO_IP_TO_BIND = "DUBBO_IP_TO_BIND";
  public static final String DUBBO_PORT_TO_BIND = "DUBBO_PORT_TO_BIND";
}
export DUBBO_DUBBO_IP_TO_REGISTRY=指定ip
export DUBBO_PORT_TO_REGISTRY=指定端口

我将本地服务器这两个环境变量配置成跳板机的ip和端口,下面我还要将跳板机配置nginx映射。

upstream service-172.17.10.121-20040 {
    server 172.17.10.121:20040 weight=1 max_fails=2 fail_timeout=30s;
}

server {
    listen 20040;
    access_log /log/nginx/access_service-172.17.10.121-20040.log str_basic;
    error_log /log/nginx/error_service-172.17.10.121-20040.log;
    proxy_connect_timeout 2s;
    proxy_pass service-172.17.10.121-20040;
}


长按可以订阅

http://objcoding.com

以上是关于Dubbo服务暴露之注册地址和端口的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo点滴之服务注册中心

Dubbo点滴之服务注册中心

dubbo起停之服务暴露

Dubbo之服务注册

dubbo服务暴露

dubbo 配置属性