Spring Cloud系列(二十七) 分布式配置中心Spring Cloud Config—Finchley版本

迷南。 2022-05-10 01:04 280阅读 0赞

Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。

在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。

配置服务端

准备配置仓库

准备一个git仓库,可以在码云或Github上创建都可以,这是我的git仓库,具体如何创建git仓库可以参考我这篇文章《创建GitHub仓库并与本地Git绑定》。

https://github.com/WYA1993/spring_cloud_config_demo

构建配置中心

第一步,构建一个Spring Boot项目,命名config-server-vFinchley.RC2,并引入如下依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.2.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  10. <java.version>1.8</java.version>
  11. <spring-cloud.version>Finchley.RC2</spring-cloud.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-web</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-config-server</artifactId>
  26. </dependency>
  27. </dependencies>

第二步,创建Application主类,添加@EnableConfigServer注解,开启Spring Cloud Config的服务端功能。

  1. @EnableConfigServer
  2. @SpringBootApplication
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

第三步,创建application.yml配置文件

  1. spring:
  2. application:
  3. name: config-server
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://github.com/WYA1993/spring_cloud_config_demo
  9. search-paths:
  10. - config/config_repo/
  11. server:
  12. port: 5666

其中git的配置信息如下:

spring.cloud.config.server.git.uri:配置git仓库位置

spring.cloud.config.server.git.search-paths:配置仓库路径下的相对搜索位置(远程仓库中配置文件所在路径),可以配置多个,如果没有配置此属性,会默认在仓库根路径找,如果你的配置文件在根路径下的目录内会找不到。

如果Git仓库需要权限访问(公开仓库不需要用户名密码),那么可以通过配置下面的两个属性来实现:

spring.cloud.config.server.git.username:访问Git仓库的用户名

spring.cloud.config.server.git.password:访问Git仓库的用户密码

第四步,启动项目,确保没有报错。

第五步,为了验证上面的分布式配置中心,在git仓库https://github.com/WYA1993/spring_cloud_config_demo/config/下创建config-repo目录作为配置仓库,并为不同环境新建四个配置文件:

70

四个配置文件的内容依次为:

form=git-dev-1.0

form=git-prod-1.0

form=git-test-1.0

form=git-deafult-1.0

为了测试版本控制,加入一个新的分支config_label_test,创建上面的文件,只是将form属性的值改为2.0后缀。

第六步,接下来就可以访问这些配置内容了。访问配置信息的URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

解释一下上面的映射关系

  • / { 应用名 } / { 环境名 } [ / { 分支名 } ]
  • / { 应用名 } - { 环境名 }.yml
  • / { 应用名 } - { 环境名 }.properties
  • / { 分支名 } / { 应用名 } - { 环境名 }.yml
  • / { 分支名 } / { 应用名 } - { 环境名 }.properties

{application}通常是客户端微服务的应用名,对应Git仓库中文件名的前缀如repo-dev.properties的application就是repo;
{profile}对应{application}-后面的dev、pro、test等;
{label}对应Git仓库的分支名,默认为master。

注意:

  1. 第一个规则的分支名是可以省略的,默认是master分支
  2. label也可以叫做提交id、分支名称或者tag
  3. 无论你的配置文件是properties,还是yml,只要是应用名+环境名能匹配到这个配置文件,那么就能取到
  4. 如果是想直接定位到没有写环境名的默认配置,那么就可以使用default去匹配没有环境名的配置文件,如http://localhost:5666/repo/default/master
  5. 使用第一个规则会匹配到默认配置
  6. 如果直接使用应用名来匹配,会出现404错误,此时可以加上分支名匹配到默认配置文件
  7. 如果配置文件的命名很由多个-分隔,此时直接使用这个文件名去匹配的话,会出现直接将内容以源配置文件内容直接返回,内容前可能会有默认配置文件的内容(已测试)

我们可以尝试构造不同的url来访问不同的配置内容,比如,要访问master分支,repo应用的test环境,就可以访问这个url:http://localhost:5666/repo/test/master,并获得如下返回:

