SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.SpringCloud Config 就可以解决该问题.
本文阐述SpringCloud配置中心和配置客户的架构
为了配置中心的高可用和服务化,使用Eureka作为注册中心,并把配置中心注册到Eureka注册中心,此时就可以多开几个配置中心来高可用集群,
为了把配置的变更刷新到客户端中的任务交给配置中而不是客户端,使用SpringCloud Bus+RabbitMQ
下面先阐述配置中心的搭建
配置中心
在pom.xml中引入依赖
springboot的版本为1.5.15
springcloud的版本为Edgware.SR4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
与spring-cloud相关的属性必须配置在bootstrap.yml中,config部分内容才能被正确加载。因为config的相关配置会先于application.yml,而bootstrap.properties的加载也是先于application.yml,主要配置了git
bootstrap.yml
server:
port: 5001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/lhc0512/config.git
search-paths:
username: username
password: password
在application.yml中配置rabbitmq,
注意配置management.security.enabled=false,
允许使用window的 curl -X POST http://localhost:5001/bus/refresh 更新消息总线
eureka:
instance:
prefer-ip-address: true
instance-id: config-server:5001
client:
service-url:
defaultZone: http://localhost:7001/eureka/
management:
security:
enabled: false
spring:
rabbitmq:
host: 120.73.233.104
port: 5672
username: guest
password: guest
在启动类中使用@EnableConfigServer使之为注册中心server,并注册到eureka中
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
启动后
我在github上创建仓库https://github.com/lhc0512/config.git
并创建文件config-dev.properties”
想访问config-dev.properties的详细信息
访问http://localhost:5001/config/dev
内容如下
{
"name": "config",
"profiles": [
"dev"
],
"label": null,
"version": "25abae735e19b2c0ac2e975cd0e112083fae1204",
"state": null,
"propertySources": [
{
"name": "https://github.com/lhc0512/config.git/config-dev.properties",
"source": {
"hello": "hello dev"
}
}
]
}
想访问config-dev.properties的内容
访问http://localhost:5001/config-dev.properties
内容如下
hello: hellodev8
更改github的文件,会发现服务端自动更新
client
接下来阐述客户端的搭建
在pom.xml引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
由于bootstrap.yml会先加载,springcloud config 都配置在这里,主要配置了rabbitmq 开启springcloud bus的消息跟踪,并注册为eureka服务
bootstrap.yml
spring:
application:
name: config-client
rabbitmq:
host: 120.77.245.104
port: 5672
username: guest
password: guest
cloud:
bus:
trace:
enabled: true #消息跟踪
config:
name: config #github上的文件名
profile: dev #运行环境
label: master #分支
discovery:
enabled: true #发现服务
service-id: config-server #服务名
eureka:
instance:
prefer-ip-address: true
instance-id: config-client:5011
client:
service-url:
defaultZone: http://localhost:7001/eureka/
server:
port: 5011
在启动类中,开启服务发现@EnableDiscoveryClient
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
在controller写RestAPI暴露配置的信息
@RefreshScope @RestController public class ConfigController { @Value("${hello}") private String hello; @RequestMapping("/getHello") public String from() { return this.hello; } }
终于把客户端的搭建讲完了,下面终于可以说一下配置更新一事.
之前更改github上的文件,配置中心更新了,但会发现,客户端并没有更新,使用SpringCloud Bus+RabbitMQ的意义在于,把客户端更新的任务交给配置中心,此时想让客户端更新很简单
使用如下方式让客户端更新,在window的cmd中,输入如下的hook
curl -X POST http://localhost:5001/bus/refresh
会发现调用http://localhost:5011/getHello的restAPI,已经更新
还没有评论,来说两句吧...