Spring cloud系列二 Spring Cloud 配置中心的基本用法

绝地灬酷狼 2021-06-24 15:57 827阅读 0赞

1. 概述

本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明。
配置中心服务器部分内容包括:服务创建,git,svn,native后端的配置,各种url访问
配置中心客户端部分内容包括:访问配置、failfast,重试

2. Spring Cloud Config的服务端

2.1. 简述

我们在开发大的系统时,由于服务较多,相同的配置(如数据库信息、缓存、开关量等)会出现在不同的服务上,如果一个配置发生变化,则可能需要修改很多的服务配置。为了解决这个问题,spring cloud提供配置中心。
首先所有的公共配置存储在相同的地址(存储的地方可以是git,svn和本地文件),然后配置中心从这些地方读取配置以restful发布出来,其它服务可以调用接口获取配置信息。

2.2. 配置服务

引入关键jar

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. </dependency>

通过@EnableConfigServer可以激活配置中心服务。配置中心可以单独做服务,也可以嵌入到其它服务中。推荐用单独做服务方式使用配置中心。

  1. @SpringBootApplication
  2. @EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务
  3. public class CloudGitConfigServerApplication {
  4. public static void main(String[] args) {
  5. args = new String[1];
  6. args[0] = "--spring.profiles.active=gitsimple2";
  7. SpringApplication.run(CloudGitConfigServerApplication.class, args);
  8. }
  9. }

由于配置文件的存储的多样性,下面介绍每种配置形式如何配置。所有的配置都配置在application-*.yml中

2.3. git后端

Spring Cloud配置中心的后端系统可以是:

  • VCS(如git,svn等)
  • 本地文件

本节我们介绍git配置
配置参数主要配置中application-gitsimple2.yml

  1. spring:
  2. application:
  3. name: special
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. # 配置文件只搜索url目录下的searchPaths
  9. uri: https://github.com/hryou0922/spring_cloud.git
  10. # 指定搜索路径,如果有多个路径则使用,分隔
  11. searchPaths: cloud-config-git/simple2/configspecial,cloud-config-git/simple2/default
  12. # 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件
  13. basedir: /tmp/spring-cloud-repo
  14. # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
  15. force-pull: true

spring.cloud.config.server.git.url:指定配置文件所在远程git库的url地址
spring.cloud.config.server.git.searchPaths:和上面的参数url配合使用,定位git库的子目录。指定搜索路径,如果有多个路径则使用,分隔

spring.cloud.config.server.git.basedir:对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件。默认存储在系统临时目录下,目录名的前缀为config-repo-,如在linux下时可能是/tmp/config-repo-。因为/tmp下的内容有可能被误删,所有为了保险,最好修改存储目录。如果你修改存储目录,你可以修改spring.cloud.config.server.git.basedir

spring.cloud.config.server.git.force-pull:配置中心通过git从远程git库读取数据时,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置。设置force-pull=true,则强制从远程库中更新本地库

以上是一些常用的配置,其它配置可以自己看配置类MultipleJGitEnvironmentRepository类

2.4. svn后端

svn的配置方法和git差不多,主要使用”spring.cloud.config.server.svn.*”。这里略

2.5. 文件系统后端

除了使用从git/svn下载配置文件,你可以从classpath目录或本地文件系统中加载配置文件。通过spring.cloud.config.server.native.searchLocations配置地址.这里又分为两类:

  • 从本地目录加载配置文件:以file开头
  1. * file:///$\{user.home\}/config-repo
  2. 默认值:file:./, file:./config
  • 从classpath中加载配置文件:以classpath开头
  1. * 如果不配置值,则默认值:classpath:/, classpath:/config

以classpath为例,file的用法和classpath用法相同,这里略.

  1. spring:
  2. profiles:
  3. # native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式
  4. active: native
  5. # 自定义配置文件路径
  6. cloud:
  7. config:
  8. server:
  9. native:
  10. searchLocations: classpath:/config/simple2/

spring.profiles.active: 如果使用本地系统配置,则此值必须是native
spring.cloud.config.server.native.searchLocations: 指定配置文件的路径

2.6. 启动和测试

通过CloudGitConfigServerApplication就可以启动服务