70 1

其余四种映射的url示例(这四种是直接返回文件的内容)

http://localhost:5666/config_label_test/repo-dev.yml —form: git-dev-2.0

http://localhost:5666/repo-dev.yml —form: git-dev-1.0

http://localhost:5666/repo-dev.properties —form: git-dev-1.0

http://localhost:5666/config_label_test/repo-dev.properties —form: git-dev-2.0

注意:

1.如果propertySources值是空的,看一下你的配置文件是否存在,文件存在的话就看你application.yml的配置路径是否写对。

2.如果你报这个错:

  1. springboot: Finchley.RC2 Cannot load environment] with root cause
  2. java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  3. at java.lang.String.substring(String.java:1967) ~[na:1.8.0_131]
  4. at org.springframework.cloud.config.server.environment.HttpClientConfigurableHttpConnectionFactory.lookupHttpClientBuilder(HttpClientConfigurableHttpConnectionFactory.java:69) ~[spring-cloud-config-server-2.0.0.RC2.jar:2.0.0.RC2]
  5. at org.springframework.cloud.config.server.environment.HttpClientConfigurableHttpConnectionFactory.create(HttpClientConfigurableHttpConnectionFactory.java:56) ~[spring-cloud-config-server-2.0.0.RC2.jar:2.0.0.RC2]
  6. at org.eclipse.jgit.transport.TransportHttp.httpOpen(TransportHttp.java:830) ~[org.eclipse.jgit-4.11.0.201803080745-r.jar:4.11.0.201803080745-r]
  7. at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:491) ~[org.eclipse.jgit-4.11.0.201803080745-r.jar:4.11.0.201803080745-r]
  8. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  9. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
  10. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  11. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  12. at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158) ~[spring-boot-actuator-2.0.3.RELEASE.jar:2.0.3.RELEASE]
  13. at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126) ~[spring-boot-actuator-2.0.3.RELEASE.jar:2.0.3.RELEASE]
  14. at
  15. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
  16. at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

解决方法:是Finchley.RC2版本问题,改成Finchley.RC1即可。

