一、程序配置
1、pom添加依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
(这里因为在pom中通过dependencyManagement配置将spring-cloud-dependencies import进来,所以不需要写版本号)
或者在是使用Spring Initializr创建SpringBoot项目时,添加上起步依赖Eureka Server (路径:Cloud Discovery -- Eureka Server)。
完整pom为:
4.0.0
com.example
microservice-discovery-eureka-ha
0.0.1-SNAPSHOT
jar
microservice-discovery-eureka-ha
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
UTF-8
UTF-8
1.8
Greenwich.M3
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2、在SpringBoot启动类上添加注释@EnableEurekaServer
3、配置application.yml或application.properties
spring:
application:
name: microservice-discovery-eureka-ha---spring:
profiles: peer1
server:
port:8761eureka:
instance:
hostname: peer1
client:
service-url:
defaultZone: http://peer2:8762/eureka/
register-with-eureka: false #作为对比,这里表示不将peer1注册到Eureka Server集群中,peer2中默认为truefetch-registry: false #作为对比,这里表示peer1不会从Eureka Server集群获取注册表信息
---spring:
profiles: peer2
server:
port:8762eureka:
instance:
hostname: peer2
client:
service-url:
defaultZone: http://peer1:8761/eureka/
二、程序运行
将程序打包后,执行jar包:
java -jar xxx.jar --spring.profiles.active=peer1
java -ajr xxx.jar --spring.profiles.active=peer2
通过spring.profiles.active指定使用application.yml中配置的哪一个profile启动程序。
三、程序截图
我们可以看到,peer2将自身注册到Eureka Server集群中了,而peer1没有。
Eureka Server配置启动好之后,我们就可以写微服务,并通过配置将其注册到Eureka Server集群中了