在浏览器中输入如下URL,可以访问到配置文件

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

下面通过具体例子说明以上url的意思。如果我们的配置文件名称cloud-config-simple2.yml,则其和URL中各个字段对应的值为:

  • application: cloud-config
  • profile: simple2
  • label: 9500e50f08c43e3e4391175c8f6d5a326b11302f

我们访问以下地址都可以访问到配置文件config-simple2.yml和特定版本下此文件:
http://127.0.0.1:10888/cloud-config/simple2
http://127.0.0.1:10888/cloud-config/simple2/9500e50f08c43e3e4391175c8f6d5a326b11302f
http://127.0.0.1:10888/cloud-config-simple2.yml
http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.yml
http://127.0.0.1:10888/cloud-config-simple2.properties
http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.properties

3. Spring Cloud Config的客户端

配置中心服务端配置成功后,然后其它服务从配置中心获取配置文件,这样的服务被称为客户端。

3.1. 配置客户端

引入jar:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-client</artifactId>
  4. </dependency>

只要是@SpringBootApplication注解启动的spring boot即可

  1. @SpringBootApplication
  2. public class SimpleCloudServiceApplication {
  3. public static void main(String[] args) {
  4. args = new String[1];
  5. args[0] = "--spring.profiles.active=simple2";
  6. SpringApplication.run(SimpleCloudServiceApplication.class, args);
  7. }
  8. }

3.2. 配置参数

请将配置中心的相关配置配置在bootstrap-.yml中,不要配置appliaction-.yml。因为服务启动时,会从bootstrap中读取配置,然后从远程配置中心读取配置文件,最后再从appliaction中获取配置,如果有相同的配置项,则后面的会覆盖前面读到的值。所以如果配置中心的配置配置在appliaction,则配置项不会有任何效果。

bootstrap-simple2.yml

  1. spring:
  2. cloud:
  3. # 配置服务器的地址
  4. config:
  5. uri: http://127.0.0.1:10888
  6. # 要读取配置文件读取的值
  7. name: cloud-config
  8. # 如果不设置此值,则系统设置此值为 spring.profiles.active
  9. profile: dev
  10. # 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔
  11. # label:
  12. # true: 如果访问配置中心失败,则停止启动服务
  13. fail-fast: true
  14. # 配置重试,默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。可以使用这个配置
  15. retry:
  16. initial-interval: 2000
  17. # 最多重试次数
  18. max-attempts: 6
  19. # 最大重试间隔
  20. max-interval: 4000
  21. # 每次重试时间是之前的倍数
  22. multiplier: 1.2

重要参数的解释如下:
配置中心的url
即从哪里读取配置文件,通过“spring.cloud.config.url”配置

要读取哪些配置文件
由以下参数共同决定,和”2.6. 启动和测试”结合看加深理解。
- “spring.cloud.config.name”:配置文件名称,对应上文的读取URL中的{applicaion}值
- “spring.cloud.config.profile”:配置文件的profile,对应上文的URL中的{profile}值
- “spring.cloud.config.label”: 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔

快速失败
如果要求客户端访问配置中心失败,则立即停止启动服务,则设置“spring.cloud.config.label”为 true

重试
如果访问配置失败,则自动重试。默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。通过下面参数可以修改值:

  • “spring.cloud.config.retry.initial-interval”:第一次失败,延迟多久重试
  • “spring.cloud.config.retry.max-attempts”:最多重试次数
  • “spring.cloud.config.retry.max-interval”: 最大重试间隔
  • “spring.cloud.config.retry.multiplier”: 每次重试时间是之前的倍数

    如果要实现重试功能,需要引入新的jar

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-aop</artifactId>
    4. </dependency>
    5. <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
    6. <dependency>
    7. <groupId>org.springframework.retry</groupId>
    8. <artifactId>spring-retry</artifactId>
    9. </dependency>

3.3. 启动和测试

启动SimpleCloudServiceApplication,在浏览器输入http://127.0.0.1:10082/simple,会返回以下信息,则表示成功

  1. {"age":112,"name":"git2-default-dev","randomNum":53}

4. 代码

以上的详细的代码见下面
github代码,请使用tag v0.2,不要使用master

发表评论

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

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

相关阅读