SpringCloud组件之配置中心Config(Hoxton版本)

Myth丶恋晨 2023-02-21 09:31 318阅读 0赞

1.Config简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

2.创建配置中心服务端工程

目前SpringCloud Config的使用主要是通过Git/SVN方式做一个配置中心,然后每个服务从其中获取自身配置所需的参数。SpringCloud Config也支持本地参数配置的获取。如果使用本地存储的方式,在 application.properties 或 application.yml 文件添加 spring.profiles.active=native 配置即可,它会从项目的 resources路径下读取配置文件。如果是读取指定的配置文件,那么可以使用 spring.cloud.config.server.native.searchLocations = file:E:/properties/ 来读取。本文中使用git文件配置作为演示

2.1 准备工作

启动上篇中Eureka演示工程中Eureka Server

2.2 创建项目

新建一个maven工程,取名为: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"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-demo</artifactId>
  7. <groupId>com.hxmec</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>spring-cloud-config-server</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <!-- config server依赖包-->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-config-server</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. </project>

新建application.yml,增加下面的配置

  1. server:
  2. port: 9005
  3. spring:
  4. application:
  5. name: config-server
  6. cloud:
  7. config:
  8. label: master
  9. server:
  10. git:
  11. #github配置地址
  12. #如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。
  13. uri: https://github.com/ty1972873004/spring-cloud-config.git
  14. #配置文件路径
  15. search-paths: appconfig
  16. #username:
  17. #password:
  18. #git分支
  19. default-label: master
  20. logging:
  21. pattern:
  22. console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
  23. eureka:
  24. client:
  25. service-url:
  26. defaultZone: http://localhost:8888/eureka/

创建一个启动类 ConfigServerApplication,并加上@EnableConfigServer注解开启配置服务器的功能。

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. @EnableDiscoveryClient
  4. public class ConfigServerApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ConfigServerApplication.class, args);
  7. }
  8. }

远程仓库https://github.com/ty1972873004/spring-cloud-config.git 中有个文件config-client-dev.yml 文件中有一个属性: 启动程序可通过访问http://localhost:9005/config-client/dev 验证是否成功获取配置format_png

springcloud config 的URL与配置文件的映射关系如下:

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

3.创建配置中心客户端演示工程

新建一个maven工程,取名为:spring-cloud-config-client,添加如下依赖pom.xml中

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-demo</artifactId>
  7. <groupId>com.hxmec</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>spring-cloud-config-client</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <!-- config client依赖包-->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-config</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. </project>

新建bootstrap.yml,增加下面的配置 config相关配置一定要配置在boostrap.yml中

  1. spring:
  2. application:
  3. name: config-client
  4. cloud:
  5. config:
  6. uri: http://127.0.0.1:9005/
  7. label: master
  8. profile: dev
  9. name: ${spring.application.name}
  10. discovery:
  11. enabled: true
  12. service-id: config-server
  13. server:
  14. port: 9006
  15. logging:
  16. pattern:
  17. console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
  18. eureka:
  19. client:
  20. service-url:
  21. defaultZone: http://localhost:8888/eureka/

创建一个启动类 ConfigClientApplication

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class ConfigClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigClientApplication.class, args);
  6. }
  7. }

编写DemoController添加一个演示接口从配置中心获取配置

  1. @RestController
  2. @RequestMapping("/demo")
  3. @Slf4j
  4. public class DemoController {
  5. @Value("${hx.name:#{null}}")
  6. private String name;
  7. @GetMapping(value = "/test1")
  8. public String test1(){
  9. return "Hello " + name;
  10. }
  11. }

访问http://localhost:9006/demo/test1 验证是否从配置中心获取format_png 1

format_png 2

4.工程地址

https://github.com/ty1972873004/spring-cloud-demo

发表评论

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

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

相关阅读