Spring Cloud 应用篇 之 Spring Cloud Config(配置中心)

为了方便保存,转自:https://blog.csdn.net/hubo_88/article/details/80692156

从前几篇文章中我们就可以看出,在分布式系统中,服务数量会很多,如果要修改服务的配置文件,会很麻烦,这个时候,我们想把配置文件放在一个地方统一管理,实时更新,Spring Cloud 就给我们提供了这样一个组件——Spring Cloud Config。

(一)简介

Spring Cloud Config 支持配置文件放在远程仓库中,例如 Git、SVN,也可以挂载到本地。Spring Cloud Config 和 Spring Boot Admin 有一点类似,它也由服务端和客户端组成,即 server、client。

(二)基于 server-client 构建项目

2.1 创建 server 

2.1.1 新建一个 module(spring-cloud-config)

2.1.2 pom 文件添加相关依赖

 
  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-config-server</artifactId>

  4. </dependency>

2.1.3 启动类添加注解 @EnableConfigServer 开启配置中心的功能

 
  1. @SpringBootApplication

  2. @EnableConfigServer

  3. public class SpringCloudConfigApplication {

  4.  
  5. public static void main(String[] args) {

  6. SpringApplication.run(SpringCloudConfigApplication.class,args);

  7. }

  8. }

2.1.4 添加配置文件

 
  1. server:

  2. port: 8888

  3.  
  4. spring:

  5. cloud:

  6. config:

  7. server:

  8. git:

  9. uri: https://github.com/shmilyah/cloud-config-samples.git

  10. search-paths: '{application}'

  11. application:

  12. name: spring-cloud-config

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

spring.cloud.config.server.git.searchPaths:配置仓库路径,这里的配置会根据传入的应用名动态查找

由于我这里配置的 git 仓库为公开的,所以不需要配置用户名和密码,如果你配置的是私有仓库,还需要配置用户名和密码

(spring.cloud.config.server.git.username 和 spring.cloud.config.server.git.password)

2.1.5 创建仓库里的配置文件

本例为了测试,我在 https://github.com/shmilyah/cloud-config-samples.git 仓库里创建了一个文件夹 config-client,文件夹下创建了一个配置文件 config-client-dev.yaml,内容如下

hello: world

2.1.6 启动 spring-cloud-config 服务,访问 http://localhost:8888/config-client/dev/master

 
  1. {"name":"config-client","profiles":["dev"],"label":"master",

  2. "version":"15a6b2770f282a7d12790ea4d03a548fe1fea664","state":null,

  3. "propertySources":[{"name":"https://github.com/shmilyah/cloud-config-samples.git/config-client/config-client-dev.yaml","source":{"hello":"world"}}]}

访问配置中心获取配置信息成功,这里的请求路径映射为:/{application}/{profile}[/{label}],还有一些其他路径的映射,详细信息可以网上查看相关文档。

2.2 创建客户端 client

2.2.1 创建新的 module(spring-cloud-client)

2.2.2 添加 pom 依赖

 
  1. <dependency>

  2.     <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-starter-config</artifactId>

  4. </dependency>

  5.  
  6. <dependency>

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-web</artifactId>

  9. </dependency>

2.2.3 添加配置文件 application.yaml

 
  1. server:

  2. port: 8989

  3.  
  4. spring:

  5. application:

  6. name: config-client

  7. cloud:

  8. config:

  9. uri: http://localhost:8888/

  10. profile: dev

  11. label: master

spring.cloud.config.uri 配置要连接的配置中心的地址

spring.cloud.config.profile 配置获取哪个环境的配置文件

一个项目一般会有开发(dev)、测试(test)、生产(pro)环境,还有一些公司的定义不同,还有 dat、uat 环境等,这里我们配置的是开发环境 dev 

spring.cloud.config.lable 配置连接远程仓库的分支,这里我们是 master

这里的 spring.application.name 配置要和 git 仓库的文件夹名称一致,因为配置中心配置了 search-paths 是通过应用名查找对应的路径

2.2.4 写一个测试接口,代码如下:

 
  1. @SpringBootApplication

  2. @RestController

  3. public class ConfigClientApplication {

  4.  
  5. public static void main(String[] args) {

  6. SpringApplication.run(ConfigClientApplication.class, args);

  7. }

  8.  
  9. @Value("${hello}")

  10. private String hello;

  11.  
  12. @RequestMapping("hello")

  13. public String hello() {

  14. return hello;

  15. }

  16. }

