- 配置中心的作用
- Spring Cloud Config 简介
- Spring Cloud Config 使用
配置中心的作用
经过前文讲解,微服务架构已经日趋完善——现在已经可以做一个大型的应用了!然而,随着项目的迭代,微服务数目往往与日俱增,如何高效地管理配置成为我们必须解决的问题。Spring Cloud 为我们提供了解决方案——Spring Cloud Config,它能够帮助我们解决哪些问题?
- 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的;
- 不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布和生产等)中是不同的;
- 运行期间可动态调整。例如,我们可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整配置时不停止微服务;
- 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。
Spring Cloud Config 简介
Spring Cloud Config 为分布式系统外部化配置提供了服务器端和客户端的支持,它包括 Config Server 和 Config Client 两部分。由于 Config Server 和 Config Client 都实现了对 Spring Environment 和 PropertySource 抽象的映射,因此,Spring Cloud Config 非常适合 Spring 应用程序,当然也可与任何其他语言编写的应用程序配合使用。
Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用 Git 存储配置内容(也可使用 Subversion、MySQL、本地文件系统或 Vault 存储配置,本博客以 Git 为例进行讲解),因此可以很方便地实现对配置的版本控制与内容审计。
Config Client 是 Config Server 的客户端,用于操作存储在 Config Server 中的配置属性。引入 Spring Cloud Config 后的架构如下图所示:
我们的微服务会通过 Http 向 Config Server 发起请求以获取配置,Config Server 则从 Git 仓库拉取配置文件并返回。
开始使用
git仓库地址:https://github.com/LIZEJU/MicroService-Study-Samples/tree/master
microservice-consumer-movie-dev.properties
:
timeout = 1000 key1 = test key2 = hello world
创建项目microservice-config-server
pom.xml
spring-cloud-config-server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.lzj1234</groupId> <artifactId>microservice-config-server</artifactId> <version> 1.0 -SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.0 . 7 .RELEASE</version> <relativePath/> </parent> <properties> <maven.compiler.source> 8 </maven.compiler.source> <maven.compiler.target> 8 </maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
- 在启动主类
App.java
上面添加注解@EnableConfigServer
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.lzj1234; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class App { public static void main(String[] args) { SpringApplication.run(App. class ,args); } <br><br> } |
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | server: port: 9999 spring: application: name: microservice-config-server cloud: config: server: git: # Git仓库地址 uri: https: //github.com/LIZEJU/MicroService-Study-Samples # Git仓库账号 username: m18611694189 @163 .com # Git仓库密码 password: xxxxxxx |
运行app.java
访问:
C:\Users\l\javademo\microservice-config-server>curl http://localhost:9999/microservice-consumer-movie-dev.properties
key1: test
key2: hello world
timeout: 1000
1 2 3 4 5 6 7 8 | 路径规则 Spring Cloud Config Server 提供了 RESTful API,可用来访问存放在 Git 仓库中的配置文件,其中的{appliation}、{profile} 和 {label} 都是占位符。 /{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties |
集成 Config Client 端
1 2 3 4 5 | Maven 中引入 Config Client 的依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> |
resources
添加配置文件:**bootstrap.yml
**
1 2 3 4 5 6 7 8 | spring: application: name: microservice-consumer-movie-ribbon # 对应config server所获取的配置文件的{application} cloud: config: uri: http: //localhost:9999/ # config server的地址 profile: dev # profile对应config server所获取的配置文件中的{profile} label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label} |
github仓库,添加对应的配置文件
microservice-consumer-movie-ribbon-dev.properties
1 2 3 4 | name = pwd shell = true timeout= 999999 key1= 'test1212131232' |
运行客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | C:\Users\l\javademo\microservice-config-server>curl http: //localhost:9999/microservice-consumer-movie-dev.properties key1: test key2: hello world timeout: 1000 C:\Users\l\javademo\microservice-config-server>curl http: //localhost:9999/microservice-consumer-movie-ribbon-dev.properties key1: 'test1212131232' name: pwd shell: true timeout: 999999 C:\Users\l\javademo\microservice-config-server>curl http: //localhost:9999/microservice-consumer-movie-ribbon-dev.properties key1: 'test1212131232' name: pwd shell: true timeout: 999999 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现