问题
dubbo在启动时,如果没有配置host,会调用 InetAddress.getLocalHost().getHostAddress()
获取本地 IP。该方法将会获取机器 hostname
,然后再在 /etc/hosts
配置文件中查找 hostname
对应的配置 IP;
如果没有在hosts配置外网ip,默认会注册到内网地址。
https://blog.csdn.net/weixin_44742132/article/details/104191297
通过dubboAdmin,或者通过PrettyZoo可以看zookeeper中的地址,来区分是注册到了哪个ip;
方式1:修改hosts文件
这种方式,会让这个服务器里所有的dubbo应用,都注册到公网ip上(不推荐)
步骤:
# 查看hostname
hostname
# 修改hosts文件
vim /est/hosts
# 添加 公网ip 主机名;如下
11.22.33.44 myHostName
原理:加hosts解析,在dubbo 拿hosts时,会拿到公网ip。
注意:此时hosts文件里不能有内网ip;或者把公网ip 移到 内网ip前。
方式2:启动jar包时加参数,无需修改hosts(推荐)
参考: https://blog.csdn.net/m0_54850604/article/details/123877647
这种方式只有加了参数的应用才会注册到公网ip,每加的默认还是会注册到内网ip
步骤:
建议在服务启动脚本里加,因为集群里每个节点的公网ip都不一样。
# 启动时加参数 -DDUBBO_IP_TO_REGISTRY= 192.168.1.1 该ip为dubbo所在服务器的公网ip即可。
java -jar myDubboRpc-api.jar -DDUBBO_IP_TO_REGISTRY= 192.168.1.1
# xjar启动
nohup ./xjar java -DDUBBO_IP_TO_REGISTRY=11.22.33.44 -XX:+UseG1GC -jar $SERVER_NAME --spring.profiles.active=$SERVER_ENV &> nohup.out &
官方文档https://dubbo.apache.org/zh/docs/advanced/set-host/
原理: 是在dubbo应用启动的时候,去读环境变量,然后通过变量来覆盖一些配置。
dubbo启动时读取的环境变量:
- DUBBO_IP_TO_REGISTRY — 注册到注册中心的IP地址
- DUBBO_PORT_TO_REGISTRY — 注册到注册中心的端口
- DUBBO_IP_TO_BIND — 监听IP地址
- DUBBO_PORT_TO_BIND — 监听端口
引申
docker配置
https://www.csdn.net/tags/NtzaYg0sNTQwNzMtYmxvZwO0O0OO0O0O.html
https://blog.csdn.net/m0_37246056/article/details/109283315
Dubbo3源码解析
https://blog.csdn.net/qq_31086797/article/details/124001826
配置 https://blog.csdn.net/songjunyan/article/details/125111145
Dubbo3 把reference的bean,注册到spring容器中,使用autowire注入;
https://www.csdn.net/tags/OtDaYg3sNTYwNTAtYmxvZwO0O0OO0O0O.html
* Step 1: Register ReferenceBean in Java-config class:
* <pre class="code">
* @Configuration
* public class ReferenceConfiguration {
* @Bean
* @DubboReference(group = "demo")
* public ReferenceBean<HelloService> helloService() {
* return new ReferenceBean();
* }
*
* @Bean
* @DubboReference(group = "demo", interfaceClass = HelloService.class)
* public ReferenceBean<GenericService> genericHelloService() {
* return new ReferenceBean();
* }
* }
* </pre>
*
* Step 2: Inject ReferenceBean by @Autowired
* <pre class="code">
* public class FooController {
* @Autowired
* private HelloService helloService;
*
* @Autowired
* private GenericService genericHelloService;
* }
* </pre>