今天根据网上配置Eureka高可用注册中心的教程来试着配置eureka集群。
我的Eureka配置文件如下:
---
spring:
profiles: peer1
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:8762/eureka/
---
spring:
profiles: peer2
application:
name: eureka-server
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
运行时启动两个实例,profiles分别为peer1和peer2。
启动后访问localhost:8761,eureka界面如下:
访问localhost:8762,界面如下:
可见peer1和peer2之间并没有互相注册,因为peer1和peer2中unavailable-replicas和
available-replicas为空,且我在peer1中注册的zuul实例,并没有共享给peer2.
后来发现原因是因为如果hostname写的localhost或者127.0.0.1的话,就会出现这种问题,当时嫌麻烦就用的localhost代替,坑啊。
这是win10改host的教程,将peer1和peer2都指向localhost之后,修改Eureka配置如下:
---
spring:
profiles: peer1
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
fetch-registry: true
register-with-eureka: false
serviceUrl:
defaultZone: http://peer2:8762/eureka/
---
spring:
profiles: peer2
application:
name: eureka-server
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
fetch-registry: true
register-with-eureka: false
serviceUrl:
defaultZone: http://peer1:8761/eureka/
再次启动,访问peer2:8762,看到了peer1:8761的信息和zuul实例的信息,问题解决