【spring cloud】config高可用

本文介绍如何使用 Spring Cloud 实现配置服务(config server)的高可用部署。通过将 config server 注册到 Eureka 服务发现组件中,并在客户端(config client)启用服务发现功能,实现客户端自动从 Eureka 获取配置服务实例列表并获取配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里的config高可用是server集群向eureka注册来完成的。主要内容如下:

1,config server 和config client都需要引入依赖

        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2,config server 和config client都需要给启动类添加注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer

3,config server 和config client都需要添加配置
添加一段向eureka注册的配置。

eureka:
  client:
    serviceUrl:
      #注册中心地址
      defaultZone: http://localhost:20000/eureka/

4,client需要添加的配置

spring:
  application:
    name: config-consumer
  cloud:
    config:
#      uri: http://localhost:60000
      discovery:
#        使服务发现生效
        enabled: true
        service-id: config-server-eureka

这里不再让client直连server了,而是通过指定服务提供方的id,在eureka上寻找id为“config-server-eureka”的实例,进而获取它提供的服务。
我们不要改变 config server的application name,只是改一下端口号,多启动几台机器,向eureka注册,这样,eureka上就会有多台提供config服务的机器了,也就完成了config服务的高可用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值