Spring Cloud 系列之 Config 配置中心

Love The Way You Lie 2022-12-03 08:41 240阅读 0赞

配置文件是我们再熟悉不过的,在微服务系统中,每个微服务不仅仅只有代码,还需要「连接其他资源」,例如数据库的配置或功能性的开关 MySQL、Redis 、Security 等相关的配置。除了项目运行的基础配置之外,还有一些配置是与我们业务有关系的,比如说七牛存储、短信和邮件相关,或者一些业务上的开关。

但是随着微服务系统的不断迭代,整个微服务系统可能会成为一个「网状结构」,这个时候就要考虑整个微服务系统的「扩展性、伸缩性、耦合性」等等。其中一个很重要的环节就是「配置管理」的问题。

2|0**常规配置管理解决方案缺点**

  • 硬编码(需要修改代码、繁琐、风险大)
  • properties 或者 yml(集群环境下需要替换和重启)
  • xml(重新打包和重启)

3|0**为什么使用 Spring Cloud Config**

format_png

由于常规配置管理有很大的缺点,所以采用 Spring Cloud Config 「集中式」的配置中心来管理「每个服务」的配置信息。

Spring Cloud Config 在微服务分布式系统中,采用 「Server 服务端」「Client 客户端」的方式来提供可扩展的配置服务。服务端提供配置文件的存储,以接口的形式将配置文件的内容提供出去;客户端通过接口获取数据、并依据此数据初始化自己的应用。

配置中心负责「管理所有服务」的各种环境配置文件。

配置中心默认采用 Git 的方式存储配置文件,因此我们可以很容易的部署和修改,有助于环境配置进行版本管理。

4|0**Spring Cloud Config 解决了什么问题**

Spring Cloud Config 解决了微服务配置的「中心化、版本控制、平台独立、语言独立」等问题。其特性如下:

  • 提供服务端和客户端支持(Spring Cloud Config Server 和 Spring Cloud Config Client)
  • 集中式管理分布式环境下的应用部署
  • 属性值的加密和解密(对称加密和非对称加密)
  • 基于 Spring 环境,无缝与 Spring 应用集成
  • 可用于任何语言开发的程序
  • 默认实现基于 Git ,可以进行版本管理

接下来,我们主要从以下几块来讲一下 Config 的使用。

  1. 基础版的配置中心(不集成 Eureka)
  2. 集成 Eureka 版的高可用配置中心
  3. 基于 Actuator 实现配置的自动刷新
  4. 配置中心属性值的加密和解密(对称加密和非对称加密)
  5. 基于 Spring Cloud Bus 实现配置的自动刷新
  6. 配置中心用户安全认证

5|0**环境准备**

5|1**项目**

config-demo 聚合工程。SpringBoot 2.2.4.RELEASESpring Cloud Hoxton.SR1

  • eureka-server:注册中心(用于集成 Eureka 版的配置中心)
  • eureka-server02:注册中心(用于集成 Eureka 版的配置中心)
  • order-service:订单服务(用于集成 Eureka 版的配置中心)

format_png 2

5|2**仓库**

config-repo 仓库。

format_png 3

  • Repository name:仓库名称
  • Description(可选):仓库描述介绍
  • Public,Private:仓库权限(公开共享,私有或指定合作者)
  • Initialize this repository with a README:添加一个 README.md
  • Add .gitignore:不需要进行版本管理的文件类型,生成对应文件 .gitignore
  • Add a license:证书类型,生成对应文件 LICENSE

不同环境的配置文件,上传至 config-repo 仓库。

format_png 4

配置文件的名称不是乱起的,例如 config-client-dev.ymlconfig-client-prod.yml 这两个文件是同一个项目的不同环境,项目名称为 config-client, 一个对应开发环境,一个对应正式环境。test 表示测试环境。

  1. ----- config-client.yml
  2. server:
  3. port: 7777 # 端口
  4. spring:
  5. application:
  6. name: config-client # 应用名称
  7. # 自定义配置
  8. name: config-client-default
  9. ------- config-client-dev.yml
  10. server:
  11. port: 7778 # 端口
  12. spring:
  13. application:
  14. name: config-client # 应用名称
  15. # 自定义配置
  16. name: config-client-dev
  17. -------- config-client-test.yml
  18. server:
  19. port: 7779 # 端口
  20. spring:
  21. application:
  22. name: config-client # 应用名称
  23. # 自定义配置
  24. name: config-client-test
  25. --------- config-client-prod.yml
  26. server:
  27. port: 7780 # 端口
  28. spring:
  29. application:
  30. name: config-client # 应用名称
  31. # 自定义配置
  32. name: config-client-prod

