老鸟飞过 , 学习使用,欢迎交流
十二.配置中心Spring Cloud Config 1.理解配置中心 1.1.什么是配置中心在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理我们需要用到置中心组件。在Spring Cloud中,分布式配置中心组件spring cloud config 它可以帮我们集中管理配置文件,修改配置无需重启服务 等,它支持配置文件放在配置服务的本地,也支持放在远程如Git仓库中集中管理。在spring cloud config 分为了服务端 config server和客户端config client 两个角色。
1.2.配置中心工作流程使用spring cloud config管理配置文件,我们需要把微服务的配置文件上传到Git仓库 , 然后搭建独立的ConfigServer服务,ConfigServer除了要从Git仓库拉取配置之外,还要注册到EurekaServer中,我们把ConfigServer本身也当做是一个微服务(当然ConfigServer可以脱离注册中心使用,但是如果客户端使用服务发现的方式指向配置中心就需要注册到注册中心了),至于ConfigClient这需要集成到具体的微服务中,如支付服务,订单服务等。其工作流程是,微服务通过ConfigClient向配置中心ConfigServer发起请求获取配置文件,配置中心从GIT仓库获取配置,然后再一路返回给微服务。
需要注意是,EurekaServer的配置不能交给ConfigServer管理,因为必须要先启动EurekaServer才能启动ConfigServer,试问EurekaServer都没配置怎么启动?
根据上图我们知道,如果要实现配置文件统一管理,我们需要创建自己的远程仓库(当然也可以本地,或者其他仓库),我这里选择“码云”,然后将配置文件上传上去 ,然后我们需要搭建自己的配置中心服务ConfigServer,配置上码云的仓库地址和相关账号,最后我们需要去修改我们的微服务如:zuul网关,支付服务等集成ConfigClient 。
2.Git仓库管理配置 2.1.使用码云创建远程仓库我们用码云作为git仓库 ,创建码云仓库
我这里以zuul网关服务做演示 。 创建zuul配置 在仓库中创建文件 ,我们先将zuul的配置文件复制到码云上 , 并负责好仓库地址以备用。
注意复制好仓库地址,在克隆中取拷贝 , 待会搭建配置中心的时候会用到
搭建工程 springcloud-config-server-1070 ,作为配置中心服务端,搭建好项目结构如下
springcloud-parent pom.xml springcloud-config-server-1070 //配置中心服务 springcloud-eureka-server-1010 springcloud-order-server-1030 springcloud-pay-server-1040 springcloud-user-common springcloud-user-server-1020 springcloud-zuul-server-1060 //网关服务 springcloud-hystrix-turbine-1050 //聚合监控服务3.2.导入依赖
这里导入了Eureka Client基础依赖 ,和config server服务端依赖已经web依赖
<dependencies> <dependency> <groupId>org.springframework.cloud public static void main( String[] args ) { SpringApplication.run(ConfigServerApplication1070.class); } }3.4.配置文件
ConfigServer的配置文件做两个事情,1.注册到EurekaServer,2.配置码云地址
eureka: client: serviceUrl: defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/ #注册中心地址 instance: prefer-ip-address: true #使用ip地址注册 instance-id: config-server:1070 #指定服务的id server: port: 1070 spring: application: name: config-server cloud: config: server: git: #配置远程仓库地址,去仓库中复制 uri: https://gitee.com/little_wolf/springcloud-config-1010.git username: 1462163787@qq.com #仓库是私有的需要账号 password: 你的密码 #search-paths: 路径 #如果配置文件不再仓库的根目录,需要配置查找路径
方式二:在本地管理配置文件
通常情况下,我们会通过 srping.cloud.config.server.git.uri配置远程的Git仓库地址,(如上配置),配置中心服务从git仓库中拉取配置文件,而其它微服务再从配置中心服务获取配置 ,但有的时候因为网络故障等原因我们没办法连接到git仓库,那整个微服务的配置文件都可能会拉取失败,针对于这种情况我们可以把配置文件统一管理在本地磁盘中,如下配置:
spring: application: name: config-server cloud: config: server: git: native: search-locations: classpath:config,file:D:/config #从d:/config 和 classpath:config加载 #配置远程仓库地址 #uri: https://gitee.com/little_wolf/springcloud-config-1010.git #username: 1462163787@qq.com #仓库是私有的需要账号 #password: 你的密码 #search-paths: 路径 #如果配置文件不再仓库的根目录,需要配置查找路径 profiles: active: native #基于本地的配置3.5.测试配置中心
浏览器访问:http://localhost:1070/application-zuul-dev.yml 你可以看到ConfigServer从Git仓库获取到的zuul的配置文件内容如下:
上一步骤我们已经可以通过ConfigServer从码云上获取到配置文件,我们的最终目的是能够让我们的微服务,zuul-server,order-server等都可以从ConfigServer拉取到配置文件,这里功能我们需要给微服务集成ConfigClient来实现。修改“springcloud-zuul-server-1060” zuul工程如下:
4.1.导入依赖config client基础依赖
<dependency> <groupId>org.springframework.cloud关注打赏