SpringCloud Config 代码实战

为了更好地理解 Spring Cloud Config 的实际应用,我们可以分成两个部分来进行实战演练:首先是设置 Config Server,然后是配置 Config Client。下面我将提供一个简单的示例来演示整个过程。

第一部分:设置 Config Server

1. 创建 Config Server 项目
  1. 创建一个新的 Spring Boot 项目:

    • 使用 Spring Initializr 创建一个新项目。
    • 添加 spring-boot-starter-webspring-cloud-config-server 依赖。
  2. 创建主类:

    • 创建一个 Spring Boot 应用的主类。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    
2. 配置 Config Server
  1. 编辑 application.yml 文件:

    • 配置 Config Server 使用 Git 作为后端存储。
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/your-org/your-config-repo.git
              default-label: main
              search-paths: /config-data
    

    这里假设你已经有一个 Git 仓库,其中包含了一个名为 config-data 的目录。

  2. 启动 Config Server:

    • 运行你的 Config Server 应用程序。

第二部分:设置 Config Client

1. 创建 Config Client 项目
  1. 创建一个新的 Spring Boot 项目:

    • 使用 Spring Initializr 创建一个新项目。
    • 添加 spring-boot-starter-webspring-cloud-starter-config 依赖。
  2. 创建主类:

    • 创建一个 Spring Boot 应用的主类。
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    @RefreshScope
    public class ConfigClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApplication.class, args);
        }
    
        @GetMapping("/config")
        public String getConfig() {
            return "Current configuration is loaded from the Config Server.";
        }
    }
    

    这里的 @RefreshScope 注解用于确保控制器能够响应配置的动态刷新。

2. 配置 Config Client
  1. 编辑 bootstrap.yml 文件:

    • 配置 Config Client 连接到 Config Server。
    spring:
      cloud:
        config:
          uri: http://localhost:8888
          fail-fast: true
          enabled: true
          name: myapp
          profile: dev
          label: main
    
  2. 创建配置文件:

    • 在你的 Git 仓库中创建一个配置文件,例如 myapp-dev.yml
    # myapp-dev.yml
    message: Hello, this is a message from the Config Server.
    
3. 测试 Config Client
  1. 启动 Config Client:

    • 运行你的 Config Client 应用程序。
  2. 测试应用:

    • 访问 http://localhost:8080/config 查看配置是否正确加载。

第三部分:动态刷新配置

1. 添加 Spring Cloud Bus
  1. 添加 Spring Cloud Bus 依赖:

    • 在 Config Client 项目的 pom.xml 文件中添加 spring-cloud-starter-bus-amqp 依赖。
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    
  2. 配置 Spring Cloud Bus:

    • bootstrap.yml 文件中激活 Spring Cloud Bus。
    spring:
      cloud:
        config:
          uri: http://localhost:8888
          fail-fast: true
          enabled: true
          name: myapp
          profile: dev
          label: main
        bus:
          enabled: true
    
2. 配置刷新
  1. 在 Config Server 中添加 Spring Cloud Bus 依赖:

    • 在 Config Server 的 pom.xml 文件中添加 spring-cloud-starter-bus-amqp 依赖。
  2. 配置 Spring Cloud Bus:

    • 在 Config Server 的 application.yml 文件中激活 Spring Cloud Bus。
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/your-org/your-config-repo.git
              default-label: main
              search-paths: /config-data
        bus:
          enabled: true
    
3. 测试配置刷新
  1. 更改配置文件:

    • 更新 Git 仓库中的配置文件,例如更改 message 属性的值。
  2. 触发刷新:

    • 在 Config Server 中,可以通过发送一个 POST 请求到 /refresh 端点来触发刷新事件。
    curl -X POST http://localhost:8888/refresh
    
  3. 验证结果:

    • 再次访问 http://localhost:8080/config,查看配置是否已经刷新。

通过以上步骤,你已经完成了 Spring Cloud Config 的基本设置,包括 Config Server 和 Config Client 的配置,以及如何使用 Spring Cloud Bus 实现动态刷新配置的功能。希望这个实战演练对你有所帮助!如果有任何疑问或需要进一步的帮助,请随时提问。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值