首页
/ Spring Cloud Config 使用教程

Spring Cloud Config 使用教程

2024-08-07 19:07:30作者:冯爽妲Honey
spring-cloud-config
External configuration (server and client) for Spring Cloud

项目介绍

Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,适用于 Spring Cloud 项目。它通过简单的配置即可实现功能,支持将配置文件存储在多种存储系统中,如 Git、SVN、数据库等。Spring Cloud Config 提供了一种集中式管理配置文件的方式,使得配置的修改和更新更加方便和高效。

项目快速启动

以下是快速启动 Spring Cloud Config 的步骤,假设我们使用 GitHub 作为配置存储。

1. 创建 GitHub 仓库

在 GitHub 上创建一个新的仓库,用于存储配置文件。例如,创建一个名为 config-repo 的仓库。

2. 添加配置文件

config-repo 仓库中添加配置文件,例如 application.yml

# application.yml
spring:
  profiles: dev
data:
  env: dev
  user:
    username: dev-user
    password: dev-password

3. 创建 Spring Boot 项目

创建一个新的 Spring Boot 项目,并添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

4. 配置 Spring Cloud Config Server

application.yml 中配置 Spring Cloud Config Server:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-username/config-repo.git
          username: your-github-username
          password: your-github-password
server:
  port: 8888

5. 启用 Config Server

在启动类上添加 @EnableConfigServer 注解:

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);
    }
}

6. 启动服务

启动 Spring Boot 应用,访问 http://localhost:8888/application/default,应该能看到配置文件的内容。

应用案例和最佳实践

案例一:多环境配置管理

在实际开发中,通常需要为不同的环境(如开发、测试、生产)维护不同的配置。Spring Cloud Config 可以轻松实现这一点。

配置文件示例

config-repo 仓库中添加多个配置文件:

# application-dev.yml
data:
  env: dev
  user:
    username: dev-user
    password: dev-password

# application-prod.yml
data:
  env: prod
  user:
    username: prod-user
    password: prod-password

客户端配置

在客户端应用中,通过设置 spring.profiles.active 来选择不同的配置文件:

spring:
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888

案例二:动态刷新配置

Spring Cloud Config 支持动态刷新配置,无需重启应用即可使配置生效。

添加 Actuator 依赖

在客户端应用中添加 Actuator 依赖:

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

暴露刷新端点

application.yml 中暴露刷新端点:

management:
  endpoints:
    web:
      exposure:
        include: "refresh"

刷新配置

通过 POST 请求 http://localhost:8080/actuator/refresh 来刷新配置。

典型生态项目

Spring Cloud Config 通常与其他 Spring Cloud 组件一起使用,构建完整的微服务架构。

spring-cloud-config
External configuration (server and client) for Spring Cloud
热门项目推荐
相关项目推荐

项目优选

收起
CangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
669
0
RuoYi-Vue
🎉 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统,同时提供了 Vue3 的版本
Java
136
18
openHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
10
4
redis-sdk
仓颉语言实现的Redis客户端SDK。已适配仓颉0.53.4 Beta版本。接口设计兼容jedis接口语义,支持RESP2和RESP3协议,支持发布订阅模式,支持哨兵模式和集群模式。
Cangjie
322
26
advanced-java
Advanced-Java是一个Java进阶教程,适合用于学习Java高级特性和编程技巧。特点:内容深入、实例丰富、适合进阶学习。
JavaScript
75.83 K
19.04 K
qwerty-learner
为键盘工作者设计的单词记忆与英语肌肉记忆锻炼软件 / Words learning and English muscle memory training software designed for keyboard workers
TSX
15.56 K
1.44 K
Jpom
🚀简而轻的低侵入式在线构建、自动部署、日常运维、项目监控软件
Java
1.41 K
292
Yi-Coder
Yi Coder 编程模型,小而强大的编程助手
HTML
30
5
easy-es
Elasticsearch 国内Top1 elasticsearch搜索引擎框架es ORM框架,索引全自动智能托管,如丝般顺滑,与Mybatis-plus一致的API,屏蔽语言差异,开发者只需要会MySQL语法即可完成对Es的相关操作,零额外学习成本.底层采用RestHighLevelClient,兼具低码,易用,易拓展等特性,支持es独有的高亮,权重,分词,Geo,嵌套,父子类型等功能...
Java
1.42 K
231
taro
开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
TypeScript
35.34 K
4.77 K