第一步 eclipse STS, File -> New -> Spring Starter Project, Type选择Maven, Packaging, Jar, 如果引入dev tool建议使用 java version 8.
第二步 添加依赖
第三步:添加两个profile文件, application-peer1.properties, application-peer2.properties
如果不需要server cluster, 通常如下设置
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
这两个值默认为true, 所以这里直接注释,这样让两个实例互相注册为client, 从而实现复制
server.port=8761
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
eureka.instance.appname=eureka-cluster
eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:8763/eureka
通过设置环境变量, 使用不同的profile运行程序,实现server cluster (两个实例, peer1, peer2)
Run -> Run Configurations -> Environment
第四步: 为程序添加@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
第五步:
设置 spring.profiles.active为peer1, Apply -> Run 运行第一个实例
设置 spring.profiles.active为peer2, Apply -> Run 运行第二个实例
第六步:打开浏览器输入网址 http://localhost:8761/ http://localhost:8763/
EUREKA-CLUSTER服务已经启动,并且复制成功, 代码参考官方Spring Cloud Eureka Sample, 本文代码地址:
https://github.com/china-fengguan/Spring_Cloud/tree/master/eureka-service