spring cloud项目03:高可用注册中心

JDK 8

spring boot 2.5.2

spring cloud 2020.0.3

---

目录

高可用配置(一)

高可用配置(二)

参考文档

在生产环境下,如果只有一个 注册中心服务,存在很大的安全风险——宕机会导致整个S.C.系统挂掉(虽然 Client的 服务信息缓存 能撑一段时间——具体多久?)。

因此,需要启动多个注册中心服务(分布在不同的主机、机房等,还可以更大?),进而实现 系统高可用

高可用配置(一)

试验:

三个 注册中心服务:peer1、peer2、peer3,端口分别为 8771、8772、8773。

在一个application.properties中包含 三个注册中心的配置。

建立服务,在 主程序类 添加 @EnableEurekaServer 即可:

@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistrationAndDiscoveryServiceApplication {

	public static byte[] arr;
	
	public static void main(String[] args) {
		SpringApplication.run(ServiceRegistrationAndDiscoveryServiceApplication.class, args);
    }
}

配置文件修改如下:

# application.properties 文件
spring.application.name=eureka-server

# 默认启动peer1
spring.profiles.active=peer1

eureka.instance.prefer-ip-address=true

#---

spring.config.activate.on-profile=peer1

eureka.instance.hostname=peer1

server.port=8771

eureka.client.service-url.defaultZone=http://localhost:8772/eureka/,http://localhost:8773/eureka/

#---

spring.config.activate.on-profile=peer2

eureka.instance.hostname=peer2

server.port=8772

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8773/eureka/

#---

spring.config.activate.on-profile=peer3

eureka.instance.hostname=peer3

server.port=8773

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8772/eureka/

配置文件注意事项:

1、这是一个多文档文件,,不同部分使用 “#---” 分隔

分隔行前后 不能直接有 注释行,否则,配置无效

详见 S.B. 官方文档 的 “Working with Multi-Document Files” 一节。

2、eureka.instance.prefer-ip-address=true

3、spring.config.activate.on-profile=peerN

不同profile,使用不同的配置。

# spring.config.activate.on-profile 官文解释
Profile expressions that should match for the document to be included.

启动时,添加参数 --spring.profiles.active=peerN (N=1、2、3),逐个启动。

先启动的服务的日志 中可能存在错误;

三个服务都启动后,依次访问链接:

http://localhost:8771/

http://localhost:8772/

http://localhost:8773/

三个 EUREKA-SERVER 都注册成功了,三个页面不同的是,General Info 下的信息 有差别。

疑问:unavailable-replicas 怎么 有值?而 available-replicas 却是空的呢?TODO

测试服务注册

两个Web服务:web-first, web-second,web-second会调用 web-first的服务

web-first配置:

eureka.instance.prefer-ip-address=true
# 仅配置1个 peer1
eureka.client.service-url.defaultZone=http://localhost:8771/eureka/

web-second配置:

eureka.instance.prefer-ip-address=true
# 仅配置1个 peer2
eureka.client.service-url.defaultZone=http://localhost:8772/eureka/

两个服务配置的 eureka.client.service-url.defaultZone 不同,此时,三个 注册中心服务上 是否会 都有两者的信息呢?

答案是 YES

下面是 peer3 的页面:

 测试结果,web-second 可以 成功调用 web-first 提供的服务。

当然,正确的 eureka.client.service-url.defaultZone 应该是下面的:

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8772/eureka/,http://localhost:8773/eureka/

停掉一个 注册中心服务——peer1,观察其它两个注册中心的 日志、页面 的变化。

- 日志

Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8771 timed out

- 页面

EUREKA-SERVER此时只有 2个了——8772、8773。

此时,web-second 能成功 调用 web-first 的服务吗?

调用成功!

生产情况下,此时会报警——各种通知,再由运维、开发人员来解决此问题,保证 短时间内停掉的服务 能重启,恢复。

重启 peer1 后,其它两个的日志显示下面的信息:

Registered instance EUREKA-SERVER/DESKTOP-BDNTQQ3:eureka-server:8771 with status UP (replication=false)

高可用配置(二)

试验:将各个profile的配置放到不同的 application-{profile}.properties 文件中,其它条件不变。

application.properties 内容:

spring.application.name=eureka-server

# 默认启动peer1
spring.profiles.active=peer1

# 默认false-需要在 注册中心服务 的 hosts 中配置(跨网络)
# 设置为 true,则不需要 配置hosts——IP可以访问成功的话
eureka.instance.prefer-ip-address=true

application-{profile}.properties 中:

eureka.instance.hostname=peer1
server.port=8771

eureka.client.service-url.defaultZone=http://localhost:8772/eureka/,http://localhost:8773/eureka/

注意,修改 前两个配置的值。

启动三个服务,正常。

启动 web-first、web-second 测试,正常。

疑问

1、注册中心服务 不会有配置几十、几百个的吧?

2、更多注册中心的 更多配置 有哪些?

3、除了 Eureka的注册中心,还有什么注册中心可以在 Spring Cloud中使用?

参考文档

1、书《Spring Cloud微服务实战》 by 翟永超

第三章 服务治理:Spring Cloud Eureka

2、SpringCloud之Eureka注册中心原理及其搭建

本文还涉及到了 安全认证 等,更深入。

3、

原文地址:https://www.cnblogs.com/luo630/p/15149622.html