eureka集群 ha_EurekaServer集群配置

一、程序配置

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集群中了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值