第十二章 Spring Cloud Config 统一配置中心详解 分手后的思念是犯贱 2024-04-01 08:51 14阅读 0赞 **目录** 一、配置问题分析及解决方案 1、问题分析 2、解决方案 二、Spring Cloud Config 介绍 1、Spring Cloud Config特性 2、Spring Cloud Config作用 3、Spring Cloud Config 组件 统一配置中心服务端 统一配置中心客户端 4、Spring Cloud Config 工作流程 三、 配置中心使用 1 搭建远程 git 仓库 1.1 新建远程仓储 1.2.创建远程仓储管理的配置文件 2搭建统一配置服务中心服务端 2.1创建项目,引入依赖 2.2 创建启动类 添加注解@EnableConfigServer 2.3 配置 config Server 管理的远端仓储信息 2.4 ConfigServer 获取远端配置信息测试 3、搭建统一配置服务中心客户端 3.1 创建项目,引入依赖 3.2 订单服务配置 3.3 创建订单服务controller 3.4 创建启动类 3.5 启动测试 -------------------- ## **一、配置问题分析及解决方案** ## ### **1、问题分析** ### ![552c704ac58e40e99b274d93acc1eeaf.png][] 通过上图可知,每个微服务都有一个配置文件,目前只是11个微服务,就需要11个配置文件,若有上百个微服务呢?常规配置管理解决方案缺如下: * 硬编码(需要修改代码、繁琐、风险大) * properties 或者 yml(集群环境下需要替换和重启) * xml(重新打包和重启) ### **2、解决方案** ### 使用Spring Cloud Config**集中式****的**配置管理中心,用来实现微服务系统中服务配置的统一管理。 组件:统一配置中心服务端集中管理配置文件、统一配置中心客户端就是各微服务。 ## 二、**Spring Cloud Config 介绍** ## ![6a2619e77e6f4fb78657e021f3e11fce.png][] ### 1、Spring Cloud Config特性 ### * 提供服务端和客户端支持(Spring Cloud Config Server 和 Spring Cloud Config Client) * 集中式管理分布式环境下的应用部署 * 属性值的加密和解密(对称加密和非对称加密) * 基于 Spring 环境,无缝与 Spring 应用集成 * 可用于任何语言开发的程序 * 默认实现基于 Git ,可以进行版本管理 ### 2、Spring Cloud Config作用 ### * Spring Cloud Config集中管理配置文件 * 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release * 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取自己的配置 * 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置 * 将配置信息以REST接口的形式暴露 ### 3、Spring Cloud Config 组件 ### ![1790049f7fb34f5ab89d67320c53038f.png][] Spring Cloud Config 在微服务分布式系统中,采用**「****Server 服务端****」**和 **「Client 客户端」**的组件方式来提供可扩展的配置服务。 #### **统一配置中心服务端** #### * 是一个独立的微服务应用 * 集中管理配置文件 * 提供配置文件的存储 * 以接口的形式将配置文件的内容提供出去; #### **统一配置中心客户端** #### * 是各个微服务 * 在启动的时候通过接口从配置中心获取和加载获取数据(配置信息) * 并依据此配置信息初始化自己的应用。 ### 4、Spring Cloud Config 工作流程 ### 工作流程:微服务即config client 到 config Server 获取配置文件,config Server 到远端仓库获取配置。 详细说明:统一配置中心服务端 config Server 也是一个微服务,这个微服务将来可能是个小集群,如果把所有配置都放到 config Server,一旦有版本改动,则整个小集群都需要改。因此,可以将所有微服务的配置统一放到 git 远程仓储上进行版本管理。config Server 只需要配置 git 远程仓储地址,在 config Server 启动时候,config Server 会到 git 远程仓储上进行拉取配置到本地仓储,启动后如果远程仓储的配置有改动,则 config Server 会自动检测到远程仓储的配置改动,进行自动拉取最新配置。其他微服务即config client 可以通过 config Server 获取所需配置信息。 因此需要搭建 git 远程仓储环境。 ![4074c55d88a3407abe367b47bd318880.png][] ![b3200487f6f742c6a745b9211e517e7c.png][] **注意**: 统一配置中心服务端 config Server 读取远程仓储的配置的时候是有一定的规则,因此在远程仓储中的配置文件命名也要有一定的规则。 * 远程仓储配置文件命名规则 \{application\}-\{profile\}.yml/\{application\}-\{profile\}.properties 如:order-dev.yml、order-test.yml、order-prod.yml * config Server 读取远程仓储的配置规则: ![e7b6910082d341e3aec3348c6160fd31.png][] 其中 label 代表的是分支例如master分支,profile代表的是环境。 如:http://localhost:7001/**master/**order-**dev**.yml 如:http://localhost:7001/**master/**order-**test**.yml 如:http://localhost:7001/order**/****dev****/master** 如:http://localhost:7001/order**/****test****/master** ## 三、 配置中心使用 ## 配置中心使用步骤 * 搭建远程 git 仓储 * 搭建统一配置服务中心服务端 * 搭建统一配置服务中心客户端 ### 1 搭建远程 git 仓库 ### #### 1.1 新建远程仓储 #### ![3feb80d8792a4822aeb6fe7e3b4341da.png][] #### #### #### 1.2.创建远程仓储管理的配置文件 #### 在远程仓储 master 分支上对订单服务的进行配置,环境分别是dev/test/prod。文件名分别为order-dev.yml、 order-test.yml、 order-prod.yml,订单服务的端口号分别为:9000、9100、9200,配置分别如下: ![f0cd85946ca142c8ba89de65fef8a0c3.png][] ![8e6be4cd3ea14daa8e9e05650a3522b6.png][]![51fce12033df49df8f32646bcd898b54.png][]![a202c334bfda468cab1469b6d63da5ca.png][] > server: > port: 9000 > spring: > application: > name: order-service > > \# 配置eureka客户端信息 > eureka: > client: > service-url: > defaultZone: http://localhost:8000/eureka/ > instance: > prefer-ip-address: true > \# instance-id: order-service > instance-id: $\{spring.cloud.client.ip-address\}:$\{server.port\} ### 2搭建统一配置服务中心服务端 ### #### 2.1创建项目,引入依赖 #### 搭建统一配置服务中心服务端 config server,创建项目,引入 统一服务配置中心 config 依赖。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloudbase</artifactId> <groupId>com.hwadee.springcloud2022</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>configServer7009</artifactId> <dependencies> <!-- 添加统一服务配置中心 config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--Eureka Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project> #### 2.2 **创建启动类** 添加注解**@EnableConfigServer** #### 创建启动类,添加 注解**@EnableConfigServer** **启动 config** 的 **服务端** 应用。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient// 启动 eureka 客户端 @EnableConfigServer// 启动 config 服务端 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } #### 2.3 **配置 config** **Server 管理的远端仓储信息** #### 配置**config** **Server**的 **服务端** 要管理的远程仓库地址。 server: port: 7009 spring: application: name: config-service # 为当前商品服务命名 cloud: config: server: git: # username: xiashanzhu # password: aa@86886830622 uri: https://gitee.com/xiashanzhu/config-repo #要读取的远程仓库的配置文件的地址。 default-label: master # 指定分支,不指定则默认master eureka: client: service-url: # 配置服务注册地址,与 eureka-server 中暴露地址保持一致 defaultZone: http://localhost:8000/eureka instance: prefer-ip-address: true # 是否使用 IP 地址注册,默认 false # instance-id: product-service # 实例 id,服务的唯一标识 instance-id: ${spring.cloud.client.ip-address}:${server.port} # 如果想在控制页面看到服务地址与端口,可以将 instance-id 这样配置 lease-renewal-interval-in-seconds: 5 # 发送心跳的间隔,单位秒,默认 30 lease-expiration-duration-in-seconds: 10 # 续约到期时间,单位秒,默认90 #### 2.4 ConfigServer 获取远端配置信息**测试** #### 分别启动 注册中心 和 统一配置中心服务端 config server,根据前面介绍的配置读取的规则分别进行测试。 * /**label**/\{ **application**\}-\{ **profile**\}.yml **方式测试** 测试:http://localhost:7001/**master/**order-**dev**.yml 测试:http://localhost:7001/**master/**order-**test**.yml ![3f20b245205a4e4ebe355371bd9d8220.png][] ![58eaed11ed8e4862a4a8f9f04975d540.png][] * /\{ **application**\}/\{ **profile**\}/**label** **方式测试** 测试:http://localhost:7001/order**/****dev****/master** 测试:http://localhost:7001/order**/****test****/master** ![9951b6920e244ceaaa8ed7e900b1435e.png][] ![86a47669371543879def2b9b06b04551.png][] **注意:** /\{ **application**\}-\{ **profile**\}.yml默认访问的是master分支下的配置文件。例如访问 [http://localhost:7009/order/dev][http_localhost_7009_order_dev] 和 [http://localhost:7009/order/dev/master][http_localhost_7009_order_dev_master] 结果相同: ![b9267d07e3a14def817d1242fd5b4383.png][] ![9951b6920e244ceaaa8ed7e900b1435e.png][] **小结** 在配置服务中心服务端访问远程仓储的配置文件例如[http://localhost:7009/order/dev/master][http_localhost_7009_order_dev_master]时, 配置服务中心服务端会自动的进行拉取远程仓储到本地,例如:C:/Users/HP/AppData/Local/Temp/config-repo-xxxx/ 目录中。 ![9fb5f7c7e8d340ceacdb0294b051941d.png][] ### 3、搭建统一配置服务中心客户端 ### #### 3.1 创建项目,引入依赖 #### 搭建统一配置服务中心客户端 config client,创建订单服务项目,引入 统一服务配置中心 spring-cloud-starter-config 依赖。就等于开启了 统一配置服务中心的客户端。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloudbase</artifactId> <groupId>com.hwadee.springcloud2022</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.hwadee.springcloud</groupId> <artifactId>orderServer9000</artifactId> <dependencies> <!-- 统一配置服务中心客户端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--动态健康监控 可以用于动态感知配置变化--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 管理公共api --> <dependency> <groupId>com.hwadee.springcloud</groupId> <artifactId>springcloud-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--Eureka Client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 方便创建类的gettter setter --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project> #### 3.2 订单服务配置 #### * 远程仓储创建订单服务配置 在远程仓储 master 分支上对订单服务的进行配置,环境分别是dev/test/prod。文件名分别为order-dev.yml、 order-test.yml、 order-prod.yml,订单服务的端口号分别为:9000、9100、9200,配置分别如下:。 ![f0cd85946ca142c8ba89de65fef8a0c3.png][] ![8e6be4cd3ea14daa8e9e05650a3522b6.png][]![51fce12033df49df8f32646bcd898b54.png][]![a202c334bfda468cab1469b6d63da5ca.png][] * 本地创建订单服务配置 订单服务客户端在启动的时候需要加载配置文件,但此时配置文件不在本地,而是在远程仓储上面,此时运行会报错。因此在运行的时候需要一个本地配置文件bootstrap.yml/properties,在bootstrap.yml/properties 中告知客户端配置文件需要从**config Server 服务端**上获取。使用配置文件bootstrap原因在于微服务启动时候 bootstrap 配置文件加载顺序优先级最高。 在 bootstrap.yml/properties 中获取 **config Server 服务端**上的配置文件的配置方式有两种方式,硬编码方式和服务名方式。 **硬编码方式(不推荐):** 硬编码方式,即将 **config Server 服务端** 的地址硬编码在bootstrap中。但是不推荐使用,因为 **config Server 服务端** 也是集群模式,如果其中硬编码的服务挂掉,则会导致其他服务失败。配置方式如下: ![7f831a1e3ba24e89971a1aba4929e82d.png][] spring: cloud: config: label: master # 指定分支 name: order # 指定应用名称 profile: dev # 指定激活环境 uri: http://localhost:7009 #硬编码 指定访问 config server 远程仓储的地址的ip和端口 **服务名方式:** 使用 **config Server 服务端** 的服务名方式。因为 **config Server 服务端** 也是集群模式,使用服务名,订单服务 会先到注册中心找到 **config Server 服务端** 该服务名的地址,然后在这些地址中在选择一个地址,进行远端的配置文件信息获取。但**注册中心的配置信息**需要写在 bootstrap中,而不是配置在远程仓储中。 注册中心信息修改为: ![cf8200dd6a574298a8bd3874bae82d9b.png][] bootstrap.yml配置信息修改为: ![faf79171af0e4da489265c72968afce7.png][] spring: cloud: config: discovery: service-id: CONFIG-SERVICE #告诉当前客户端 统一配置中心的服务端服务id enabled: true #开启客户端,根据服务id到注册中心获取配置信息 label: master # 指定分支 name: order # 指定应用名称 profile: dev # 指定激活环境 # 配置eureka客户端信息 eureka: client: service-url: defaultZone: http://localhost:8000/eureka/ #### 3.3 创建订单服务controller #### import com.hwadee.springcloud.entity.Product; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/order") public class OrderController { @Value("${env}") private String env; @Value("${port}") private String port; @Value("${info}") private String info; @RequestMapping(value = "/getConfig") public Product getConfigInfo() { Product product = new Product(); product.setName(env +"环境 端口:"+ port +" "+ info); return product; } } #### 3.4 创建启动类 #### 创建启动类,在启动类中加入注解 @EnableEurekaClient 将来注入到服务中心。 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient// 启动 eureka 客户端 public class OrderServerApplication { public static void main(String[] args) { SpringApplication.run(OrderServerApplication.class, args); } } #### 3.5 启动测试 #### 启动 注册中心 、 配置管理中心服务端、订单服务。分别对硬编码和服务名方式测试。 * **配置文件硬编码方式测试:** 重新启动订单服务,输入地址 [http://localhost:9000/order/getConfig][http_localhost_9000_order_getConfig] 查看结果 ![2081cd7d0fd24a8e86ab24b80ec239b0.png][] * **配置文件服务名方式测试:** 重新启动订单服务,输入地址 [http://localhost:9000/order/getConfig][http_localhost_9000_order_getConfig] 查看结果 ![2081cd7d0fd24a8e86ab24b80ec239b0.png][] 第十一章:[GetAway服务网关详解][GetAway] 第十三章:[Spring Cloud Config 统一配置中心详解-客户端动态刷新][Spring Cloud Config _-] [552c704ac58e40e99b274d93acc1eeaf.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/c2f5dfe8bc934df7b86bd51c010e9d93.png [6a2619e77e6f4fb78657e021f3e11fce.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/5d7a4540f0f54afe995a6a8582220305.png [1790049f7fb34f5ab89d67320c53038f.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/0aa74da550fe473db5d582bed9621022.png [4074c55d88a3407abe367b47bd318880.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/92ff4fdea36343759711379d5d2843cb.png [b3200487f6f742c6a745b9211e517e7c.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/2d931670b0004a868ea9f52c15bb8359.png [e7b6910082d341e3aec3348c6160fd31.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/4e06a0e3bbd24bf7a76d8b5cd049fcbc.png [3feb80d8792a4822aeb6fe7e3b4341da.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/af787ba5a51d4d1aa232e6ce2d4a2553.png [f0cd85946ca142c8ba89de65fef8a0c3.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/a77ce8e3430e4914aad897dffd06953f.png [8e6be4cd3ea14daa8e9e05650a3522b6.png]: https://img-blog.csdnimg.cn/8e6be4cd3ea14daa8e9e05650a3522b6.png [51fce12033df49df8f32646bcd898b54.png]: https://img-blog.csdnimg.cn/51fce12033df49df8f32646bcd898b54.png [a202c334bfda468cab1469b6d63da5ca.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/706527ffc4ac4243a27cc1c17553c427.png [3f20b245205a4e4ebe355371bd9d8220.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/212628f15a654d65828f1b70903de6af.png [58eaed11ed8e4862a4a8f9f04975d540.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/baf68dd078da42219c1f969bb832195a.png [9951b6920e244ceaaa8ed7e900b1435e.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/10f513c80882444baf0b0867c2720080.png [86a47669371543879def2b9b06b04551.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/f5c0943674144c399ca14f096553ccd1.png [http_localhost_7009_order_dev]: http://localhost:7009/order/dev [http_localhost_7009_order_dev_master]: http://localhost:7009/order/dev/master [b9267d07e3a14def817d1242fd5b4383.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/2c0005812a1641e08b0806f92062d022.png [9fb5f7c7e8d340ceacdb0294b051941d.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/6673d6e4c3c340f083692843bb4f8441.png [7f831a1e3ba24e89971a1aba4929e82d.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/edda998ed6354792b143597df3e50bb5.png [cf8200dd6a574298a8bd3874bae82d9b.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/2d7db6f42719426fac84ff40f828dd09.png [faf79171af0e4da489265c72968afce7.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/3750ebc107e74705816866a36a1f5f48.png [http_localhost_9000_order_getConfig]: http://localhost:9000/order/getConfig [2081cd7d0fd24a8e86ab24b80ec239b0.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/01/328c74222b1147aeb21be9f3d5ca14ad.png [GetAway]: https://blog.csdn.net/qq_41946216/article/details/127401374?spm=1001.2014.3001.5502 [Spring Cloud Config _-]: https://blog.csdn.net/qq_41946216/article/details/127461068?spm=1001.2014.3001.5502
还没有评论,来说两句吧...