随着线上项目变的日益庞大,每一个项目都散落着各类配置文件,若是采用分布式的开发模式,须要的配置文件随着服务增长而不断增多。某一个基础服务信息变动,都会引发一系列的更新和重启,运维苦不堪言也容易出错。就是在这种背景下,基本上BAT的没加公司都研发了配置中心,这里不列举.html
Spring Cloud Config就是Spring Cloud团队研发的配置中心,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为服务端和客户端. 服务端是一个独立的微服务应用,用来链接配置仓库并未客户端提供获取配置信息等的访问接口; 客户端则是微服务中的各个微服务应用,经过指定的配置中心来管理应用资源和业务相关的配置内容,并在启动的时候从配置中心中加载配置信息.git
Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,因此使用 Spring Cloud Config 构建的配置服务器,自然就支持对微服务应用配置信息的版本管理,而且能够经过 Git 客户端工具来方便的管理和访问配置内容。固然它也提供了对其余存储方式的支持,好比:SVN 仓库、本地化文件系统。这里先使用github来演示案例(也可使用码云).github
内容分别是:web
wz.hello=hello in dev wz.hello=hello in prod wz.hello=hello in test
引入依赖:算法
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
配置文件:spring
spring.application.name=config-server # 帐号密码不予展现 spring.cloud.config.server.git.username= spring.cloud.config.server.git.password= spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config # git仓库地址下的相对地址,能够配置多个,用,分割。本人的在根目录 spring.cloud.config.server.git.search-paths= server.port=12000
启动类添加注解:docker
@SpringBootApplication @EnableConfigServer public class CloudConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigDemoApplication.class, args); } }
以后config server项目就搭建好了,启动以后就能够访问了.http://localhost:12000/wz-config-client-prod.properties就能够访问内容了,固然还有另外一种方式是: http://localhost:12000/wz-config.client/prod效果是同样的.json
仓库中的配置文件会被转换成web接口,访问能够参照如下的规则:bootstrap
以wz-config-client-prod.properties为例子,它的application是wz-config-client,profile是prod。client会根据填写的参数来选择读取对应的配置。windows
引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
application.properties的内容
server.port=13000 spring.application.name=config-client management.endpoints.web.exposure.include=* spring.output.ansi.enabled=ALWAYS
bootstrap.properties的内容
# 配置文件的application spring.cloud.config.name=wz-config-client # 配置文件的profile spring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:12000/ spring.cloud.config.label=master
启动类无需改变,接下来建立个controller
@RestController @RefreshScope public class HelloController { @Value("${wz.hello:null}") private String hello; @GetMapping("/hello") public String hello(){ return hello; } }
这下就能够启动访问了,访问hello就能够经过客户端访问配置中心了.
上面bootstrapproperties的内容:
注意的是上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部份内容才能被正确加载。由于config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。
还有就是那个actuator和@RefreshScope的做用是刷新,也就是说当github上的配置文件更新了以后,post请求http://localhost:13000/actuator/refresh就能够起到动态刷新的做用,不用从新启动项目能够获取到更新后的配置.
至于post请求创建本地下载了postman能够模拟请求.
WebHook是当某个事件发生时,经过发送http post请求的方式来通知信息接收方。Webhook来监测你在Github.com上的各类事件,最多见的莫过于push事件。若是你设置了一个监测push事件的Webhook,那么每当你的这个项目有了任何提交,这个Webhook都会被触发,这时Github就会发送一个HTTP POST请求到你配置好的地址。
如此一来,你就能够经过这种方式去自动完成一些重复性工做,好比,你能够用Webhook来自动触发一些持续集成(CI)工具的运做,好比Travis CI;又或者是经过 Webhook 去部署你的线上服务器。下图就是github上面的webhook配置。
events事件类型 | 描述 |
push | 仓库有push时触发。默认事件 |
create | 当有分支或标签被建立时触发 |
delete | 当有分支或标签被删除时触发 |
svn也有相似的hook机制,每次提交后会触发post-commit脚本,咱们能够在这里写一些post请求
这样咱们就能够利用hook的机制去触发客户端的更新,可是当客户端愈来愈多的时候hook支持的已经不够优雅,另外每次增长客户端都须要改动hook也是不现实的。后面会有消息总线bus也会这个功能
前面都是config server和config client的案例,那如何注册到注册中心呢,须要什么改动能实现服务化和高可用呢?下面案例
Spring Cloud Config就到这了.配置中心还有不少,好比Apollo等.
新增eureka依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.RELEASE</version> </dependency>
添加注解
@SpringBootApplication @EnableConfigServer @EnableDiscoveryClient public class CloudConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigDemoApplication.class, args); } }
添加配置文件
spring.application.name=config-server spring.cloud.config.server.git.username=**** spring.cloud.config.server.git.password=**** spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config # git仓库地址下的相对地址,能够配置多个,用,分割。本人的在根目录 spring.cloud.config.server.git.search-paths= server.port=12000 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.RELEASE</version> </dependency>
添加配置:
server.port=13000 spring.application.name=config-client management.endpoints.web.exposure.include=* spring.output.ansi.enabled=ALWAYS eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
添加注解:
@SpringBootApplication @EnableDiscoveryClient public class CloudConfigClientDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigClientDemoApplication.class, args); } }
上面就实现了配置中心服务化(注册到eureka上)
配置文件的修改:
spring.application.name=config-server spring.cloud.config.server.git.username=***** spring.cloud.config.server.git.password=***** spring.cloud.config.server.git.uri=https://github.com/MissWangLove/cloud-config # git仓库地址下的相对地址,能够配置多个,用,分割。本人的在根目录 spring.cloud.config.server.git.search-paths= server.port=12001 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
以后启动eureka-server,config-server-demo,config-server-demo1,config-client-demo;就能够看到eureka的注册中心会有3个服务,两个config-server,一个config-client,访问http://localhost:12000/wz-config-client/prod和http://localhost:12001/wz-config-client/prod就能够看到相同的效果,这样当一个挂掉,另外一个还能够正常运行.
前面说了github上的配置文件若是更新的话,须要post发送refresh才能刷新,还有一种就是经过消息总线(bus),这的内容看: http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html,由于这个实现须要消息中间件kafka或者RabbitMQ,须要一个虚拟机或者阿里云服务器,本人的到期了,等弄好了再进行测试.
本人在windows上安装了个docker,以后建立了个rabbitmq容器,这个过程仍是很简单了,自行百度就好.
添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
添加配置文件:
# rabbitmq spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
启动类添加注解:
@SpringBootApplication @EnableConfigServer @EnableDiscoveryClient @RefreshScope public class CloudConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigDemoApplication.class, args); } }
上卖弄server端就修改完成,以后在github上修改配置文件,这边会自动刷新
添加依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
添加配置文件:
# rabbitmq spring.cloud.bus.enabled=true spring.cloud.bus.trace.enabled=true spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
添加注解:
@SpringBootApplication @EnableDiscoveryClient @RefreshScope public class CloudConfigClientDemoApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigClientDemoApplication.class, args); } }
本人在修改的时候还在controller上加了@RefreshScope注解,可是仍是不能起到自动刷新的做用,也就是在github上进行修改以后,必须在客户端执行curl -X POST http://localhost:13000/actuator/bus-refresh/才能够起到刷新的效果.