SpringCloud Config(集中式配置中心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33363618/article/details/83048526

分布式服务面临的问题:

是什么?

 

能干什么?

Config Server 端点

可以使用Config Server 的端点获取配置文件的内容,端点与配置文件的映射规则如下:

/{application}/{profile}[/{label}]

{application}-{profile}.yml

{label}-{application}-{profile}.yml

{application}-{profile}.properties

{label}/{application}-{profile}.properties

以上端点都可以映射到{application}-{profile}.properties 这个配置文件 ,{application}表示微服务的名称,{label} 对应git 仓库的分支, 默认是master

 

搭建microservice-config-server 项目并运行:

访问:

http://localhost:8080/microservice-foo/dev 可得到类似如下 json

{

name: "microservice-foo",

profiles:

[

"dev"

],

  • label: "master",
  • version: "11d8265e757b77b06b4170ae7a7e6171ee1f0218",
  • state: null,
  • propertySources:

[

 

 

访问:

http://localhost:8080/microservice-foo-dev.properties 可得到:

profile: dev-1.0 spring.application.name: microservicecloud-config-caisebei-dev spring.profiles.active[0]: dev

 

直接访问分支可得到:

http://localhost:8080/config-label-v2.0/microservice-foo-dev.properties

profile: dev-2.0 spring.application.name: microservicecloud-config-caisebei-dev spring.profiles.active[0]: dev

引导上下文:

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

如果需要禁用引导过程,可设置springg.cloud.bootstrap.enabled=false.

 

ConfigServer 的Git 仓库配置详解:

1. 占位符支持{application},{profile} ,{label} https://github.com/caiqiufang/{application}

https://github.com/caiqiufang/{application}

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

git:

uri: https://github.com/caiqiufang/microservicecloud-config.git

repos:

simple: https://github.com/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 仓库,它只匹配所有配置文件中名为simple 的应用程序。local 仓库则匹配所有配置文件中义local 开头的所有应用程序的名称

3 .搜索目录

有时,可能把配置文件放在了Git 仓库子目录中,此时可以使用search-path 指定,search-path 同样支持占位符:

uri: https://github.com/caiqiufang/microservicecloud-config.git

search-paths:

- foo,bar*

这样Config server 就会在git 仓库目录、foo子目录,以及所有以bar 开始的子目录中查找配置文件

4. 启动时加载配置文件

默认情况下,在配置被首次请求时,Config Server 才会clone Git 仓库, 也可让Config Server 在启动时就clone Git 仓库

uri: https://github.com/caiqiufang/microservicecloud-config.git

repos:

team-a:

pattern: microservice-*

clone-on-start: true

uri: https://github.com/caiqiufang/microservicecloud-config-repo

这样可让ConfigServer 启动时clone 指定Git 仓库

当然也可以使用其进行全局配置,还可以帮助Config Server 启动时快速识别错误的配置源(不存在的仓库或者用户信息配置错误等)

操作相关:

创建一个github 远程仓库

https://github.com/caiqiufang/microservicecloud-config.git

 

git 命令

git add .(当前)

git commit -m "提示信息"

git push origin master(将文件的版本及修改推送到远程github )

 

What is bootstrap.yml file?

 

 

配置内容的加解密

java 8 中使用JCE(Java Cryptography Extension) 地址:

https://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

 

下载的包应该放在 类似 C:\Program Files\Java\jdk1.8.0_121\jre 的目录中,直接放到jre 里是无效的

 

 

curl $CONFIG-SERVER-URL/encrypt -d 想要加密的明文

curl $CONFIG-SERVER-URL/decrypt -d 想要解密的密文

 

对称加密:

application.yml 中添加

encrypt:

key: foo # 设置对称秘钥

运行项目在linux 终端使用curl 即可进行测试5

加密后的内容,可使用{cipher} 密文的形式存储

 

比如git 仓库中的配置文件内容如下:

sprnig:

datasource:

username: dbuser

# 下边的单引号是不能少的,如果使用的是db.properties 格式配置,则不能说使用单引号

password: '{cipher}8818b2359d88ba40ce2c749e84cf7acdd09228b7c57cb0c2125563dcdda73d2395498a11ae29cbc197a2cfc5b1e3b450'

如果想要直接返回密文本身,则可以设置 spring.cloud.config.server.encrypt.enable=false 然后由Config Client 自行解密

 

使用/refresh 端点手动刷新配置

添加如下依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

 

当修改了git 仓库中的配置之后, 发送post 请求到 Config Server 服务器如:

curl -X POST http://localhost:8081/refresh

 

SpringCloud Bus 自动刷新配置

SpringCloud Bus 使用轻量级的消息代理(RabbitMQ, Kafka等)连接分布式系统的节点,这样就可以传播状态的更改(例如配置的更改) 或者其他的管理命令。 可以将SpringCloud Bus 想象成一个分布式的Springboot Actuator ,

 

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-bus-amqp</artifactId>

</dependency>

bootstrap.yml 中添加如下内容

spring:

rabbitmq: # for SpringCloud bus 动态刷新配置

host: 192.168.115.132

port: 5672

username: guest

password: guest

此时项目有一个/bus/refresh 端点

 

局部刷新:

某些场景(例如灰度发布等),若只想刷新部分微服务的配置,可以通过/bus/refresh 端点的destination 参数来定位要刷新的应用程序。

例如: /bus/refresh?destination=customers:9000, 这样消息总线上的微服务实例就会根据destination 参数的值来判断是否需要刷新。其中,customers:9000 指的是各个微服务的ApplicationContext ID, destination 参数还可以用来定位特定的微服务,例如:/bus/refresh?destionation=customers:** 这样就可以出发customers 微服务所有实例的配置刷新

(配置的局部刷新与ApplicationContext ID 有关,默认情况下,AppliccationContext ID 是spring.application.name:server.port)

标注: 感谢周立 微服务实战相关书籍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值