0
点赞
收藏
分享

微信扫一扫

软件架构-分布式集中配置中心Spring Cloud Config详解(上)


config配置中心介绍(一)

  • ① 为什么需要配置中心?
  1. 集中管理配置。
  2. 不同环境不同配置。
  3. 运行期间动态调整配置。
  4. 自动刷新。
  • ② Spring Cloud Config介绍

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储配置),因此可以方便的实现对配置的版本控制与内容审计。Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_02

官网
​​​https://github.com/spring-cloud/spring-cloud-config​​

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_03

  • ③ 使用config实现配置中心服务端及客户端

首先新增git配置仓库中心,地址为:https://github.com/limingios/springcloudconfig.git,在仓库里增加如下配置文件

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_04

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_05

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_06

服务端源码 (二)

10-ms-config-server

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_07

添加依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_08

并在启动类上增加注解@EnableConfigServer

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_09

配置文件application.yml

server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
uri: https://github.com/limingios/springcloudconfig.git # 配置Git仓库的地址
force-pull: true
username: # Git仓库的账号
password: # Git仓库的密码

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_10

  • 启动项目,访问地址:http://localhost:8080/ms-config/dev,得到整个项目的配置信息

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_11

里面有个version,其实跟git中的版本是一致的。

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_12

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_13

​​http://localhost:8080/ms-config-dev.properties​​

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_14

​​http://localhost:8080/task/ms-config-dev.properties​​

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_15

每次访问后,都会将文件通过server下载到本地

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_16

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_17

  • ① config文件的映射规则

1.application 项目的名称

2.label 是分支名称

3.profile 就是类别 dev test

4.默认是 master

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_18

  • 各种配置方法见项目配置文件

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_19

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_20

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_21

客户端client,启动的时候其实就是config从server中获取一下配置文件,对应的参数放入内存中,不保存在本地。如果server-config挂了的话,只要clent不重启不影响client的。

编写config配置中心客户端(三)

spring cloud有一个【引导上下文】的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml或 properties)中的属性不同,引导上下文加载(bootstrap.)中的属性。配置在 bootstrap.*中的属性有更高的优先级,因此默认情况下它们不能被本地配置覆盖。

  • ① 源码

10-ms-config-client

添加依赖

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

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_22

默认的application.yml配置文件,还需增加一个bootstrap.yml的配置文件

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_23

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_24

1.client端启动后,启动访问server端,从server端下载配置文件到【内存】中
2.访问对应的value中的值,直接就可以看到server端配置的属性

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_25

编写了一个Controller,value获取配置文件中的数据。这都是spring mvc的基础,这里就不在做阐述了。

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_26

配置信息的加解密安全处理(四)

在 Git仓库中明文存储配置属性的。很多场景下,对于某些敏感的配置内容(例如数据库账号、密码等),应当加密存储。 config server为配置内容的加密与解密提供了支持。

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_27

  • ① 安装JCE

config server的加解密功能依赖Java Cryptography Extension(JCE)
Java8 JCE下载地址:
​​​ http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html​​

注意:就算有也要覆盖,负责会报错:Unable to initialize due to invalid secret key

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_28

下载JCE并解压,将其中的jar包覆盖到JDK/jre/lib/security目录中

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_29

  • ② 对称加密

config server提供了加密与解密的接口,分别是/encrypt与/decrypt

源码:10-ms-config-server-encryption

启动项目:注意填写加密的key。

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_git_30

获取加密信息(post方式):http://localhost:8080/encrypt
body中填写要加密的信息:www.idig8.com

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_31

获取到加密信息:9ef1e86a01b272fd75d72a0dc40161db938430c069a76d6d82a17b2b5a8e2cf2

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_spring_32

将这个信息放入需要加密的信息中前面一定要加入{cipher}
{cipher} 英文就是暗号的意思,有了暗号才会给你解密的
注意:如果是properties 不需要加引号引入value值,如果是yml需要加入引号

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_配置文件_33

获取解密信息 http://localhost:8080/decrypt
将刚才加密的信息进行解密9ef1e86a01b272fd75d72a0dc40161db938430c069a76d6d82a17b2b5a8e2cf2

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_34

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_35

直接访问http://localhost:8080/ms-config-encryption-dev.yml,得到密钥原文

软件架构-分布式集中配置中心Spring Cloud Config详解(上)_java_36

说明 config server能自动解密配置内容。一些场景下,想要让 config server直接返回密文本身,而并非解密后的内容,可设置spring.cloud.config.server.encrypt.enabled=false,这时可由 ConfigCIient自行解密。

PS:分布式集中配置中心Spring Cloud Config 确实功能很强大,这次咱们主要说下,如果制作server,client端如何获取,而且还说了加密和解密。下次咱们说说动态刷新配置这块。


举报

相关推荐

0 条评论