首页
/ Spring Cloud Config 使用教程

Spring Cloud Config 使用教程

2024-08-07 19:07:30作者:冯爽妲Honey

项目介绍

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 组件一起使用,构建完整的微服务架构。

登录后查看全文
热门项目推荐
相关项目推荐

项目优选

收起
kernelkernel
deepin linux kernel
C
22
6
docsdocs
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
161
2.05 K
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
8
0
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
146
191
leetcodeleetcode
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
60
16
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
198
279
apintoapinto
基于golang开发的网关。具有各种插件,可以自行扩展,即插即用。此外,它可以快速帮助企业管理API服务,提高API服务的稳定性和安全性。
Go
22
0
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
949
556
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
96
15
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
346
1.33 K