0%

EUREKA注册中心高可用集群配置

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

第二步:2002,2003的主啟動類EurekaServerApplication_2002,EurekaServerApplication_2003複製下

第三步:直接改端口不同,來區別是什麼

第四步:
之前的2001

1
2
3
4
5
6
7
8
9
10
11
12
13
 server:
port:2001
context-path:/
eurka:
instance:
hostname:locahost #eureka註冊中心名稱
hostname:eureka2001.java1234.com #集群
client:
register-with-eureka:false (不能自己註冊自己)
fetch-registry:false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaltZone:http://${eurka.instance.hostname}:${service.port}/eureka #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
defaltZone: http://eureka2002.java1234.com:2002/eureka http://eureka2003.java1234.com:2003/eureka #集群版

2002修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server:

port: 2002

context-path: /



eureka:

instance:

# 单机 hostname: localhost #eureka注册中心实例名称

hostname: eureka2002.java1234.com # 集群

client:

register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。

fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false

service-url:

defaltZone: http://eureka2001.java1234.com:2001/eureka http://eureka2003.java1234.com:2003/eureka #集群版

2003修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server:

port: 2003

context-path: /

eureka:

instance:

# 单机 hostname: localhost #eureka注册中心实例名称

hostname: eureka2003.java1234.com # 集群

client:

register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。

fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false

service-url:

defaltZone: http://eureka2001.java1234.com:2001/eureka http://eureka2002.java1234.com:2002/eureka #集群版

第五部:修改服務提供者項目application.yml,主要修改eureka.client.service-url.defaultZone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server:

port: 1001

context-path: /

eureka:

instance:

hostname: localhost #eureka客户端主机实例名称

appname: microservice-student #客户端服务名

instance-id: microservice-student:1001 #客户端实例名称

prefer-ip-address: true #显示IP

client:

service-url:

# 单机 defaultZone: http://localhost:2001/eureka #把服务注册到eureka注册中心

defaultZone: http://eureka2001.java1234.com:2001/eureka/,http://eureka2002.java1234.com:2002/eureka/,http://eureka2003.java1234.com:2003/eureka/ # 集群

这里本质是三个服务注册中心都有我们服务提供者的信息,等后面讲到服务发现和服务调用,我们通过一些策略(默认轮询),会去找对应的服务注册中心;通过集群,能减轻每个服务注册中心的压力;