当前位置: 首页 > news >正文

Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件

要使用 Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件,你可以按照以下步骤操作:

### 步骤 1: 添加依赖

首先,确保你的项目中添加了 Spring Cloud Config 客户端和 Bus 的依赖。对于 Maven 项目,`pom.xml` 文件应该包含类似如下的依赖:

```xml
<dependencies>
    <!-- Spring Cloud Config Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!-- Spring Cloud Bus (选择适合你的消息代理的启动器,这里是 RabbitMQ) -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
</dependencies>
```

### 步骤 2: 配置 Config Server

配置一个 Config Server 实例,它将作为配置信息的中心仓库。在 `application.yml` 或 `application.properties` 文件中配置 Git 仓库和搜索路径:

```yaml
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-config-repo.git
          search-paths: config-repo-path
```

### 步骤 3: 启动 Config Server

启动你的 Config Server 应用。它将监听配置文件的更改,并提供配置信息给客户端。

### 步骤 4: 配置 Config Client

在客户端应用的 `bootstrap.yml` 或 `bootstrap.properties` 文件中配置 Config Server 的地址和客户端的名称:

```yaml
spring:
  application:
    name: your-client-name
  cloud:
    config:
      uri: http://localhost:8888 # Config Server 的地址
      profile: dev # 客户端使用的配置文件的 profile
```

### 步骤 5: 启用 Bus

在 Config Server 和客户端应用中配置 Bus。对于使用 RabbitMQ 的情况,在 `application.yml` 中添加:

```yaml
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
```

### 步骤 6: 刷新端点

确保客户端应用中开启了刷新端点。在 `application.properties` 中添加:

```properties
management.endpoints.web.exposure.include=refresh,bus-refresh
```

### 步骤 7: 触发刷新

当 Config Server 上的配置文件更新后,你可以通过发送 POST 请求到 `/actuator/bus-refresh` 端点来触发所有客户端的配置刷新:

```sh
curl -X POST http://localhost:8080/actuator/bus-refresh
```

### 步骤 8: 使用 @RefreshScope

在客户端应用中,使用 `@RefreshScope` 注解来标记那些需要动态刷新的 Beans。当配置文件更改并触发刷新时,这些 Beans 将重新加载。

### 步骤 9: 测试

更改 Git 仓库中的配置文件,然后触发刷新操作,观察客户端应用的配置是否更新。

通过这些步骤,你可以实现 Spring Cloud Config 和 Bus 的基本使用,以支持配置文件的动态更新。记得根据你的具体环境和需求调整配置和步骤。
 

http://www.lryc.cn/news/429258.html

相关文章:

  • Qt:Qt背景
  • 【数据结构】选择排序
  • 国产GD32单片机开发入门(二)GD32单片机详解
  • 8个我平时每天都会看的网站,涵盖办公、娱乐、学习等
  • Vue2——父子之间间的调用
  • xfs Vs ext4?
  • 数据结构stack (笔记)
  • SQL - 创建 表和数据库
  • 使用 Arch Linux 几个月有感 | 为什么我选择 Arch Linux ,Arch 的优缺点有什么 | 一些Linux发行版推荐
  • SQLserver中的增删改查和数据类型
  • 个人收藏个性化、实用性、可玩性在线网站持续更新,与君共享
  • win10蓝牙只能发送,无法接收
  • 【论文阅读03】用于海洋物体检测的多注意力路径聚合网络
  • Linux 进程(2)
  • [CSCCTF 2019 Qual]FlaskLight1
  • layui table表单 checkbox选中一个其它也要选中
  • 【pip镜像设置】pip使用清华镜像源安装
  • c++ 智能指针--std::shared_ptr
  • 网络工程师学习笔记(二)
  • 90.WEB渗透测试-信息收集-Google语法(4)
  • 阿里Qwen2开源大模型本地部署及调试全攻略
  • 『功能项目』移动后的光标显示【04】
  • HTML 基本语法特性与 title 标签介绍
  • CSS的:placeholder-shown伪类:精确控制输入框占位符样式
  • Java之HashMap的底层实现
  • 多张图片进行模型重建并转换为OBJ模型
  • 信息安全保证人员CISAW:安全集成
  • 别再无效清理微信内存啦,这才是正确清理内存的方式
  • ant design 的 tree 如何作为角色中的权限选择之一
  • 如何在项目管理中完成项目立项?