springcloud的高可用配置中心-Config
目录
4.使用rabbitmq和springcloud-bus动态半自动刷新和全局通知
1.在进行分布式框架中,如何统一管理各个服务的配置文件
在分布式中,当我们把搭建多个分布式 服务时。我们如何统一管理(或者叫动态管理)。就举一个简单的例子,当注册中心eureka的地址改变,我们就需要到各个服务中去修改配置文件,然后再全部重启,这样是不是很繁琐还容易出错。那我们思考一下:a.将所有的有效的配置文件放到统一的一个仓库中(如github或者码云等)有错统一改?b.如何在修改完配置文件后不重启服务也能让配置文件生效。
2.搭建spirngclodu的配置中心Connfig
(1)appilication.yml
info:
name: 统一配置中心(基于spirngboot2.0.3.RELEASE+springcloud-F)
version: 2.0.3.RELEASE
server:
port: 2001
management: ##security的秘密
security:
enabled: false
logging: ###日志
level:
root: info
(2)bootstrap.yml(区别于appliction.yml动态资源与静态资源配置文件分开)
spring:
application:
name: config-ha
cloud:
config:
server:
git:
uri: https://gitee.com/mxzOfxiaoguaiguai/monxz #码云地址
search-paths: my #文件夹,实际生产中用项目名命名
username:xxxxx
password: xxxxx
eureka: #eureka注册
instance:
hostname: localhost # 服务提供者应用所在的主机(本地测试的话,使用localhost或者127.0.0.1即可,或者在host文件中指定一个虚拟域名也可以)
lease-renewal-interval-in-seconds: 5 # 心跳时间,表示eureka client发送心跳给eureka server端的频率(缺省为30s)
client:
service-url:
defaultZone: http://admin:[email protected]:1002/eureka,http://admin:[email protected]:1003/eureka,http://admin:[email protected]:1001/eureka
(3)application.java
@SpringBootApplication
@EnableDiscoveryClient //隐藏ureka
@EnableConfigServer //声明config服务端
public class ConfigHaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigHaApplication.class, args);
}
}
3.客户端测试
(1)该目录下有lisiService-dev.porperties的配置
(2)访问:localhost:2001/lisiService-dev.porperties显示
明显已经获取到仓库中的配置了
(3)注意关于仓库中的yml文件的命名建议百度
(3)创建客户端测试数据
a.客户端的yml配置 application.yml
server:
port: 8225
spring:
application:
name: foot ##注意这里的命名一定规范,比如我要加载仓库里的foot-dev.yml文件(foot:就是这里的name,dev:是环境在下面 的配置中)
b.bootstrap.yml
# spring.cloud.config.uri 参考 https://segmentfault.com/a/1190000006138698
spring:
cloud:
config:
discovery:
enabled: true # 开启Config服务发现支持
serviceId: config-ha # 指定配置中心服务名,也就是config server端spring.application.name的值,具体信息请查看 \sea-server-config\src\main\resources\application.yml
label: master # 配置仓库的分支
profile: dev # 环境(就是上面的foot-dev.yml的中的dev)
eureka的配置略
c.使用controller的访问
访问foot-dev.yml中的name的访问:http://localhost:8225/name 出现lisi 说明正确
思考问题:当修改foot-dev.yml中name: zhangsna,在没有重启config.client情况下,用上述url访问发现值没有改变。
4.使用rabbitmq和springcloud-bus动态半自动刷新和全局通知
(1).说明:添加rabbitmq和bus总线用来动态全局刷新配置
(2).yml添加acutor和bus支持
(3).boostrap.yml的配置
(4).Controller
用来访问仓库中profile属性
(5)测试
a.开启服务端
b.以2101和2100俩个端口启动客户端
c.访问localhost:2100/profile(或者localhost:2101/profle)第一次出现 name
d.修改仓库中的yml中的profile为 1024
e.bus手动刷新总线通知post:http://localhost:2100/actuator/bus-refresh(或者http://localhost:2101/actuator/bus-refresh)
f.重复c步骤发现profile值已经改变
ps:1.在springboot1.5x版本中刷新总线请求post:http://locahost:2100/bus/refresh
2.在总线刷新中还有一种自动刷新的方法;即修改仓库中的yml文件就会立即刷新,不用发送上述的post请求,但是在实际
业务中我们明显只需要这种半自动刷新,有兴趣可以百度:bus+webhook自动刷新