入门案例讲解:基础版的配置中心(不集成 Eureka)

官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/

6|1**创建服务端**

点击链接观看:Config 入门案例创建服务端视频(获取更多请关注公众号「哈喽沃德先生」)

config-demo 父工程下创建子项目 config-server

添加依赖

添加 spring-cloud-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</groupId>
  6. <artifactId>config-server</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <!-- 继承父依赖 -->
  9. <parent>
  10. <groupId>com.example</groupId>
  11. <artifactId>config-demo</artifactId>
  12. <version>1.0-SNAPSHOT</version>
  13. </parent>
  14. <!-- 项目依赖 -->
  15. <dependencies>
  16. <!-- spring cloud config server 依赖 -->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-config-server</artifactId>
  20. </dependency>
  21. <!-- spring boot test 依赖 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. <exclusions>
  27. <exclusion>
  28. <groupId>org.junit.vintage</groupId>
  29. <artifactId>junit-vintage-engine</artifactId>
  30. </exclusion>
  31. </exclusions>
  32. </dependency>
  33. </dependencies>
  34. </project>

配置文件

  1. server:
  2. port: 8888 # 端口
  3. spring:
  4. application:
  5. name: config-server # 应用名称
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/imrhelloworld/config-repo # 配置文件所在仓库地址
  11. #username: # Github 等产品的登录账号
  12. #password: # Github 等产品的登录密码
  13. #default-label: master # 配置文件分支
  14. #search-paths: # 配置文件所在根目录

启动类

启动类添加 @EnableConfigServer 注解。

  1. package com.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. // 配置中心服务端注解
  6. @EnableConfigServer
  7. @SpringBootApplication
  8. public class ConfigServerApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(ConfigServerApplication.class, args);
  11. }
  12. }

访问规则

Spring Cloud Config 有一套访问规则,我们通过这套规则在浏览器上直接访问即可。

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties
  • {application}:应用名称(目标服务名称)
  • {profile}:获取指定环境配置,项目有开发环境、测试环境、生产环境,对应到配置文件就是以 application-{profile}.yml 加以区分,例如 application-dev.yml、application-test.yml、application-prod.yml。默认值为 default。
  • {label}:表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件。

「测试」

http://localhost:8888/config-client/default

http://localhost:8888/config-client/dev/master

http://localhost:8888/config-client-test.yml

http://localhost:8888/master/config-client-prod.yml

访问以上地址,如果可以正常返回数据,说明配置中心服务端一切正常。

6|2**创建客户端**

点击链接观看:Config 入门案例创建客户端视频(获取更多请关注公众号「哈喽沃德先生」)

config-demo 父工程下创建子项目 config-client

添加依赖

添加 spring-cloud-starter-config 依赖,完整 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</groupId>
  6. <artifactId>config-client</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <!-- 继承父依赖 -->
  9. <parent>
  10. <groupId>com.example</groupId>
  11. <artifactId>config-demo</artifactId>
  12. <version>1.0-SNAPSHOT</version>
  13. </parent>
  14. <!-- 项目依赖 -->
  15. <dependencies>
  16. <!-- spring cloud starter config 依赖 -->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-config</artifactId>
  20. </dependency>
  21. <!-- spring boot web 依赖 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- spring boot test 依赖 -->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. <exclusions>
  32. <exclusion>
  33. <groupId>org.junit.vintage</groupId>
  34. <artifactId>junit-vintage-engine</artifactId>
  35. </exclusion>
  36. </exclusions>
  37. </dependency>
  38. </dependencies>
  39. </project>

配置文件

客户端配置文件名称必须叫 bootstrap.yml:

  1. spring:
  2. cloud:
  3. config:
  4. name: config-client # 配置文件名称,对应 git 仓库中配置文件前半部分
  5. uri: http://localhost:8888 # config-server 服务端地址
  6. label: master # git 分支
  7. profile: default # 指定环境

客户端配置文件名称必须叫 bootstrap.yml

控制层

添加一个 RestController 用于测试获取配置文件信息。

  1. package com.example.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. @RestController
  6. public class ConfigController {
  7. @Value("${name}")
  8. private String name;
  9. @GetMapping("/name")
  10. public String getName() {
  11. return name;
  12. }
  13. }

启动类

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

测试

访问:http://localhost:7777/name 结果如下:

format_png 5

