失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ

SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ

时间:2020-04-26 10:00:43

相关推荐

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: 5001spring:application:name: config-servercloud:config:server:git:uri: /lhc0512/config.git search-paths:username: usernamepassword: password

在application.yml中配置rabbitmq,

注意配置management.security.enabled=false,

允许使用window的 curl -X POST http://localhost:5001/bus/refresh 更新消息总线

eureka:instance: prefer-ip-address: trueinstance-id: config-server:5001client:service-url:defaultZone: http://localhost:7001/eureka/ management:security:enabled: false spring: rabbitmq:host: 120.73.233.104port: 5672username: guestpassword: guest

在启动类中使用@EnableConfigServer使之为注册中心server,并注册到eureka中

@EnableDiscoveryClient@EnableConfigServer@SpringBootApplicationpublic class ConfigApplication {public static void main(String[] args) {SpringApplication.run(ConfigApplication.class, args);}}

启动后

我在github上创建仓库/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": "/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-clientrabbitmq:host: 120.77.245.104port: 5672username: guestpassword: guestcloud:bus:trace:enabled: true #消息跟踪config: name: config #github上的文件名profile: dev #运行环境label: master #分支 discovery: enabled: true #发现服务service-id: config-server #服务名eureka: instance:prefer-ip-address: trueinstance-id: config-client:5011client:service-url:defaultZone: http://localhost:7001/eureka/server:port: 5011

在启动类中,开启服务发现@EnableDiscoveryClient

@EnableDiscoveryClient@SpringBootApplicationpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}

在controller写RestAPI暴露配置的信息

@RefreshScope@RestControllerpublic 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,已经更新

如果觉得《SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。