Spring Cloud Config
参考个人项目
参考个人项目 : (希望大家能给个star~)
https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B5
什么是 Spring Cloud Config?
配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
场景介绍:在一个 Application
中,很经常需要连接资源和其它应用,经常有很多需要外部设置的信息去调整Application行为。我们在实际开发应用中的会经常见到的xml、properties、yaml等就是配置信息,但这种做法有一定的缺陷:每次更新需要重新打包和重启。
创建 Spring Cloud Config Server(Git 存储)
-
这里我用了我原本的”服务注册中心”(Eureka Server),将其改造为”配置中心”(Config Server)。
-
引入依赖
1 2 3 4 5 | <!-- Config Server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> |
- 在启动主类添加
@EnableConfigServer
注解,开启 Config Server。
1 2 3 4 5 6 7 8 | @SpringBootApplication @EnableEurekaServer @EnableConfigServer public class MySpringCloudApplication { public static void main(String[] args) { SpringApplication.run(MySpringCloudApplication. class , args); } } |
-
在Github上创建一个项目,并在其中添加配置文件 config-client.properties,在里面添加一个属性
config=hello world !
。 -
在
application.properties
中配置服务信息以及git信息(这里不包括了 Eureka Server 的配置,自行补充)
1 2 3 4 5 | server.port= 8761 spring.cloud.config.server.git.uri=https: //github.com/FunriLy/springcloud-study/ spring.cloud.config.server.git.searchPaths=config-repo spring.cloud.config.server.git.username=Username spring.cloud.config.server.git.password=Password |
- 启动工程 Config Server。访问 http://localhost:8761/config-client/default/ (关于这个URL请参考附录),可以看到以下配置信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { "name" : "config-client" , "profiles" : [ "default" ], "label" : null , "version" : "af7ce2a15dcdea9dab42e6b44d37e401072382d8" , "propertySources" : [ { "name" : "https://github.com/FunriLy/springcloud-study/config-repo/config-client.properties" , "source" : { "configword" : "hello world !" } } ] } |
创建一个Spring Cloud Config Client
-
这里我用到了原来的”服务提供者”(Eureka Client),将其改造为 Config Client。
-
在
resource
下创建bootstrap.properties
,并设置信息,具体如下:
1 2 3 4 | spring.application.name=config-client spring.cloud.config.profile= default spring.cloud.config.label=master spring.cloud.config.uri=http: //localhost:8761/ |
1 2 | 注意这里是bootstrap.properties而不是appliction.properties。 因为bootstrap.properties会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动 |
- 创建一个Controller来进行测试。
1 2 3 4 5 6 7 8 9 10 11 | @RestController public class ConfigController { @Value ( "${configword}" ) String configword; @RequestMapping ( "/config" ) public String printfConfig(){ return "The Config Word Is : " +configword; } } |
- 启动 Config Client,访问 http://localhost:1111/config 。就能看到:
1 | The Config Word Is : hello world ! |
附录
来源于 Spring Cloud Config 中文文档。
URL与配置文件的映射关系
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
application是SpringApplication
的spring.config.name
,(一般来说’application’是一个常规的Spring Boot应用),profile是一个active的profile(或者逗号分隔的属性列表),label是一个可选的git标签(默认为”master”)。
比如,我的文件名是”config-client”,一般在Github上都是default环境,默认为master分支。所以就是/config-client/default/master
Config Server 配置文件
- spring.cloud.config.server.git.uri:配置git仓库位置
- spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
Config Client 配置文件
- spring.application.name:对应前配置文件中的{application}部分
- spring.cloud.config.profile:对应前配置文件中的{profile}部分
- spring.cloud.config.label:对应前配置文件的git分支
- spring.cloud.config.uri:配置中心的地址
其他
在Config Server中,还有一种不使用Git的”native”的配置方式,这种方式是从本地classpath 或文件系统中加载配置文件(使用 “spring.cloud.config.server.native.searchLocations”配置项进行设置)。 加载Config Server 的”spring.profiles.active=native”配置项可以开启native配置。如:
1 2 | spring.profiles.active= native spring.cloud.config.server. native .searchLocations=file:D:/properties |
注意 : 牢记使用file:
前缀来指示资源(默认没有前缀是从classpath中去文件)。也可以嵌入${}
环境参数占位符,但是windows系统下使用绝对路径,前缀后面需要多加个”/”。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具