SpringCloud-Config

​核心功能

Config Server/Client架构关系

flowchart TD subgraph Config Server Zone A[Config Server<br/>配置中心] B[(Config Repository<br/>Git/Storage)] end A -- 读取配置 --> B subgraph Client Zone C[Client Microservice A] D[Client Microservice B] E[Client Microservice N] end C -- 启动时/定时拉取配置 --> A D -- 启动时/定时拉取配置 --> A E -- 启动时/定时拉取配置 --> A style A fill:#e1f5fe,stroke:#01579b,stroke-width:2px style B fill:#f3e5f5,stroke:#4a148c,stroke-width:2px style C fill:#f1f8e9,stroke:#33691e,stroke-width:2px style D fill:#f1f8e9,stroke:#33691e,stroke-width:2px style E fill:#f1f8e9,stroke:#33691e,stroke-width:2px

服务端搭建

创建配置仓库
搭建Git仓库https://gitee.com/looyang/config.git

# 创建文件夹springcloud
# 创建文件
config-server-dev.yml
config-server-prod.yml
config-server-test.yml

示例文件

data:
  env: config-server-dev
  user:
    username: QQ
    password: 999

添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 服务端包 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>

添加配置

server:
  port: 3344

spring:
  profiles:
    active: dev
  application:
    name: Config3344  #应用名称
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/looyang/config.git #配置文件所在仓库
          username: git用户名
          password: git密码 
          default-label: master #默认git分支
          search-paths: springcloud  #代码仓中,配置文件所在目录

logging:
  level:
    org.springframework: debug

访问地址

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

eg:
http://localhost:3344/config-server-dev.yml
http://localhost:3344/config-server/dev/master

客户端集成

添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 客户端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

客户端需要注意,依赖跟服务端不一样

添加配置bootstrap.yml

server:
  port: 3355
logging:
  level:
    org.springframework: info

spring:
  application:
    name: CONFIG-CLIENT-3355
  cloud:
    config:
      uri: http://localhost:3344 #配置服务地址
      label: master #分支名
      name: config-server #配置文件名称
      profile: dev #环境

用bootstrap而不是application,是因为bootstrap优先级比application更高,是Spring Application Context的父上下文,不可覆盖

测试客户端

@RestController  
@RequestMapping("/config")  
public class ConfigController {  
    @Value("${data.user.password}")  
    private String password;  
  
    @GetMapping("/getConfigInfo")  
    public String getConfig() {  
        return password;  
    }  
}
curl http://localhost:3355/config/getConfigInfo

配置动态刷新 actuator/refresh
如果修改git仓库中配置文件,直接请求Config Server可以获取最新数据,但是Config Client只能重启生效
有两种方式可以实现客户端刷新

  1. actuator
  2. SpringCloud Bus

通过actuator实现客户端配置刷新

客户端新增依赖

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

新增bootstrap.yml端点配置

management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info

在需要刷新的地方添加@RefreshScope注解

@RestController  
@RequestMapping("/config")  
@RefreshScope  
public class ConfigController {  
    @Value("${data.user.password}")  
    private String password;  
  
    @GetMapping("/getConfigInfo")  
    public String getConfig() {  
        return password;  
    }  
}

测试

# 第一次测试
curl -X GET "http://localhost:3355/config/getConfigInfo"
997
# 修改git配置后测试
curl -X GET "http://localhost:3355/config/getConfigInfo"
997
# 触发手动刷新
curl -X POST "http://localhost:3355/actuator/refresh"
["config.client.version","data.user.password"]
# 重新测试
curl -X GET "http://localhost:3355/config/getConfigInfo"
999

通过actuator必须手动post请求触发刷新,如果是集群的话就要挨个请求

通过SpringCloud Bus实现客户端配置刷新

SpringCloud Bus

​阶段三:安全与进阶​

  1. ​安全防护​

    • 配置HTTP Basic认证
    • 集成OAuth2.0/JWT认证
    • 配置文件权限控制(分支级/文件级)
  2. ​高可用方案​

    • Config Server集群搭建(Eureka注册)
    • 使用Redis实现配置缓存
    • 配置中心监控(Prometheus+Grafana)

​阶段四:实战演练​

  1. ​高级特性​
    • 配置加密解密(RSA对称加密)
    • 本地文件系统存储配置(fallback机制)
    • 集成Spring Cloud Bus实现集群刷新
  2. ​电商项目实战​
    • 搭建商品服务配置中心(商品库存/价格配置)
    • 实现多环境配置切换(开发/测试环境)
    • 配置动态刷新(价格实时调整)
  3. ​最佳实践​
    • 配置文件版本回滚策略
    • 敏感信息脱敏处理
    • 配置中心与Nacos对比选型

​持续学习

posted @ 2025-08-22 18:42  一只盐桔鸡  阅读(42)  评论(0)    收藏  举报