Spring Cloud 之Config详解

13 篇文章 0 订阅
4 篇文章 0 订阅

大家好,我是升仔

  1. 在微服务架构中,统一的配置管理是维护大规模分布式系统的关键。Spring Cloud Config为微服务提供集中化的外部配置支持,它可以与各种源代码管理系统集成,如Git、SVN等。本文将详细介绍如何搭建配置服务器、管理客户端配置和动态刷新配置。

    搭建配置服务器

    基本原理

    Spring Cloud Config Server作为中心化的配置服务器,它从源代码管理系统中读取配置文件,然后提供给客户端应用。原理图如下:
    在这里插入图片描述

    步骤和代码

    1. 添加依赖:在项目的pom.xml文件中添加Config Server的依赖。

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      
    2. 启用Config Server:在应用的主类上添加@EnableConfigServer注解。

      @SpringBootApplication
      @EnableConfigServer
      public class ConfigServerApplication {
          public static void main(String[] args) {
              SpringApplication.run(ConfigServerApplication.class, args);
          }
      }
      
    3. 配置文件:在application.yml中配置Git仓库地址。

      server:
        port: 8888
      
      spring:
        cloud:
          config:
            server:
              git:
                uri: [Git仓库地址]
                clone-on-start: true
      

    客户端配置管理

    配置客户端

    1. 添加依赖:在客户端应用的pom.xml中添加Config Client的依赖。

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      
    2. 配置bootstrap.yml:客户端应用需要在bootstrap.yml文件中指定Config Server的地址。

      spring:
        application:
          name: client-app
        cloud:
          config:
            uri: http://localhost:8888
      

    读取配置

    在客户端应用中,可以直接通过@Value注解读取配置:

    @RestController
    public class ConfigClientController {
    
        @Value("${some.config}")
        private String someConfig;
    
        @GetMapping("/getConfig")
        public String getConfig() {
            return someConfig;
        }
    }
    

    动态刷新配置

    Spring Cloud Config支持在不重启服务的情况下刷新配置。

    1. 添加依赖:在客户端应用中添加spring-boot-starter-actuator依赖。

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      
    2. 使用@RefreshScope:在需要动态刷新的Bean上添加@RefreshScope注解。

      @RestController
      @RefreshScope
      public class ConfigClientController {
          // ...
      }
      
    3. 触发刷新:通过POST请求/actuator/refresh端点来刷新配置。

      curl -X POST http://localhost:8080/actuator/refresh
      
最后说一句(求关注,求赞,别白嫖)

最近无意间获得一份阿里大佬写的刷题笔记,一下子打通了我的任督二脉,进大厂原来没那么难。

这是大佬写的,7701页的BAT大佬写的刷题笔记,让我offer拿到手软

本文已收录于我的技术网站,next-java.com, 有大厂完整面经,工作技术等经验分享

求一键三连:点赞、分享、收藏

点赞对我真的非常重要!在线求赞,加个关注非常感激

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

升仔聊编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值