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

拼搏现实的明天。 2022-05-15 06:54 339阅读 0赞

随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.SpringCloud Config 就可以解决该问题.
本文阐述SpringCloud配置中心和配置客户的架构
为了配置中心的高可用和服务化,使用Eureka作为注册中心,并把配置中心注册到Eureka注册中心,此时就可以多开几个配置中心来高可用集群,
为了把配置的变更刷新到客户端中的任务交给配置中而不是客户端,使用SpringCloud Bus+RabbitMQ
下面先阐述配置中心的搭建

配置中心

在pom.xml中引入依赖
springboot的版本为1.5.15
springcloud的版本为Edgware.SR4

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-config-server</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-eureka</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-actuator</artifactId>
  16. </dependency>

与spring-cloud相关的属性必须配置在bootstrap.yml中,config部分内容才能被正确加载。因为config的相关配置会先于application.yml,而bootstrap.properties的加载也是先于application.yml,主要配置了git
bootstrap.yml

  1. server:
  2. port: 5001
  3. spring:
  4. application:
  5. name: config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/lhc0512/config.git
  11. search-paths:
  12. username: username
  13. password: password

在application.yml中配置rabbitmq,
注意配置management.security.enabled=false,
允许使用window的 curl -X POST http://localhost:5001/bus/refresh 更新消息总线

  1. eureka:
  2. instance:
  3. prefer-ip-address: true
  4. instance-id: config-server:5001
  5. client:
  6. service-url:
  7. defaultZone: http://localhost:7001/eureka/
  8. management:
  9. security:
  10. enabled: false
  11. spring:
  12. rabbitmq:
  13. host: 120.73.233.104
  14. port: 5672
  15. username: guest
  16. password: guest

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

  1. @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
内容如下

  1. {
  2. "name": "config",
  3. "profiles": [
  4. "dev"
  5. ],
  6. "label": null,
  7. "version": "25abae735e19b2c0ac2e975cd0e112083fae1204",
  8. "state": null,
  9. "propertySources": [
  10. {
  11. "name": "https://github.com/lhc0512/config.git/config-dev.properties",
  12. "source": {
  13. "hello": "hello dev"
  14. }
  15. }
  16. ]
  17. }

想访问config-dev.properties的内容
访问http://localhost:5001/config-dev.properties
内容如下

  1. hello: hellodev8

更改github的文件,会发现服务端自动更新

client

接下来阐述客户端的搭建
在pom.xml引入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-config</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-eureka</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-actuator</artifactId>
  16. </dependency>

由于bootstrap.yml会先加载,springcloud config 都配置在这里,主要配置了rabbitmq 开启springcloud bus的消息跟踪,并注册为eureka服务
bootstrap.yml

  1. spring:
  2. application:
  3. name: config-client
  4. rabbitmq:
  5. host: 120.77.245.104
  6. port: 5672
  7. username: guest
  8. password: guest
  9. cloud:
  10. bus:
  11. trace:
  12. enabled: true #消息跟踪
  13. config:
  14. name: config #github上的文件名
  15. profile: dev #运行环境
  16. label: master #分支
  17. discovery:
  18. enabled: true #发现服务
  19. service-id: config-server #服务名
  20. eureka:
  21. instance:
  22. prefer-ip-address: true
  23. instance-id: config-client:5011
  24. client:
  25. service-url:
  26. defaultZone: http://localhost:7001/eureka/
  27. server:
  28. port: 5011

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

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

在controller写RestAPI暴露配置的信息

  1. @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

  1. curl -X POST http://localhost:5001/bus/refresh

会发现调用http://localhost:5011/getHello的restAPI,已经更新

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读