什么是配置中心

配置中心架构

image.png

SpringCloudConfig概述

Spring Cloud Config是SpringCloud微服务体系中的配置中心,是一个集中化外部配置的分布式系统,有服务端和客户端组成,不依赖注册中心,是一个独立的配置中心。SpringCloudConfig支持多种存储配置信息的形式,目前主要有jdbc,Vault,git等,默认为git. 基于Git的配置架构如下:
image.png

入门使用

ConfigServer

添加Maven依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springcloud-server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-cloud-server</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2021.0.1</spring-cloud.version>
  19. </properties>
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>${spring-cloud.version}</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. </dependencies>
  30. </dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-logging</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-web</artifactId>
  39. <exclusions>
  40. <exclusion>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-logging</artifactId>
  43. </exclusion>
  44. </exclusions>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-devtools</artifactId>
  49. <scope>runtime</scope>
  50. <optional>true</optional>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-actuator</artifactId>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-test</artifactId>
  59. <scope>test</scope>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework.cloud</groupId>
  63. <artifactId>spring-cloud-config-server</artifactId>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>

Git仓库配置文件

image.png
文件名称说明:文件名称包含项目名称与环境profile,一个仓库下可添加多个项目的配置文件,支持以下形式

  • {applicationName}-{profile}.yml
  • {applicationName}-{profile}.properties

    添加Git仓库配置

    ```properties

    logging.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 登录密码

  1. search-paths: 可搜索当前仓库下多个文件
  2. <a name="yxa9f"></a>
  3. #### 添加@EnableConfigServer
  4. ```java
  5. @SpringBootApplication
  6. @EnableConfigServer
  7. @Configuration
  8. public class ConfigServerApplication implements ApplicationRunner {
  9. public static void main(String[] args) {
  10. SpringApplication.run(ConfigServerApplication.class, args);
  11. }
  12. @Override
  13. public void run(ApplicationArguments args) throws Exception {
  14. System.out.println("ConfigServerApplication started.");
  15. }
  16. }

启动服务后,Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml /{label}
  3. /{application}-{profile}.yml
  4. /{application}-{profile}.properties /{label}
  5. /{application}-{profile}.properties

{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如上述文件的cloud-client部分。
{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。
{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
所以基于上述访问规则,访问示例如下:

  1. http://localhost:8888/cloud-client/dev/master
  2. http://localhost:8888/cloud-client/test/master
  3. http://localhost:8888/cloud-client-test.properties

ConfigClient

添加Maven配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springcloud-demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-cloud-demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <!--aka Jubilee-->
  19. <spring.cloud-version>2021.0.1</spring.cloud-version>
  20. </properties>
  21. <dependencyManagement>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-dependencies</artifactId>
  26. <version>${spring.cloud-version}</version>
  27. <type>pom</type>
  28. <scope>import</scope>
  29. </dependency>
  30. </dependencies>
  31. </dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-devtools</artifactId>
  40. <scope>runtime</scope>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.projectlombok</groupId>
  50. <artifactId>lombok</artifactId>
  51. </dependency>
  52. <!-- config client -->
  53. <dependency>
  54. <groupId>org.springframework.cloud</groupId>
  55. <artifactId>spring-cloud-starter-config</artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.cloud</groupId>
  59. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-starter-actuator</artifactId>
  64. </dependency>
  65. <!-- 自动配置加载 -->
  66. <dependency>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-actuator-autoconfigure</artifactId>
  69. </dependency>
  70. </dependencies>
  71. <build>
  72. <plugins>
  73. <plugin>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-maven-plugin</artifactId>
  76. </plugin>
  77. </plugins>
  78. </build>
  79. </project>

应用配置

  1. server:
  2. port: 8080
  3. spring:
  4. cloud:
  5. config:
  6. refreshInterval: 30
  7. management:
  8. endpoint:
  9. shutdown:
  10. enabled: false
  11. endpoints:
  12. web:
  13. exposure:
  14. include: "*"
  1. spring:
  2. profiles:
  3. active: test
  4. cloud:
  5. config:
  6. uri: http://localhost:8888
  7. name: cloud-client -- 应用名称
  8. label: master
  9. profile: test

获取远程配置

  1. @Component
  2. @Data
  3. @RefreshScope
  4. public class ConfigInfoProperties {
  5. @Value("${spring.cloud.config.server.name}")
  6. private String name;
  7. @Value("${spring.cloud.config.server.greeting}")
  8. private String greeting;
  9. }
  10. @RestController
  11. public class PropertyController {
  12. @Resource
  13. private ConfigInfoProperties configInfoProperties;
  14. @RequestMapping("/getProp")
  15. public String getProp() {
  16. return configInfoProperties.toString();
  17. }
  18. }

配置变更自动刷新

  1. /**
  2. * 自动刷新配置的AutoConfiguration
  3. * @author XieLe on 2022/4/9
  4. */
  5. @ConditionalOnClass(RefreshEndpoint.class)
  6. @ConditionalOnProperty("spring.cloud.config.refreshInterval")
  7. @AutoConfigureAfter(RefreshAutoConfiguration.class)
  8. @Configuration
  9. public class ConfigClientRefreshAutoConfiguration implements SchedulingConfigurer {
  10. /**
  11. * refresh interval
  12. */
  13. @Value("${spring.cloud.config.refreshInterval}")
  14. private int refreshInterval = 60;
  15. @Autowired
  16. private RefreshEndpoint refreshEndpoint;
  17. @Override
  18. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  19. final int refreshInterval = this.refreshInterval;
  20. taskRegistrar.addFixedDelayTask(new IntervalTask(new Runnable() {
  21. @Override
  22. public void run() {
  23. refreshEndpoint.refresh();
  24. }
  25. }, refreshInterval));
  26. }
  27. /**
  28. * 如果Application上下文中没有注册, 则注册调度器
  29. */
  30. @ConditionalOnMissingBean(ScheduledAnnotationBeanPostProcessor.class)
  31. @EnableScheduling
  32. @Configuration
  33. protected static class EnableScheduleConfig {
  34. }
  35. }