为何要统一管理微服务配置?git
随着微服务不断的增多,每一个微服务都有本身对应的配置文件。在研发过程当中有测试环境、UAT环境、生产环境,所以每一个微服务又对应至少三个不一样环境的配置文件。这么多的配置文件,若是须要修改某个公共服务的配置信息,如:缓存、数据库等,不免会产生混乱,这个时候就须要引入Spring Cloud另一个组件:Spring Cloud Config。 github
有哪几种?
Apollo(阿波罗)是携程框架部门研发的配置管理平台,可以集中化管理应用不一样环境、不一样集群的配置,配置修改后可以实时推送到应用端。spring
apollo项目基于springboot与springcloud,能够独立部署数据库
Apollo GitHub地址:bootstrap
https://github.com/ctripcorp/apollo缓存
今天重点SpringCloud-config
compile 'org.springframework.cloud:spring-cloud-config-server'
客户端Gradlespringboot
compile 'org.springframework.cloud:spring-cloud-starter-config'
启动类引入ConfigServer表明为配置中心服务端架构
package org.gd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * @DATA 2019-03-11 09:10 * @Author 张国伟 WeChat:17630376104 * @Description TODO */ @EnableConfigServer @SpringBootApplication public class ConfigConter { public static void main(String[] args) { SpringApplication.run(ConfigConter.class, args); } }
建立三个bootstrapapp
咱们把配置文件放在Gitlab上,在Gitlab上建立yml框架
建立yml
spring: application: name: config-center cloud: config: server: git: uri: https://gitlab.com/zgw1469039806/config-center ###git地址 clone-on-start: true #默认状况下,配置服务会在配置文件第一次被请求时远程的配置库.固然也能够配置为在启动时clone远程的配置库 search-paths: local #选择是那个配置 username: ****** #git帐号密码 password: ****** server: port: 9999clone
spring: application: name: config-center cloud: config: server: git: uri: https://gitlab.com/zgw1469039806/config-center.git clone-on-start: true search-paths: test username: ***** password: ***** server: port: 9999
没错 两个个就是环境不同
spring:
profiles:
active: test
用主yml来负责启动时的切换测试环境仍是生产环境,若是配置没有错误的话直接访问能够访问到yml
直接访问yml的名字便可,ok到这里配置中心已经配置完毕,那咱们再来看下客户端怎么配置
spring: application: name: project-shopping-mall cloud: config: uri: http://localhost:9999
客户端很简单,指向config服务端就能够
咱们能够看到客户端直接读取到git上面,说明配置已经成功。
项目GitHub地址:https://github.com/zgw1469039806/springcloud-project