第六节:分布式配置中心SpringCloudConfig

深碍√TFBOYSˉ_ 2023-02-20 07:13 111阅读 0赞

一、项目准备
为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。SpringCloudConfig组件的作用就是从GIT上加载配置文件。然后有一个Client,使用刚才读取到的配置文件。这样的话,就可以避免因为某个配置文件更新,导致需要重启一些微服务的麻烦。

  1. 需要在GIT上创建仓库,然后创建一个文件夹,里面放了三种不同的配置文件。
  2. https://github.com/hairdryre/Study\_SpringCloud 这是我的SpringCloud的学习的仓库。
  3. 仓库有了,然后我再创建一个config/chapter6文件夹,用于放配置文件。
  4. 我创建了三个配置文件,分别是开发环境的jay-config-dev.properties,测试环境的jay-config-test.properties与生产环境的jay-config-pro.properties
  5. 配置文件的内容也很简单。比如开发环境的键值对:jay.label = dev

20190127144233639.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmx1YW5kYWkxOTg1_size_16_color_FFFFFF_t_70

二、搭建Server端口
2.1 创建父项目main

  1. 首先创建一个maven项目,我习惯将其命名为main。导入依赖,父项目的依赖与以前的一样,比较长,可以参考源码。
  2. 2.2 创建Server模块
  3. 右击项目根目录,New--Module--选择左侧的Maven,点击next--输入新模块的名字config-server
  4. config-server子模块导入依赖。
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-config-server</artifactId>
  13. </dependency>
  14. </dependencies>

2.3 编写application.yml配置文件

  1. spring.cloud.config.server.git下,设置配置中心的相关路径。
  2. #项目部署的端口
  3. server:
  4. port: 8001
  5. spring:
  6. #当前项目的名字
  7. application:
  8. name: spring-cloud-config-server
  9. #配置中心的相关配置
  10. cloud:
  11. config:
  12. server:
  13. git:
  14. # 配置git仓库的URL地址
  15. uri: https://github.com/hairdryre/Study_SpringCloud
  16. # git仓库地址下的文件夹,可以配置多个,用“,”分割。
  17. search-paths: config/chapter6
  18. #公共仓库可不用配置帐号密码
  19. username:
  20. password:

2.4 编写启动类

使用@EnableConfigServer开启配置中心。

  1. package com.safesoft.server;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /**
  6. * @author jay.zhou
  7. * @date 2019/1/27
  8. * @time 14:09
  9. */
  10. @SpringBootApplication
  11. @EnableConfigServer
  12. public class ApplicationServer {
  13. public static void main(String[] args) {
  14. SpringApplication.run(ApplicationServer.class, args);
  15. }
  16. }

2.5 测试

  1. 访问http://localhost:8001/jay-config/dev,也就是说访问git上的 jay-config-dev.properties。虽然我也不知道为什么就能访问到了这个配置文件,但是我想这应该也是一种约定吧。我本机的效果如下,我通过F12工具,看到了最关键的一条属性,就是配置文件中的键值对: jay.label = dev;

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmx1YW5kYWkxOTg1_size_16_color_FFFFFF_t_70 1

直接访问配置文件的内容:发送http://localhost:8001/jay-config-test.properties

20190127150729589.png

直接访问配置文件的内容:发送http://localhost:8001/jay-config-pro.properties

20190127150841600.png

搜集的资料:SpringBoot的核心思想就是约定优于配置,所以仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

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

我们的配置文件是 jay-config-dev.properties,所以符合第四条,因此就有了如下映射

application = jay-config

profile = dev

三、搭建Client端

  1. 右击项目根目录,New--Module--选择左侧的Maven,点击next--输入新模块的名字**config-client,效果如下。**

20190127151004596.png

2.1 导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>

2.2 编写配置文件

  1. 有两个配置文件,一个application.properites,另外一个是bootstrap.propertiesbootstrap.properties的加载优先级别要高于application.properties
  2. application.properties配置文件就是配置当前项目的部署的端口号,还有当前项目的名称。内容比较简单。

spring.application.name=spring-cloud-config-client
server.port=8002

bootstrap.properties就很重要了。它指明了要去哪个Server端去获取此Server从GIT上获取的配置文件内容。

  1. 我在配置文件中使用生产环境的配置文件:jay-config-pro.properties,所以就像下面那样配置就好了。
  2. #对应{application}部分
  3. spring.cloud.config.name=jay-config
  4. #对应{profile}部分。如果想使用别的配置,修改这里成
  5. #spring.cloud.config.profile=dev 或者 spring.cloud.config.profile=test
  6. spring.cloud.config.profile=pro
  7. #配置中心的具体地址,即Server的端口
  8. spring.cloud.config.uri=http://localhost:8001/
  9. #git分支
  10. spring.cloud.config.label=master

2.3 编写Controller

  1. 使用@Value("$\{...\}")注解获取读取到的配置文件的内容
  2. package com.safesoft.client.web;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @author jay.zhou
  8. * @date 2019/1/27
  9. * @time 14:19
  10. */
  11. @RestController
  12. public class IndexController {
  13. @Value("${jay.label}")
  14. private String label;
  15. @RequestMapping("/hello")
  16. public String test() {
  17. return label;
  18. }
  19. }

2.4 编写启动类

  1. package com.safesoft.client;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @author jay.zhou
  6. * @date 2019/1/27
  7. * @time 14:18
  8. */
  9. @SpringBootApplication
  10. public class ApplicationClient {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ApplicationClient.class, args);
  13. }
  14. }

2.5 测试

访问:http://localhost:8001/jay-config-pro.properties,我本机得到的结果

20190127152106814.png

再从部署在8002端口的Client访问数据,访问:http://localhost:8002/hello

20190127153410704.png

获取到jay-config-pro.properties里面的配置文件的内容,说明测试成功。

四、源码下载
https://github.com/hairdryre/Study\_SpringCloud

  1. 参考文章: 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)
  2. springcloud(六):配置中心git示例 纯洁的微笑
  3. 参考文章:[https://blog.csdn.net/yanluandai1985/article/details/86665973][https_blog.csdn.net_yanluandai1985_article_details_86665973]

发表评论

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

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

相关阅读