先看官方文档的配置
--- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2/eureka/ --- spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1/eureka/
两台注册中心的高可用的时候,就是分别注册到对方上去
同理,三台以上的时候
--- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2/eureka/,http://peer3/eureka/ --- spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1/eureka/,http://peer3/eureka/
--- spring: profiles: peer3 eureka: instance: hostname: peer3 client: serviceUrl: defaultZone: http://peer1/eureka/,http://peer2/eureka/
不论多少台主机,都是将自己注册到除了自己之外的其他注册中心上去
不过这里有一个小问题,就是peer是一个主机名,如果想要使用还需要在host文件中配置,如果想要简单一点,只用ip就可以了。
--- spring: profiles: peer1
server:
port: 8761 eureka: instance: client: serviceUrl: defaultZone: http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/ --- spring: profiles: peer2
server:
port: 8762 eureka: instance: client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8763/eureka/
--- spring: profiles: peer3
server:
port: 8763 eureka: instance: client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
这样就可以了,而且官方文档也是这么说的
如果您在一台知道自己的主机名的机器上运行(默认情况下使用java.net.InetAddress
查找),则不需要eureka.instance.hostname
。