dubbo 设置注册到注册中心的IP地址为公网IP

场景
某些情况下(例如公共测试环境 xx云上ECS 部署了dubbo提供者),
我们需要将dubbo提供者应用 注册到注册中心的地址为公网ip地址。

这样方便公司内网或者其他云上应用调用该dubbo提供者的接口服务。

测试
dubbo 版本 2.7.8

查看 dubbo 官方文档,一开始以为 修改属性 dubbo.protocol.host

# 服务提供者协议配置。对应的配置类: org.apache.dubbo.config.ProtocolConfig。
# 同时,如果需要支持多协议,可以声明多个 标签,并在 中通过 protocol 属性指定使用的协议。
protocol:
name: dubbo
port: 20207
host: 8.17.29.89
1
2
3
4
5
6
上面的 8.17.29.89 是 xx云 ECS 绑定的 弹性公网ip , 然而启动报错:

org.apache.dubbo.rpc.RpcException: Fail to start server(url: dubbo://8.17.29.89:20207/com.middol.zentao.rpc.jar.ZenTaoAggregationService?anyhost=false&application=zentao-rpc-api&bind.ip=192.168.6.51&bind.port=20207&channel.readonly.sent=true&codec=dubbo&deprecated=false&document=%E7%A6%85%E9%81%93rpc%E6%8E%A5%E5%8F%A3&dubbo=2.0.2&dynamic=true&generic=false&heartbeat=60000&interface=com.middol.zentao.rpc.jar.ZenTaoAggregationService&logger=slf4j&metadata-type=remote&methods=taskInfoList,getById,getUser,bugInfoList,getZenTaoEmployeesLogs,getProductStatistics,getPersonStatistics&pid=21676&qos.accept.foreign.ip=false&qos.enable=true&qos.port=33307&release=2.7.8&revision=1.0-SNAPSHOT&side=provider&timestamp=1627035267274&version=1.0) Failed to bind NettyServer on /192.168.6.51:20207, cause: Cannot assign requested address: bind
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:348) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.openServer(DubboProtocol.java:320) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.export(DubboProtocol.java:303) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:64) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:155) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:66) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.registry.integration.RegistryProtocol.lambda$doLocalExport$2(RegistryProtocol.java:255) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_151]
at org.apache.dubbo.registry.integration.RegistryProtocol.doLocalExport(RegistryProtocol.java:253) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:205) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:62) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:153) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:64) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:492) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:325) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:300) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.ServiceConfig.export(ServiceConfig.java:206) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.bootstrap.DubboBootstrap.lambda$exportServices$15(DubboBootstrap.java:1103) ~[dubbo-2.7.8.jar:2.7.8]
at java.util.HashMap$Values.forEach(HashMap.java:981) ~[na:1.8.0_151]
at org.apache.dubbo.config.bootstrap.DubboBootstrap.exportServices(DubboBootstrap.java:1090) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.bootstrap.DubboBootstrap.start(DubboBootstrap.java:901) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onContextRefreshedEvent(DubboBootstrapApplicationListener.java:59) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onApplicationContextEvent(DubboBootstrapApplicationListener.java:52) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.config.spring.context.OneTimeExecutionApplicationContextEventListener.onApplicationEvent(OneTimeExecutionApplicationContextEventListener.java:40) ~[dubbo-2.7.8.jar:2.7.8]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]
at com.middol.zentao.rpc.Application.main(Application.java:37) [classes/:na]
Caused by: org.apache.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.6.51:20207, cause: Cannot assign requested address: bind
at org.apache.dubbo.remoting.transport.AbstractServer.(AbstractServer.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.netty4.NettyServer.(NettyServer.java:77) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.transport.netty4.NettyTransporter.bind(NettyTransporter.java:35) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.Transporter$Adaptive.bind(Transporter$Adaptive.java) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.Transporters.bind(Transporters.java:56) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:44) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:70) ~[dubbo-2.7.8.jar:2.7.8]
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:346) ~[dubbo-2.7.8.jar:2.7.8]
… 40 common frames omitted
Caused by: java.net.BindException: Cannot assign requested address: bind
at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_151]

查看官方文档,debug 源码,知道 host的含义:

在 Dubbo 中, Provider 启动时主要做两个事情,
一是启动 server
二是向注册中心注册服务。
启动 server 时需要绑定 socket,
向注册中心注册服务时也需要发送 socket 唯一标识服务地址

报错无法启动 server Cannot assign requested address: bind, 其实是socket无法启动,socket端口外网无法连接, 理由是 如果服务端套接字绑定在它上面,你的客户端程序就只能在本机访问。

这里面 暂不讨论源码,总之配置文件中 (dubbo.protocol 或 dubbo.provider 的 host)的host属性,如果要配置 必须配置本机的ip (内网ip 或者 127.0.0.1 或者 不配dubbo自己获取)

解决
网上 或者 文档中有几个方法,修改hosts之类的, 我个人解决方法推荐:

设置 java 系统参数( -Dxxx=value) -DDUBBO_IP_TO_REGISTRY=“8.17.29.89”
如果是 springboot 可以这样:

java -jar myDubboRpc-api.jar -DDUBBO_IP_TO_REGISTRY=”8.17.29.89″
1
当然 也可以事先 将 DUBBO_IP_TO_REGISTRY 这个参数设置到系统环境变量里面。

如果是docker 方式,请参考 : https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-docker

其实就是docker 启动时候指定环境变量 -e DUBBO_IP_TO_REGISTRY = “8.17.29.89”
————————————————
版权声明:本文为CSDN博主「小时候的阳光」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gzt19881123/article/details/119042064

发表评论