老司机之路——Spring Cloud 配置中心的基本用法与配置中心集群

1,584 阅读8分钟

1. Spring Cloud Config的服务端

1.1. 简述

我们在开发大的系统时,由于服务较多,相同的配置(如数据库信息、缓存、开关量等)会出现在不同的服务上,如果一个配置发生变化,则可能需要修改很多的服务配置。为了解决这个问题,spring cloud提供配置中心。

首先所有的公共配置存储在相同的地址(存储的地方可以是git,svn和本地文件),然后配置中心从这些地方读取配置以restful发布出来,其它服务可以调用接口获取配置信息。

1.2. 配置服务

引入关键jar

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId></dependency>1234

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

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

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

1.3. git后端

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

  • VCS(如git,svn等)

  • 本地文件

本节我们介绍git配置

配置参数主要配置中application-gitsimple2.yml

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

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类

1.4. svn后端

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

1.5. 文件系统后端

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

  • 从本地目录加载配置文件:以file开头

  • file:///${user.home}/config-repo

    默认值:file:./, file:./config

  • 从classpath中加载配置文件:以classpath开头

  • 如果不配置值,则默认值:classpath:/, classpath:/config

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

spring: profiles: # native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式 active: native # 自定义配置文件路径 cloud: config: server: native: searchLocations: classpath:/config/simple2/12345678910

spring.profiles.active: 如果使用本地系统配置,则此值必须是native

spring.cloud.config.server.native.searchLocations: 指定配置文件的路径

1.6. 启动和测试

通过CloudGitConfigServerApplication就可以启动服务

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

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

下面通过具体例子说明以上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

2. Spring Cloud Config的客户端

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

2.1. 配置客户端

引入jar:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId></dependency>1234

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

@SpringBootApplicationpublic class SimpleCloudServiceApplication { public static void main(String[] args) { args = new String[1]; args[0] = "--spring.profiles.active=simple2"; SpringApplication.run(SimpleCloudServiceApplication.class, args); }}123456789

2.2. 配置参数

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

bootstrap-simple2.yml

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

重要参数的解释如下:

配置中心的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

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry --> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency>12345678910

2.3. 启动和测试


具有1-5工作经验的,面对目前流行的技术不知从何下手,需要突破技术瓶颈的可以加群。在公司待久了,过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的可以加群。如果没有工作经验,但基础非常扎实,对java工作机制,常用设计思想,常用java开发框架掌握熟练的可以加群。java架构群:591240817 一起交流。

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

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

2. 配置中心集群和使用

1.1. 配置中心集群

配置中心集群主要通过将配置服务注册成服务来达到集群的目地。

配置中心工程: cloud-config-center

配置中心的其它配置见上篇文章内容,这里只介绍注册成服务的部分

  • @EnableEurekaClient:通过本注解将配置中心注册到配置中心

@SpringBootApplication@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务@EnableEurekaClient // 配置本应用将使用服务注册和服务发现public class CloudGitConfigServerApplication { public static void main(String[] args) { args = new String[1]; args[0] = "--spring.profiles.active=gitsimple2"; SpringApplication.run(CloudGitConfigServerApplication.class, args); }}1234567891011

application-gitsimple2.yml:部分相关配置内容如下

  • spring.application.name:配置服务的服务名称

spring: application: name: config-config-gitsimple2123

1.2. 客户端访问配置中心集群

工程名称:cloud-service

bootstrap-simple2.yml:部分相关配置内容如下

  • 注释掉’spring.cloud.config.uri’的配置,增加两个新的配置值:

  • spring.cloud.config.discovery.enabled:如果设置为true,则表示通过注册中心获取配置服务中心地址

  • spring.cloud.config.discovery.service-id:配置中心的服务注册名称,即配置中心工程cloud-config-center的application.yml中spring.application.name的值

spring: cloud: # 配置服务器的地址 config: # uri: http://127.0.0.1:10888 # 通过注册服务 discovery: enabled: true service-id: config-config-gitsimple2 # 要读取配置文件读取的值 name: cloud-config # 如果不设置此值,则系统设置此值为 spring.profiles.active profile: dev12345678910111213

1.3. 测试

依次启动工程:

cloud-registration-center、 cloud-config-center、cloud-service

查看当前的注册到配置中的信息

http://127.0.0.1:10761/


访问url: http://127.0.0.1:10082/simple,返回内容:

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

说明我们的配置已经生效了