- 配置中心的作用
- 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
<?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
:
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); }
}
application.yml
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:Usersljavademomicroservice-config-server>curl http://localhost:9999/microservice-consumer-movie-dev.properties
key1: test
key2: hello world
timeout: 1000
路径规则 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 端
Maven 中引入 Config Client 的依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
resources
添加配置文件:**bootstrap.yml
**
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
name = pwd shell = true timeout= 999999 key1='test1212131232'
运行客户端
C:Usersljavademomicroservice-config-server>curl http://localhost:9999/microservice-consumer-movie-dev.properties key1: test key2: hello world timeout: 1000 C:Usersljavademomicroservice-config-server>curl http://localhost:9999/microservice-consumer-movie-ribbon-dev.properties key1: 'test1212131232' name: pwd shell: true timeout: 999999 C:Usersljavademomicroservice-config-server>curl http://localhost:9999/microservice-consumer-movie-ribbon-dev.properties key1: 'test1212131232' name: pwd shell: true timeout: 999999