修改配置文件为 dev 环境:

  1. spring:
  2. cloud:
  3. config:
  4. name: config-client # 应用名称,对应 git 仓库中配置文件前半部分
  5. uri: http://localhost:8888 # config-server 服务端地址
  6. label: master # git 分支
  7. profile: dev # 指定环境

访问:http://localhost:7778/name 结果如下:

format_png 6

7|0**Spring Cloud Config 高可用**

以上讲了 Spring Cloud Config 最基础的用法,如果我们的项目中使用了 Eureka 作为服务注册发现中心,那么 Spring Cloud Config 也应该注册到 Eureka,方便其他服务使用,并且可以注册多个配置中心服务端,实现高可用。

接下来就集成 Spring Cloud Config 到 Eureka。关于 Eureka 的相关知识大家可翻阅我的历史文章进行学习。

7|1**添加配置文件**

在 Github 仓库中增加配置文件。

format_png 7

order-service-dev.yml

  1. server:
  2. port: 9090 # 端口
  3. spring:
  4. application:
  5. name: order-service # 应用名称
  6. # 配置 Eureka Server 注册中心
  7. eureka:
  8. instance:
  9. prefer-ip-address: true # 是否使用 ip 地址注册
  10. instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  11. client:
  12. service-url: # 设置服务注册中心地址
  13. defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  14. # 自定义配置
  15. name: order-service-dev

order-service-prod.yml

  1. server:
  2. port: 9091 # 端口
  3. spring:
  4. application:
  5. name: order-service # 应用名称
  6. # 配置 Eureka Server 注册中心
  7. eureka:
  8. instance:
  9. prefer-ip-address: true # 是否使用 ip 地址注册
  10. instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  11. client:
  12. service-url: # 设置服务注册中心地址
  13. defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
  14. # 自定义配置
  15. name: order-service-prod

7|2**整合注册中心**

案例已经给大家准备好了,无需创建注册中心直接使用即可,为了清楚,把依赖和配置信息给大家贴出来。

依赖

eureka-servereureka-server02 核心依赖部分一致。

  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</groupId>
  6. <artifactId>eureka-server</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <!-- 继承父依赖 -->
  9. <parent>
  10. <groupId>com.example</groupId>
  11. <artifactId>config-demo</artifactId>
  12. <version>1.0-SNAPSHOT</version>
  13. </parent>
  14. <!-- 项目依赖 -->
  15. <dependencies>
  16. <!-- netflix eureka server 依赖 -->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  20. </dependency>
  21. <!-- spring boot web 依赖 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- spring boot test 依赖 -->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. <exclusions>
  32. <exclusion>
  33. <groupId>org.junit.vintage</groupId>
  34. <artifactId>junit-vintage-engine</artifactId>
  35. </exclusion>
  36. </exclusions>
  37. </dependency>
  38. </dependencies>
  39. </project>
  40. 配置文件

eureka-server 的 application.yml

  1. server:
  2. port: 8761 # 端口
  3. spring:
  4. application:
  5. name: eureka-server # 应用名称(集群下相同)
  6. # 配置 Eureka Server 注册中心
  7. eureka:
  8. instance:
  9. hostname: eureka01 # 主机名,不配置的时候将根据操作系统的主机名来获取
  10. prefer-ip-address: true # 是否使用 ip 地址注册
  11. instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  12. client:
  13. # 设置服务注册中心地址,指向另一个注册中心
  14. service-url: # 注册中心对外暴露的注册地址
  15. defaultZone: http://localhost:8762/eureka/

eureka-server02 的 application.yml

  1. server:
  2. port: 8762 # 端口
  3. spring:
  4. application:
  5. name: eureka-server # 应用名称(集群下相同)
  6. # 配置 Eureka Server 注册中心
  7. eureka:
  8. instance:
  9. hostname: eureka02 # 主机名,不配置的时候将根据操作系统的主机名来获取
  10. prefer-ip-address: true # 是否使用 ip 地址注册
  11. instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  12. client:
  13. # 设置服务注册中心地址,指向另一个注册中心
  14. service-url: # 注册中心对外暴露的注册地址
  15. defaultZone: http://localhost:8761/eureka/
  16. 启动类

eureka-servereureka-server02 启动类核心代码一致。

  1. package com.example;
  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. // 开启 EurekaServer 注解
  7. @EnableEurekaServer
  8. public class EurekaServerApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(EurekaServerApplication.class, args);
  11. }
  12. }

发表评论

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

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

相关阅读

    相关 Spring Cloud Config远程配置

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud