SpringCloud——分布式配置中心(Spring Cloud Config)

你的名字 2021-05-12 11:55 631阅读 0赞

一、Spring Cloud Config简介

  1. Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring `Environment``PropertySource`抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

Spring Cloud Config具有中心化、版本控制、支持动态更新和语言独立等特性。其特点是:

  • 提供服务端和客户端支持(Spring Cloud Config Server和Spring Cloud Config Client);
  • 集中式管理分布式环境下的应用配置;
  • 基于Spring环境,实现了与Spring应用无缝集成;
  • 可用于任何语言开发的程序;
  • 默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理。

Spring Cloud Config的结构图如下:

70

二、创建工程代码测试

1、创建一个eureka注册中心(config-eureka-server)

pom.xml依赖文件:

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example.demo.config</groupId>
  6. <artifactId>config-eureka-server</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>config-eureka-server</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>com.example.demo</groupId>
  13. <artifactId>springcloud-config</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-web</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-config-server</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

配置文件applicatio.yml

  1. server:
  2. port: 8081
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. service-url:
  8. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  9. fetch-registry: false
  10. register-with-eureka: false

启动类添加注解@EnableEurekaServer,代码如下:

  1. package com.example.demo.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class ConfigEurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConfigEurekaServerApplication.class, args);
  10. }
  11. }

2、创建Config Server(config-server)

pom.xml依赖文件:

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example.demo.config</groupId>
  6. <artifactId>config-server</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>config-server</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>com.example.demo</groupId>
  13. <artifactId>springcloud-config</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-config-server</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

application.yml配置文件:

  1. server:
  2. port: 8082
  3. spring:
  4. application:
  5. name: config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://gitee.com/MoCunXiaoPing/springcloud/
  11. search-paths: /**
  12. username:
  13. password:
  14. label: master
  15. eureka:
  16. client:
  17. service-url:
  18. defaultZone: http://localhost:8081/eureka/

配置说明:

  • spring.cloud.config.server.git.uri:配置git仓库地址
  • spring.cloud.config.server.git.searchPaths:配置仓库路径
  • spring.cloud.config.label:配置仓库的分支
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码

注意:如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。

git服务器(码云)上配置文件config-client-dev.properties如下:

  1. foo = foo version 2
  2. foo.message = hello spring io

启动类添加注解@EnableConfigServer开启配置服务器,代码如下:

  1. package com.example.demo.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. @EnableConfigServer
  9. public class ConfigServerApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigServerApplication.class, args);
  12. }
  13. }

3、创建Config Client(config-client)

pom.xml依赖文件:

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example.demo.config</groupId>
  6. <artifactId>config-client</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>config-client</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>com.example.demo</groupId>
  13. <artifactId>springcloud-config</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-config</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>

bootstrap.properties配置文件:

  1. server.port=8083
  2. spring.application.name=config-client
  3. spring.cloud.config.profile=dev
  4. spring.cloud.config.label=master
  5. #spring.cloud.config.uri= http://localhost:8082/
  6. eureka.client.serviceUrl.defaultZone: http://localhost:8081/eureka/
  7. spring.cloud.config.discovery.enabled=true
  8. spring.cloud.config.discovery.serviceId=config-server

配置说明:

  • spring.cloud.config.discovery.enabled 是从配置中心读取文件。
  • spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。
  • spring.cloud.config.label 指明远程仓库的分支
  • spring.cloud.config.profile

    • dev开发环境配置文件
    • test测试环境
    • pro正式环境

注意:因为在springboot是启动的时候才从配置文件中读取配置属性,配置文件在远程配置中心的话,注册中心的信息需要放在bootstrap.properties中才能启动优先读取,放在application.yml会报该异常没发现属性,异常信息如下:

  1. Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'foo' in value "${foo}"
  2. at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  3. at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  4. at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  5. at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  6. at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  8. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  9. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  10. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  11. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  12. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
  13. ... 17 common frames omitted

启动类添加注解@EnableEurekaClient,代码如下:

  1. package com.example.demo.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class ConfigClientApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConfigClientApplication.class, args);
  10. }
  11. }

创建接口API,TestController.java类,代码如下:

  1. package com.example.demo.config.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * 路径:com.example.demo.config.controller
  7. * 类名:
  8. * 功能:《用一句描述一下》
  9. * 备注:
  10. * 创建人:typ
  11. * 创建时间:2018/9/14 10:55
  12. * 修改人:
  13. * 修改备注:
  14. * 修改时间:
  15. */
  16. @RestController
  17. public class TestController {
  18. @Value("${foo}")
  19. String foo;
  20. @GetMapping("/test")
  21. public String test(){
  22. return foo;
  23. }
  24. }

4、启动工程测试

访问:http://localhost:8081/,浏览器显示结果:

70 1

访问:http://localhost:8083/test,浏览器显示结果:

  1. foo version 2

源码下载:https://download.csdn.net/download/typ1805/10667167

发表评论

表情:
评论列表 (有 0 条评论,631人围观)

还没有评论,来说两句吧...

相关阅读