随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.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-servercloud: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: 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
@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-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
@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,已经更新

SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ相关推荐

  1. springcloud 分布式配置中心 config server config client

    ---------------------------------------------------------------------------------------------------- ...

  2. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  3. 主流微服务配置中心对比 config,nacso和Apollo对比

    主流微服务配置中心对比  config,nacso和Apollo对比 不想打字直接图片对比 转载于:https://my.oschina.net/u/1446314/blog/3022862

  4. Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    上一篇文章,留了一个悬念,Config Client 实现配置的实时更新,我们可以使用 /refresh 接口触发,如果所有客户端的配置的更改,都需要手动触发客户端 /refresh ,当服务越来越多 ...

  5. springcloud 之 配置中心服务 spring cloud config

    开门见山,不做过多的陈述,简单说一下配置中心的作用:在我们的实际项目开发中,我们一般都是一个项目一套配置,就像我之前的springboot项目,每个项目下都有一套resources/applicati ...

  6. Spring Boot + Spring Cloud 实现权限管理系统 配置中心(Config、Bus)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  7. SpringCloud 分布式配置中心Config Hoxton版本

    Spring Cloud Config简介:Spring Cloud Config为分布式系统提供了服务端和客户端用于支持外部配置.使用Config Server可以在所有环境中管理应用程序的外部属性 ...

  8. 六、springcloud之配置中心Config

    一.配置中心提供的核心功能 Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对S ...

  9. SpringCloud: 分布式配置中心(Spring Cloud Config)

    简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config , ...

  10. spring boot pom配置_125 SpringCloud服务配置中心

    1. 为什么需要使用配置中心 (1) 常用的配置管理解决方案有哪些缺点? 硬编码:需要修改代码的话繁琐且风险大: 配置写在properties里面,在集群环境下,需要替换和重启: 写在xml文件中,一 ...

最新文章

  1. placeholder的样式设置
  2. WPF 文本框添加水印效果
  3. linux mysql v_Linux 主机升级MySQL v5.5 性能提升360%
  4. php递归简单例子,php递归json类实例
  5. 云效 > 产品简介 > 产品概述
  6. 做人要无需,做事要务实!
  7. 【特色团队采访】实力队伍鱼遇雨欲语与余比赛经验分享
  8. 是新最全Git命令大全及速记图
  9. css技巧---电子表体字体引入
  10. linux sata 3驱动下载,linux – SSD SATA3驱动器可能存在的问题
  11. mongodb3 重启_“打工人”必备技能 OPPOR9splus重启让手机再战一年|手机|打工人|oppor|splus...
  12. 24张高清无码图,看到就停不下来了...
  13. 403forbiden解决
  14. 免费idc公益接口_数据科学促进社会公益免费开放数据的最佳来源
  15. python设置背景颜色为豆绿色_eclipse 设置豆沙绿保护色,保护眼睛
  16. python爬虫学校正方教务系统获取全部成绩
  17. 8000401a错误解决方案(Excel)
  18. 用c语言编写爱心的代码是什么
  19. 素数 android10万以内,10万以内的质数表?
  20. STM32Cube和HAL库使用初体验-第5季第2部分-朱有鹏-专题视频课程

热门文章

  1. 反激变压器结构设计学习笔记(进阶)
  2. 机器学习笔记 - 使用Face recognition、OpenCV、Python进行人脸识别
  3. QtQuick 技巧 2
  4. Libcef源码下载与编译
  5. 提示msvcr71.dll丢失处理办法
  6. labuladong算法小结
  7. 国产手机下乡难以撼动山寨手机农村市场
  8. Windows10虚拟机安转(详细版)
  9. 世界第4疯狂的科学家,在103岁生日那天去世了
  10. github与git 实现多人开发的配置,使用htps和ssh两种方式