什么是配置中心
配置中心架构
SpringCloudConfig概述
Spring Cloud Config是SpringCloud微服务体系中的配置中心,是一个集中化外部配置的分布式系统,有服务端和客户端组成,不依赖注册中心,是一个独立的配置中心。SpringCloudConfig支持多种存储配置信息的形式,目前主要有jdbc,Vault,git等,默认为git. 基于Git的配置架构如下:
入门使用
ConfigServer
添加Maven依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springcloud-server</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-server</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><spring-cloud.version>2021.0.1</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
Git仓库配置文件

文件名称说明:文件名称包含项目名称与环境profile,一个仓库下可添加多个项目的配置文件,支持以下形式
- {applicationName}-{profile}.yml
 - {applicationName}-{profile}.properties
添加Git仓库配置
```propertieslogging.config=单独的日志配置文件路径,默认会加载classpath的log4j.xml文件
debug: true server: port: 8888 
Config Server配置
spring: profiles: active: test application: name: configserver cloud: config: server: git: uri: https://gitee.com/scherrer/spring-cloud-demo.git default-label: master search-paths: config username: github 登录账号 password: github 登录密码
search-paths: 可搜索当前仓库下多个文件<a name="yxa9f"></a>#### 添加@EnableConfigServer```java@SpringBootApplication@EnableConfigServer@Configurationpublic class ConfigServerApplication implements ApplicationRunner {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ConfigServerApplication started.");}}
启动服务后,Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。
/{application}/{profile}[/{label}]/{application}-{profile}.yml /{label}/{application}-{profile}.yml/{application}-{profile}.properties /{label}/{application}-{profile}.properties
{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如上述文件的cloud-client部分。
{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。
{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
所以基于上述访问规则,访问示例如下:
http://localhost:8888/cloud-client/dev/masterhttp://localhost:8888/cloud-client/test/masterhttp://localhost:8888/cloud-client-test.properties
ConfigClient
添加Maven配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springcloud-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><!--aka Jubilee--><spring.cloud-version>2021.0.1</spring.cloud-version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud-version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- config client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 自动配置加载 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator-autoconfigure</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
应用配置
server:port: 8080spring:cloud:config:refreshInterval: 30management:endpoint:shutdown:enabled: falseendpoints:web:exposure:include: "*"
spring:profiles:active: testcloud:config:uri: http://localhost:8888name: cloud-client -- 应用名称label: masterprofile: test
获取远程配置
@Component@Data@RefreshScopepublic class ConfigInfoProperties {@Value("${spring.cloud.config.server.name}")private String name;@Value("${spring.cloud.config.server.greeting}")private String greeting;}@RestControllerpublic class PropertyController {@Resourceprivate ConfigInfoProperties configInfoProperties;@RequestMapping("/getProp")public String getProp() {return configInfoProperties.toString();}}
配置变更自动刷新
/*** 自动刷新配置的AutoConfiguration* @author XieLe on 2022/4/9*/@ConditionalOnClass(RefreshEndpoint.class)@ConditionalOnProperty("spring.cloud.config.refreshInterval")@AutoConfigureAfter(RefreshAutoConfiguration.class)@Configurationpublic class ConfigClientRefreshAutoConfiguration implements SchedulingConfigurer {/*** refresh interval*/@Value("${spring.cloud.config.refreshInterval}")private int refreshInterval = 60;@Autowiredprivate RefreshEndpoint refreshEndpoint;@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {final int refreshInterval = this.refreshInterval;taskRegistrar.addFixedDelayTask(new IntervalTask(new Runnable() {@Overridepublic void run() {refreshEndpoint.refresh();}}, refreshInterval));}/*** 如果Application上下文中没有注册, 则注册调度器*/@ConditionalOnMissingBean(ScheduledAnnotationBeanPostProcessor.class)@EnableScheduling@Configurationprotected static class EnableScheduleConfig {}}
