spring cloud config 简介


spring cloud config
分布式配置中心
1.把各模块公共配置抽取出来,做到一处修改处处生效
2.高可用, 改了配置后, 各模块动态刷新, 不需要重启

服务端
1.jar包导入
spring-cloud-config-server

2.启动类
@EnableConfigServer

3.配置文件
spring.cloud.config.server.git.uri=https://github.com/xiaobinhuang2015/zg-config-repo
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=xiaobinhuang2015
spring.cloud.config.server.git.password=Steven8494631
# 本地缓存目录
spring.cloud.config.server.git.basedir=/Users/stevenhuang/Documents/steven/IT/config_server/git_baseDir
# 强制从 GitHub 配置中心中拉取配置信息,不走缓存
spring.cloud.config.server.git.force-pull=true

客户端
1.jar包导入
org.springframework.cloud/spring-cloud-config-client
org.springframework.retry/spring-retry

2.配置文件
spring.cloud.config.profile=dev
spring.cloud.config.label=master
# 单机配置
# spring.cloud.config.uri=http://localhost:8085/
# 高可用配置
# 开启 config server 服务发现功能
spring.cloud.config.discovery.enabled=true
# 服务发现的服务名称
spring.cloud.config.discovery.service-id=micro-config-server

# 如果获取配置有问题,快速失败
spring.cloud.config.fail-fast=true
# 默认重试的间隔时间,默认 1000 ms
spring.cloud.config.retry.multiplier=1000
# 下一间隔时间的乘数,默认是 1.1
# spring.cloud.config.retry.initial-interval=1.1
# 最大间隔时间,最大 2000 ms
spring.cloud.config.retry.max-interval=2000
# 最大重试次数,默认 6 次
spring.cloud.config.retry.max-attempts=6

3.git -- branch --> master
zg-config-repo / config-repo / micro-order-dev.properties
username=username--dev

动态刷新
服务端 / 客户端
1.jar包导入
org.springframework.cloud/spring-cloud-starter-bus-amqp

2.配置文件
spring.rabbitmq.host=192.168.0.105
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=rabbit_mq_steven
spring.rabbitmq.username=steven
spring.rabbitmq.password=002310

# 客户端
spring.cloud.bus.refresh.enabled=true
spring.cloud.bus.trace.enabled=true

3.代码使用
# 微服务业务项目
// 每次访问都会创建一个新的
@RefreshScope
@Autowired
private Environment environment;
//读取配置
@Value("${redis.password}")

# zuul
@Bean
// 每次访问都会创建一个新的
@RefreshScope
@ConfigurationProperties("zuul")
@Primary
public ZuulProperties zuulProperties() {
    return new ZuulProperties();
}

4.刷新配置 url 
// 调用任意一台接入消息总线的端点即可,可配置到 github 中,改了 github 的配置自动调用  
http://localhost:8084/actuator/bus-refresh

配置加密
1.本地生成秘钥文件 -- jre keytools
cd /Users/stevenhuang/Documents/steven/IT/software/jdk8.macosx.x86_64_1.8.0.v112/jre/bin

keytool -genkeypair -alias config-server -keyalg RSA -keystore config-server.keystore -validity 365 
输入密钥库口令:  123456
您的名字与姓氏是什么? steven huang
您的组织单位名称是什么? oneteam
您的组织名称是什么?  com
您所在的城市或区域名称是什么?  shenzhen
您所在的省/市/自治区名称是什么?  guangdong
该单位的双字母国家/地区代码是什么?  PRC
CN=steven huang, OU=oneteam, O=com, L=shenzhen, ST=guangdong, C=PRC是否正确?   y
输入 <config-server> 的密钥口令    123456

2.服务端 -- config server
生成的秘钥文件 config-server.keystore copy到 springcloud-config-server/src/main/resources

配置 bootstrap.properties
# 加密配置
encrypt.key-store.location=config-server.keystore
encrypt.key-store.alias=config-server
encrypt.key-store.password=123456
encrypt.key-store.secret=123456

pom 增加静态文件扫描
<resource>
  <directory>src/main/resources</directory>
  <includes>
    <include>**/*.properties</include>
    <include>**/*.xml</include>
    <include>**/*.txt</include>
    <include>**/*.keystore</include>
  </includes>
</resource>

服务端加密/解密接口 
// post
http://localhost:8085/encrypt?data=123456
"data":"just do it."

// post
http://localhost:8085/decrypt

3.git -- branch --> master
#{cipher} -- 密文标识,需要服务端解密
zg-config-repo/config-repo/micro-order-dev.properties
username=username--dev
redis.password={cipher}AQBvqElwsAV2Tl3Fz6KUn0ZdB0pC...................

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我三师弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值