我们可以看到该JSON返回了应用名repo,环境名test,分支名master,以及default环境和test环境的配置内容。另外version对应得是Git上的commit号。同时在控制台还输出了如下内容。

  1. 2018-09-25 11:36:02.144 INFO 3016 --- [nio-5666-exec-5] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@58e3d589: startup date [Tue Sep 25 11:36:02 CST 2018]; root of context hierarchy
  2. 2018-09-25 11:36:02.153 INFO 3016 --- [nio-5666-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-8649563955543866185/config/config_repo/repo-test.properties
  3. 2018-09-25 11:36:02.153 INFO 3016 --- [nio-5666-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-8649563955543866185/config/config_repo/repo.properties
  4. 2018-09-25 11:36:02.153 INFO 3016 --- [nio-5666-exec-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@58e3d589: startup date [Tue Sep 25 11:36:02 CST 2018]; root of context hierarchy

配置服务器在从Git中获取配置信息后,会存储一份在config-server的文件系统中,实质上config-server是通过git clone命令将配置内容复制了一份在本地存储,然后读取这些内容并返回给微服务应用进行加载。

config-server通过Git在本地仓库暂存,可以有效防止当Git仓库出现故障而引起无法加载配置信息的情况。我们可以断开网络再次发起http://localhost:5666/repo/test/master请求,在控制台输出了如下内容,但是它依然会返回配置内容,这些内容源自于之前访问时存于config-server的本地文件系统中的配置内容。

  1. 2018-09-25 11:46:53.166 WARN 3016 --- [nio-5666-exec-8] .c.s.e.MultipleJGitEnvironmentRepository : Could not fetch remote for master remote: https://github.com/WYA1993/spring_cloud_config_demo
  2. 2018-09-25 11:46:53.479 INFO 3016 --- [nio-5666-exec-8] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@81e8011: startup date [Tue Sep 25 11:46:53 CST 2018]; root of context hierarchy
  3. 2018-09-25 11:46:53.487 INFO 3016 --- [nio-5666-exec-8] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-8649563955543866185/config/config_repo/repo-test.properties
  4. 2018-09-25 11:46:53.487 INFO 3016 --- [nio-5666-exec-8] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-8649563955543866185/config/config_repo/repo.properties
  5. 2018-09-25 11:46:53.487 INFO 3016 --- [nio-5666-exec-8] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@81e8011: startup date [Tue Sep 25 11:46:53 CST 2018]; root of context hierarchy

构建客户端

完成上述验证后,确定了配置服务中心已经正常工作,下面尝试如何在微服务应用中获取上述配置信息。

第一步,创建Spring Boot项目,命名config-client-vFinchley.RC2,引入如下依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.2.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  10. <java.version>1.8</java.version>
  11. <spring-cloud.version>Finchley.RC1</spring-cloud.version>
  12. </properties>
  13. <dependencies>
  14. <!-- web的依赖,必须加 -->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <!--Spring Boot Actuator,感应服务端变化 -->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-actuator</artifactId>
  28. </dependency>
  29. <!--Spring Cloud Config 客户端依赖 -->
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-config-server</artifactId>
  33. </dependency>
  34. </dependencies>

第二步,创建应用主类

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }

第三步,创建bootstrap.yml配置文件

  1. spring:
  2. application:
  3. name: repo #要获取的应用名
  4. cloud:
  5. config:
  6. uri: http://localhost:5666/ # 配置服务中心的地址 默认值http://localhost:8888
  7. label: master #要获取的分支名
  8. profile: dev #要获取的环境名
  9. fail-fast: true #没有读取成功则执行快速失败
  10. server:
  11. port: 5777

上述配置参数与Git中存储的配置文件中各个部分的对应关系如下:

  • spring.application.name:对应配置文件规则中的{application}部分
  • spring.cloud.config.profile:对应配置文件规则中的{profile}部分
  • spring.cloud.config.label:对应配置文件规则中的{label}部分
  • spring.cloud.config.uri:配置中心config-server的地址
  • fail-fast:没有读取成功则执行快速失败

这里需要格外注意:上面这些属性必须配置在bootstrap.yml或者properties文件中中,这样config-server中的配置信息才能被正确加载。为何需要把 config server 的信息放在 bootstrap.yml 里?bootstrap.yml 先于 application.yml 加载,当使用 Spring Cloud 的时候,配置信息一般是从 config server 加载的,为了取得配置信息(比如密码等),你需要一些提早的或引导配置。因此,把 config server 信息放在 bootstrap.yml,用来加载真正需要的配置信息。

关于bootstrap.yml 和 application.yml可以看一下这篇文章:《application.yml和 bootstrap.yml区别》

第四步,创建一个RESTful接口返回配置中心的form属性,也就是Git仓库里配置文件里的key,通过@Value(“${form}“)绑定配置服务中心配置的form属性,具体实现如下:

  1. @RefreshScope
  2. @RestController
  3. public class TestController {
  4. @Value("${form}")
  5. private String form;
  6. @GetMapping("test1")
  7. public String getForm(){
  8. return form;
  9. }
  10. }

除了通过@Value注解绑定注入外,也可以通过Environment对象来获取配置属性。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.cloud.context.config.annotation.RefreshScope;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RefreshScope
  8. @RestController
  9. public class TestController {
  10. @Value("${form}")
  11. private String form;
  12. @GetMapping("test1")
  13. public String getForm(){
  14. return form;
  15. }
  16. @Autowired
  17. private Environment env;
  18. @GetMapping("test2")
  19. public String getForm2(){
  20. return env.getProperty("form", "undefined");
  21. }
  22. }

测试

启动客户端,访问http://localhost:5777/test1或者http://localhost:5777/test2,页面返回如下内容git-dev-1.0。

发表评论

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

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

相关阅读