配置中心Spring Cloud Config

本文介绍了如何使用Spring Cloud Config搭建配置中心,包括Config Server和Config Client的配置,以及通过Eureka进行服务注册。此外,还展示了如何实现配置的动态刷新,包括使用Actuator的`/refresh`端点和结合Spring Cloud Bus实现全网更新。最后,通过RabbitMQ作为消息总线,实现在Config Server端更新配置后,所有客户端自动刷新配置。

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

1、概念

在这里插入图片描述

官网:https://cloud.spring.io/spring-cloud-config/reference/html/

访问方式:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application 服务器名称
profile 环境名称,开发、测试、生产
lable 仓库分支、默认master分支

2、搭建Config Server

2.1、引入依赖

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

2.2、@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

2.3、配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/xxxx/config_cloud
          username:xxx8@qq.com
          password: xxxx

server:
  port: 9010

2.4、测试

在这里插入图片描述

访问http://localhost:9010/config-dev.yml

在这里插入图片描述

3、搭建Config Client

在这里插入图片描述

3.1、引入依赖

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

3.2、修改对应服务的配置文件

把application.yml 改为 bootstrap.yml

#  bootstrap.yml
#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

#服务的名称
spring:
  application:
    name: order-service
  #指定从哪个配置中心读取
  cloud:
    config:
      discovery:
        service-id: CONFIG-SERVER
        enabled: true
      profile: dev
      #建议用lable去区分环境,默认是lable是master分支
      #label: test

3.3、gitee中添加对应的配置文件

在这里插入图片描述

3.4、测试

添加一个测试类

在这里插入图片描述

@RestController
public class TestController {

    @Value("${test}")
    private String test;

    @GetMapping("test")
    public String test(){
        return test;
    }
}

启动顺序:注册中心-> 配置中心->对应的服务

通过配置中心访问:http://localhost:9010/order-service-dev.yml

在这里插入图片描述

启动order-servcie:

在这里插入图片描述

访问:http://localhost:8090/test

在这里插入图片描述

3.5、手动刷新

现在有一个问题:在git上修改配置文件后,order-service必须重启才能读到修改的内容

在这里插入图片描述

配置中心读到的也是2

在这里插入图片描述

但是order-service的还是1

在这里插入图片描述

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

增加配置:

management:
      endpoints:
        web:
          exposure:
            include: "*"

需要刷新配置的地方,增加注解 @RefreshScope

@RefreshScope
@RestController
public class TestController {

    @Value("${test}")
    private String test;

    @GetMapping("test")
    public String test(){
        return test;
    }
}

post 方式发送http://localhost:8090/actuator/refresh刷新

4、结合Spring Cloud Bus

Spring Cloud Bus消息总线,支持RabbitMQ 和Kafka

在这里插入图片描述

4.1、Config Server端

引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

添加配置

spring:
  rabbitmq:
    host: 192.168.0.71
    port: 5672
    username: guest
    password: guest
management:
      endpoints:
        web:
          exposure:
            include: "bus-refresh"

4.2、Config Client 端配置

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置文件

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/


#服务的名称
spring:
  rabbitmq:
    host: 192.168.0.71
    port: 5672
    username: guest
    password: guest

  application:
    name: order-service
  # 允许消息跟踪
  cloud:
    bus:
      trace:
        enabled: true
    #指定从哪个配置中心读取
    config:
      discovery:
        service-id: CONFIG-SERVER
        enabled: true
      profile: dev
      #建议用lable去区分环境,默认是lable是master分支
      #label: test

management:
  endpoints:
    web:
      exposure:
        include: "*"

在需要刷新的类上加上@RefreshScope

@RefreshScope
@RestController
public class TestController {

    @Value("${test}")
    private String test;

    @GetMapping("test")
    public String test(){
        return test;
    }
}

4.3、发送刷新请求到Config Server端

curl -X POST http://localhost:9091/actuator/bus-refresh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值