使用 Spring Cloud Config 统一管理微服务配置

使用 Spring Cloud Config 统一管理微服务配置

Spring Cloud Config 服务器是一个统一管理分布式系统的配置中心。通过其中的应用和服务,可以部署、访问和管理所有的运行时配置属性项。Spring Config 服务器也支持配置属性的版本控制。

Spring Config 将属性值储存在一个有版本控制的仓库中,如 Git 或 SVN。Git 库可以是本地或者远程的。

一、简单使用

1、创建 Config Server

  • 创建新的 Spring 启动项目,选择 Config Server 和 Actuator。

  • 创建 Git 库。

  • 修改 Spring 启动项目的 application.properties 内容

    spring.application.name=configServer
    spring.cloud.config.server.git.uri=https://gitee.com/zolmk/train-ms-spring-config-server.git
    spring.cloud.config.server.git.username=username
    spring.cloud.config.server.git.password=password
    

    配置 Git Uri 和 用户名、密码。

  • 添加开启服务注解

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigserverApplication
    {
    
        public static void main (String[] args)
        {
            SpringApplication.run(ConfigserverApplication.class, args);
        }
    
    }
    
    
  • Spring Config 服务器就配置完成,点击启动就可以了。

2、创建 Config Client

  • 创建 Spring Cloud Config Client 应用

  • 在 pom.xml 中添加下列依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    如果使用了最新版的 Spring Boot 则还需添加

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-bootstrap</artifactId>
    	<version>3.0.0-M5</version>
    </dependency>
    

    文章 《最新 SpringCould 项目使用 Config 配置中心》中有提到。

  • 配置 bootstrap.yml 文件

    spring:
      application:
        name: user-register-server # 微服务名称
      cloud:
        config:
          uri: localhost:port
          profile: ${spring.profiles.active}
          label: master
    
    
  • 启动项目,即可从 Config Server 获取 user-register-server-profile.yml 文件

二、进阶使用

1、不指定 Config Server Uri ,从注册中心获取 Config Server 信息

这里,只需要改变 bootstrap.yml 配置即可

spring:
  application:
    name: user-register-server
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-center-server
      profile: ${spring.profiles.active}
      label: master
eureka:
  client:
    service-url:
      defaultZone: http://zolmk:zolmk@${client.desktop}:8761/eureka/,http://zolmk:zolmk@${client.desktop}:8762/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true
client:
  desktop: 192.168.1.9
  notebook: eureka-node-a

2、Config Server 的 Git 仓库配置详解

1)占位符支持

Config Server 支持的占位符有 {application}、{profile} 和 {label}。

示例:

server:
	port: 8080
spring:
	application:
		name: microservice
	cloud:
		config:
			server:
				git:
					uri: https://git.net/zolmk/{application}

2)模式匹配

模式匹配指的是带有通配符 {application}/{profile} 名称的列表。如果 {application}/{profile} 不匹配任何模式,它将会使用 spring.cloud.config.server.git.uri 定义的 URI。

通过 pattern 来指定匹配 {application}/{profile}

spring:
	cloud:
		server:
			git:
				uri: uri
				repos:
					simple: https://simple/config-repo
					special:
						pattern: special*/dev*,*special*/dev*
						uri: https://github.com/special/config-repo
					local:
						pattern: local*
						uri: file:/home/configsvc/config-repo

simple 仓库未指定 pattern,uri 默认为 simple 后面的字段,simple 只会匹配所有配置文件中名为 simple 的应用程序。

Config Server 可以通过该方式指定多个 git 仓库,并且支持为每个仓库定义各自的访问规则。

3)搜索目录

可以通过指定 search-path 来指定 搜索目录,search-path 支持占位符

server:
  port: 8793
spring:
  application:
    name: config-center-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zolmk/spring-cloud-learn-config-server.git
          username: zolmk
          password: password
          skip-ssl-validation: true
          default-label: master
          search-paths: config/**
          clone-on-start: true

search-paths 可以指定多个目录,config/** 表示搜索 config 目录下的所有子目录,config/foo* 表示搜索 config 目录下以 foo 开头的所有子目录。

4)启动时加载配置文件

通过配置 spring.cloud.config.server.git.clone-on-start=truespring.cloud.config.server.git.repos.team-a.clone-on-start=true

3、配置内容的加解密

这部分需配置在 bootstrap.yml 中(Spring Boot 版本 2.4.0)

1)准备工作

Config Server 的加解密功能依赖 Java Cryptography Extension ( JCE )。所以我们首先需要为 JDK 安装 JCE。

2) 对称加密

  • 配置 bootstrap.yml

    encrypt:
    	key: key
    
  • 使用 POST 工具 post 加密内容到 localhost:port/encrypt,即可得到加密后的数据,这里的加密内容要放到 body 内

  • 使用 POST 工具 post 解密内容到localhost:port/decrypt,即可得到解密后的数据,这里的解密内容要放到 body 内

  • 使用在 config server 中,先用 localhost:port/encrypt 接口得到需要 加密的字符串内容,然后将配置文件中的文字进行替换,如下

    spring:
    	datasource:
    		username: dbuser
    		password: '{cipher}加密后的内容'
    

    spring.datasource.password 这里的 {cipher} 是一个标记符,用来标记该字段是否已加密。

    aplication.properties 中的写法如下:

    spring.datasource.username=dbuser
    spring.datasource.password={cipher}加密后的内容
    

3)非对称加密

  • 先使用 keytool 生成一个密钥对,具体方法在https://www.cnblogs.com/zolmk/p/14101544.html

  • 将生成的 *.jks 文件复制到项目的 classpath 下

  • 在 bootstrap.yml 中添加如下内容

    encrypt:
      key-store:
        alias: config-server-key # alias
        location: classpath:config-server-key.jks # jks classpath:filename.jks
        password: password # storepass
        secret: comzolmk # keypass
    
  • 接下来的方法和上小结对称加密方法一致

4、刷新配置

使用 /refresh 端点手动刷新配置
并且需要在作用的类上加 @RefreshScope 来表明当配置刷新时,该类必须也同步刷新。

posted @ 2020-12-08 17:35  zolmk  阅读(101)  评论(0编辑  收藏  举报