Spring Cloud Config 使用教程
项目介绍
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 组件一起使用,构建完整的微服务架构。
鸿蒙开发工具大赶集
本仓将收集和展示鸿蒙开发工具,欢迎大家踊跃投稿。通过pr附上您的工具介绍和使用指南,并加上工具对应的链接,通过的工具将会成功上架到我们社区。017inference
通过更改一行代码,您可以在应用程序中用另一个大型语言模型(LLM)替换OpenAI GPT。Xinference赋予您使用任何所需LLM的自由。借助Xinference,您能够在云端、本地、甚至笔记本电脑上运行任何开源语言模型、语音识别模型和多模态模型的推理。Python00每日精选项目
🔥🔥 每日精选已经升级为:【行业动态】,快去首页看看吧,后续都在【首页 - 行业动态】内更新,多条更新哦~🔥🔥 每日推荐行业内最新、增长最快的项目,快速了解行业最新热门项目动态~~033BootstrapAdmin
基于 RBAC 的 Net8 后台管理框架,权限管理,前后台分离,支持多站点单点登录,兼容所有主流浏览器,内置微信、支付宝、QQ等多种登录方式,内置多种样式,可切换至 Blazor 多 Tabs 模式,权限控制细化到网页内任意元素(按钮、表格、文本框等等)C#00phalapi
PhalApi开源接口框架,简称π框架,一个轻量级PHP开源接口框架,专注于接口服务开发。接口,从简单开始!Objective-C00Cangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。Cangjie062毕方Talon工具
本工具是一个端到端的工具,用于项目的生成IR并自动进行缺陷检测。Python040PDFMathTranslate
PDF scientific paper translation with preserved formats - 基于 AI 完整保留排版的 PDF 文档全文双语翻译,支持 Google/DeepL/Ollama/OpenAI 等服务,提供 CLI/GUI/DockerPython06mybatis-plus
mybatis 增强工具包,简化 CRUD 操作。 文档 http://baomidou.com 低代码组件库 http://aizuda.comJava03国产编程语言蓝皮书
《国产编程语言蓝皮书》-编委会工作区018
热门内容推荐
最新内容推荐
项目优选









