微服务 Spring Boot 2.0 + Spring cloud + consul + Hystrix + zuul + config + feign (一)
上篇介绍:consul 安装,spring boot 2.0 使用,ribbon,feign 调用接口。
接下来还有目录结构:
- config:配置中心(本篇)
- zuul :网关总线
- hystrix:断路保护机制
- sleuth+zipkin:链路追踪系统(sleuth 不了解的可以看看这篇简介:sleuth 简介)
1.配置中心
spring boot 2.0 快速搭建:
没有别的,只要 pom 中引入下列依赖,然后两步;
- 注册到 consul
- 添加 @EnableConfigServer 注解;
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
application.properties :
- github 作为我的配置存放地址;
- 如果公开的直接免密码拿取;
- 选择分支master
server.port=7070
#spring.cloud.config.server.default-application-name=config-server
spring.application.name=consul-config-server
spring.sleuth.sampler.probability=1
spring.zipkin.base-url=http://192.168.99.100:32768
#eureka.client.service-url.defaultZone=http://slave1:8761/eureka/,http://slave2:8762/eureka/
# git
spring.cloud.config.server.git.uri=https://github.com/TorGor/config-center.git
#path
spring.cloud.config.server.git.search-paths=testConfig
# label branch
spring.cloud.config.label=master
# if git public ,ignore these
# username
#spring.cloud.config.server.git.username=TorGor
# pwd
#spring.cloud.config.server.git.password=*******
# default open health,info ,* just for test
management.endpoints.web.exposure.include=*
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
我的GitHub 配置中心:
配置文件中简单写两个:
在consul-client-invoker 服务 中使用配置:
- 使用 bootstrap.properties ;
- @Value("${myname}") 直接调用配置中心的配置;
- 也可以在自己的properties 文件中使用:
myconfig.remote.invoker=${invoker}