访问 http://localhost:8989/hello

这说明我们的 config-client 已经成功获取了配置中心里的配置文件,下面我们介绍基于 eureka 来使用配置中心。

(三)基于 Eureka 构建项目

如(二)的配置,我们需要为每个服务配置指定 git 的 uri,但我们基于 Eureka 来使用,就可以省略这一步,把配置中心和其他服务都注册到 eureka server,这个时候,服务会自动获取 eureka 里的 config,去获取自己配置的需要的配置文件。

3.1 修改 spring-cloud-config 服务

3.1.1 pom 文件添加 Eureka 的依赖

 
  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-config-server</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>org.springframework.cloud</groupId>

  7. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  8. </dependency>

3.1.2 启动类添加注解

 
  1. @SpringBootApplication

  2. @EnableEurekaClient

  3. @EnableConfigServer

  4. public class SpringCloudConfigApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(SpringCloudConfigApplication.class,args);

  8. }

  9. }

3.1.3 配置文件添加 eureka 的配置

 
  1. server:

  2. port: 8888

  3.  
  4. spring:

  5. cloud:

  6. config:

  7. server:

  8. git:

  9. uri: https://github.com/shmilyah/cloud-config-samples.git

  10. search-paths: '{application}'

  11. application:

  12. name: spring-cloud-config

  13. eureka:

  14. client:

  15. service-url:

  16. defaultZone: http://localhost:8761/eureka/

3.1.4 依次启动 eureka server、spring-cloud-config,访问 http://localhost:8888/config-client/dev/master

3.2 重构 spring-cloud-config-client 服务

3.2.1 添加 pom 依赖

 
  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  4. </dependency>

  5. <dependency>

  6. <groupId>org.springframework.cloud</groupId>

  7. <artifactId>spring-cloud-starter-config</artifactId>

  8. </dependency>

  9. <dependency>

  10. <groupId>org.springframework.boot</groupId>

  11. <artifactId>spring-boot-starter-web</artifactId>

  12. </dependency>

3.2.2 启动类添加注解

 
  1. @SpringBootApplication

  2. @RestController

  3. @EnableEurekaClient

  4. public class ConfigClientApplication {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(ConfigClientApplication.class, args);

  8. }

  9.  
  10. @Value("${hello}")

  11. private String hello;

  12.  
  13. @RequestMapping("hello")

  14. public String hello() {

  15. return hello;

  16. }

  17. }

3.2.3 修改配置文件

 
  1. server:

  2. port: 8989

  3.  
  4. spring:

  5. application:

  6. name: config-client

  7. cloud:

  8. config:

  9. # uri: http://localhost:8888/

  10. profile: dev

  11. label: master

  12. discovery:

  13. enabled: true

  14. service-id: spring-cloud-config

  15. eureka:

  16. client:

  17. service-url:

  18. defaultZone: http://localhost:8761/eureka

这种方式,我们通过 spring.cloud.config.discovery 的配置,通过 service-id 向注册中心查找配置中心,这个 service-id 和配置中心的 application.name 一致

3.2.4 启动 spring-cloud-config-client 服务

访问 http://localhost:8989/hello,虽然我们没有配置 git 的 uri,这里依然获取到了配置信息,因为我们从注册中心里拿到了配置中心

至此,我们的配置中心就已经搭建成功了,后续就可以把其他服务的配置文件都放到配置中心指定的远程仓库中统一管理了

不过这里我们没有还没有实现配置中心文件实时刷新,这个后续会讲

其实还有一个问题,这里面有一个坑,可能有人已经发现了,当你配置的配置中心的 server.port 不是 8888 的时候,其他服务就起不来了,从日志中可以发现,服务启动的时候,Fetching config from server at: http://localhost:8888,时间有限,这个问题下一篇来说吧!

源码下载:https://github.com/shmilyah/spring-cloud-componets

--------------------- 作者:hubo_88 来源:CSDN 原文:https://blog.csdn.net/hubo_88/article/details/80692156?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/ecnu344/article/details/82992215