配置管理中心Spring Cloud Config快速入门

配置管理中心-Spring Cloud Config

Spring Cloud Config简介

Spring Cloud Config是用为分布式系统中的基础设施和服务应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。服务端是一个独立的服务,用来连接配置仓库并为客户端提供获取配置信息、加密、解密信息等访问接口,客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关配置内容,并在启动时候从配置中心获取和加载配置信息。Spring Cloud Config默认是git仓库进行存储配置信息,同时也支持其他方式进行存储,例如数据库,文件系统等。

Spring Cloud Config使用

服务端

  1. 添加依赖项spring-cloud-stater-config-server

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  2. 创建Spring boot的程序主类,并添加@EnableConfigServer注解,开启Spring Cloud Config的服务端功能。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 配置相关信息。
    */
    @SpringBootApplication
    @EnableConfigServer # 开启Spring Cloud Config功能。
    @EnableEurekaClient
    public class SpringCloudConfigApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringCloudConfigApplication.class, args);
    }
    }
  3. 修改配置文件信息,添加配置服务的基本信息以及git仓库相关信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    server:
    port: 8080
    spring:
    cloud:
    config:
    server:
    git:
    uri: https://github.com/dwlsxj/spring-cloud-test.git # 配置仓库地址。
    search-paths: git-repo # 仓库文件夹地址。
    eureka:
    instance:
    prefer-ip-address: true
    metadata-map:
    user.name: ${security.user.name}
    user.password: ${security.user.password}
    client:
    service-url:
    defaultZone: http://admin:${REGISTRY_SERVER_PASSWORD:password@localhost:8761/eureka/# Eureka地址。
    # 安全验证。
    security:
    user:
    name: user
    password: ${CONFIG_SERVER_PASSWORD:password}

其中配置信息分别如下所示:
spring.cloud.config.server.git.uri:git仓库的地址。
spring.cloud.config.server.search-paths:对应仓库下相对所搜的位置,可以配置多个,其实就是存储配置信息的具体位置,例如如果仓库中存放了其他的内容并不是单纯存储配置信息,可通过该参数对配置信息进行搜索,定位。
spring.cloud.config.server.git.username:访问git仓库的用户名。
spring.cloud.config.server.git.password:访问git仓库的密码。
4. 配置规则详解,我们可以通过浏览器、postman、curl等工具直接访问配置相关信息,访问配置信息的URL与配置文件的映射关系如下所示:

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

访问地址:
配置信息
当我们通过URL访问配置信息时,我们发现控制台出现如下内容:
本地存储信息

配置服务实际是从Git仓库获取配置信息后,会存储一份在config-server的文件系统中,实质上config-server是通过git clone命令将配置内容复制一份在本地存储,然后读取本地内容返回给客户端,通过本地仓库暂存,可以有效的放置当git仓库出现故障而引起的无法加载的问题。

客户端

  1. 添加依赖项spring-cloud-starter-config

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  2. 对配置文件进行添加配置中心服务地址,以及安全访问的用户名密码等信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    spring:
    application:
    name: springclouduser
    cloud:
    config:
    uri: http://localhost:8080
    fail-fast: true
    username: user
    password: ${CONFIG_SERVER_PASSWORD:password}
    profile: dev
    label: master
    retry: # 重试机制。
    initial-interval: 2000
    max-interval: 10000
    multiplier: 2
    max-attempts: 10

主要配置信息讲解:
spring.application.name:服务名称,对应配置文件规则中的{application}部分。
spring.cloud.config.uri:配置中心服务地址。
spring.cloud.config.username:访问服务注册中心用户名。
spring.cloud.config.password:访问注册中心密码。
spring.cloud.config.profile:应用的环境信息,对应配置规则中的{profile}部分。
spring.cloud.config.label:主要是分支,对应配置文件规则中的{label}部分。
需要注意的是这些配置信息必须配置到bootstrap.ymlbootstrap.properties文件中,因为配置信息application.ymlbootstrap.yml是有加载顺序的,首先程序启动时先加载的是bootstrap.yml文件,其次是application.yml配置信息,因为jar包之外的配置信息优先于jar包之内的配置信息,所以需要首先加载外部的配置信息。
3. 对配置信息的读取和使用,首先需要添加配置类,对配置信息通过EL表达式的方式进行注入。
读取方式有两种方式:

  • 通过@Value(“${profile}”)方式绑定参数到属性中。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
    * 服务配置信息。
    */
    @RefreshScope
    @Configuration
    public class ServiceConfig {
    @Value("${profile}")
    private String profile;
    }
  • 通过Environment对象来获取配置属性。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /**
    * 服务配置信息。
    */
    @RefreshScope
    @RestController
    public class TestController{
    @Autowired
    private Environment env;
    @GetMapping(“/getProfile”)
    public String getProfile(){
    return env.getProperty(“profile”,“null”);
    }
    }
文章目录
  1. 1. 配置管理中心-Spring Cloud Config
    1. 1.1. Spring Cloud Config简介
    2. 1.2. Spring Cloud Config使用
      1. 1.2.1. 服务端
    3. 1.3. 客户端
|
载入天数...载入时分秒...