1.修改eureka-server的配置文件application.yml,在该配置文件中采用多profile的格式,具体代码如下:(按照之前代码修改)
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone:
http://peer2:8762/eureka/
register-with-eureka: false
fetch-registry: false
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone:
http://peer1:8761/eureka/
在上述代码中,定义了两个profile文件,分别为peer1和peer2,它们的hostname分别为peer1和peer2。在实际开发中,可能是具体的服务器ip地址,它们的端口分别为8761和8762。
因为在本地搭建的Eureka Server集群,所以修改本地host。windows的host路径:C:\Windows\System32\drivers\etc\hosts,
修改内容如下:
127.0.0.1 peer1
127.0.0.1 peer2
编译maven工程,编译完成会在target文件夹下生成eureka-server-1.0-SNAPSHOT.jar
idea终端进入target目录下,开启两个终端,分别输入以下指令:
java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=peer2
修改eureka-client项目下的bootstrap.yml文件,具体内容为:
eureka:
client:
service-url:
defaultZone:
http://peer1:8761/eureka/
server:
port: 8763
spring:
application:
name: eureka-client
启动eureka-client工程,访问http://peer1:8761/,显示以下结果:
eureka-client已经向peer1结点注册,并且在DS Replicas选项中显示了peer2
访问http://peer2:8762/
构建完成。