一、SpringCloud Config

1、前言

  • 微服务意味着要将单体应用中的业务拆分成一个个子服务, 每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
  • SpringCloud提供了ConfigServer来解决这个问题, 我们每-个微服务自己带着一个application.yml, 上百个配置文件的管理…/(ToT)/~~

2、是什么?

  • SpringCloud Config为微服务架构中的微服务提供集化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了-个中心化的外部配置。

3、怎么玩?

  • SpringCloud Config分为服务端和客户端两部分。
  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,组可以通过git客户端工具来方便的管理和访问配置内容

4、能干嘛?

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/releasco
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露

5、与GitHub整合配置

  • 由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式

6、Config服务端配置与测试

  • pom
 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
  • yml
server:port: 3344
spring:application:name: cloud-config-centercloud:config:server:git:uri: https://github.com/rwnb/springcloud-config.git#uri: git@github.com:rwnb/springcloud-config.gitsearch-paths:- springcloud-configskip-ssl-validation: true #记得一定要写 要不会报SSL异常的label: master
eureka:client:service-url:defaultZone:  http://localhost:7001/eureka
  • 启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterMain3344 {public static void main(String[] args) {SpringApplication.run(ConfigCenterMain3344.class,args);}
}

7、读取配置规则

label:分支(branch)
name :服务名
profiles:环境(dev/test/prod)

  • /{label}/{application}-{profile}.yml

    • master分支

      • http://config-3344.com:3344/master/config-dev.yml
      • http://config-3344.com:3344/master/config-test.yml
      • http://config-3344.com:3344/master/config-prod.yml
    • dev分支
      • http://config-3344.com:3344/dev/config-dev.ymI
      • http://config-3344.com:3344/dev/config-test.ymI
      • http://config-3344.com:3344/dev/config-prod.ymI
  • /{application}-{profile}.yml
    • http://config-3344.com:3344/config-dev.yml
    • http://config-3344.com:3344/config-test.yml
    • http://config-3344.com:3344/config-prod.yml
  • /{application}-{profile}/{label}
    • http://config-3344.com:3344/config/dev/master
    • http://config-3344.com:3344/config/test/master
    • http://config-3344.com:3344/config/prod/master

8、Config客户端配置与测试

(1)bootstrap.yml介绍
  • applicaiton. yml是用户级的资源配置项
  • bootstrap. yml是系统级的,优先级更加高
  • Spring Cloud会创建一个"Bootstrap Context",作为Spring应用的Application Context的父上下文。初始化的时候,BootstrapContext’负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
  • Bootstrap’属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context’ 和Application Context有着不同的约定所以新增了-个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
  • 要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
  • 因为bootstrap.ym是比application.yml先加载的。bootstrapyml优先级高于application.yml
(2)pom
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • bootstrap.yml
server:port: 3355spring:application:name: config-clientcloud:config:label: master #分支名称name: config #配置文件名称aprofile: dev #上述三个分支config-dev.yml的配置文件将被读取:http://localhost:3344/master/config-dev.ymluri: http://localhost:3344
eureka:client:service-url:defaultZone: http://eureka7001.com:7001/eureka
  • 启动类
package com.rw.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {public static void main(String[] args) {SpringApplication.run(ConfigClientMain3355.class,args);}
}
  • 存在问题:修改了GitHub上的配置信息 客户端3344可以跟随动态刷新了 但是3355需要重启后才能刷新–>噩梦
9、config客户端之动态刷新
  • 修改pom引入actuator监控
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 控制层加自动刷新注解
@RefreshScope
  • 光凭借这些是不能实现动态刷新的,此时需要修改配置后 发送post请求获取最新配置

  • curl -X POST “http:// loca I host : 3355/ actuator/refresh”

  • 问题,如果有很多台机器的话,逐个发送post请求是不是很不方便,当然可以写脚本来实现,但是是否可以一出广播,针对接收自动刷新呢?抱歉 config做不到,所以引入了消息总线↓↓↓↓↓↓↓

消息总线

二、SpringCloud Bus

1、概述

  • 分布式自动刷新配置功能
  • SpringCloud Bus配合SprignCloud Config使用可以实现配置的动态刷新

2、是什么?

  • Bus支持两种消息代理:RabbitMQ和Kafka
  • Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。

3、能干嘛?

  • Spring Cloud Bus能管理和传播分布式系统间的消息,就像一 个分布式执行器,可用于广播状态更改、事件推送等, 也可以当作微服务间的通信通道

4、为何被称为总线

  • 什么是总线

    • 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一 个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播- -些需要让其他连接在该主题上的实例都知道的消息。
  • 基本原理
    • ConfigClient实例都监听MQ中同一 个topic(默认是springCloudBus)。 当- 个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。
      https://www. bilili.com/video/av55976700?from=search&seid=15010075915728605208

5、RabbitMQ环境配置

  • 安装Erlang,下载地址: http://erlang.org/download/otp_win64_21.3.exe
  • 安装RabbitMQ:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe
  • 进入sbin目录以管理员身份打开cmd 运行rabbitmq-plugins enable rabbitmq_ management
  • 启动完成之后访问:http://localhost:15672/
  • 默认密码:guest guest

6、SpringCloud Bus 动态刷新全局广播

  • 采用通知消息总线,总线分发消息的形式实现动态刷新
  • 服务端加入pom
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yml
server:port: 3344
spring:application:name: cloud-config-centercloud:config:server:git:uri: https://github.com/rwnb/springcloud-config.git#uri: git@github.com:rwnb/springcloud-config.gitsearch-paths:- springcloud-configskip-ssl-validation: truelabel: masterrabbitmq:host: localhostport: 5672username: guestpassword: guest
eureka:client:service-url:defaultZone:  http://localhost:7001/eureka# rabbitmq相关的
management:endpoints:web:exposure:include: 'bus-refresh'
  • 客户端 也引入pom
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 客户端 bootstrap.yml修改
server:port: 3355spring:application:name: config-clientcloud:config:label: master #分支名称name: config #配置文件名称aprofile: dev #上述三个分支config-dev.yml的配置文件将被读取:http://localhost:3344/master/config-dev.ymluri: http://localhost:3344rabbitmq:host: localhostport: 5672username: guestpassword: guest
eureka:client:service-url:defaultZone: http://eureka7001.com:7001/eureka# 暴露监控端点
management:endpoints:web:exposure:include: "*"
  • 最终效果:更改GitHub上配置文件 然后去给3344发送广播:curl -X POST “http://localhost:3344/actuator/bus-refresh”
  • 就能做到移除广播处出接收修改

7、SpringCloud Bus 动态刷新定点通知

  • 不想全部通知,只想定点通知: 只通知3355不通知3366
  • curl -X POST “http://localhost:3344/actuator/bus-refresh/config-client:3355”
  • curl -X POST “http://localhost:服务中心/actuator/bus-refresh/微服务名:端口”

SpringCloud Config配置中心、SpringCloud Bus消息总线相关推荐

  1. SpringCloud config 配置中心集群配置以及整合消息总线BUS实现关联微服务配置自动刷新

    一.SpringCloud Config 基本配置中的问题 在上一章节<SpringCloud config 配置中心介绍与基本配置使用>中我们现实了配置中心的配置集中管理.调用微服务应用 ...

  2. Java之 Spring Cloud 微服务的 SpringCloud Config 配置中心(第四个阶段)【二】【SpringBoot项目实现商品服务器端调用】

    SpringCloud学习目录点击跳转对应的文章 Java之 Spring Cloud 微服务搭建(第一个阶段)[一][SpringBoot项目实现商品服务器端是调用] Java之 Spring Cl ...

  3. SpringCloud —— Config 配置中心

    前文 SpringCloud 简介 SpringCloud 版本选型 SpringCloud 工程构建 SpringCloud -- Eureka 注册中心 SpringCloud -- Eureka ...

  4. springcloud config配置中心概述

    Spring Cloud Config简介 Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Co ...

  5. SpringCloud config 配置中心介绍与基本配置使用

    一.SpringCloud Config 介绍 出现背景:在微服务架构中,在没有配置中心出现时,我们每个应用的配置信息都在其配置文件application.properties中维护.加入整个系统中有 ...

  6. springcloud config配置中心 访问报错:java.security.InvalidAlgorithmParameterException: the trustAnchors param

    1. 报错信息 org.eclipse.jgit.api.errors.TransportException: https://github.com/fenggbinn/springcloud-con ...

  7. SpringCloud学习笔记(十) Bus 消息总线

    目录 一.SpringCloud Bus 介绍 1.消息总线的由来 2.是什么 3.能干嘛 4.总线 1)什么是总线 2)基本原理 二.RabbitMQ环境配置 三.动态刷新全局广播 1.搭建客户端微 ...

  8. SpringCloud微服务架构,Config 分布式配置中心,Bus 消息总线, Stream 消息驱动,Sleuth+Zipkin 链路追踪

    Config分布式配置中心 Config 概述 概述 • Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护. • 好处: • 集中管理配置文件 • 不同环境不同配 ...

  9. SpringCloud版本Hoxton SR5 --- 第七讲:SpringCloud Config 分布式配置中心+整合bus、rabbitmq、actuator

    传送门:SpringCloud版本Hoxton SR5 --- 第一讲:认识 先看SpringCloud Config 可以完成的功能,或者说他在项目中的定位和作用. SpringCloud conf ...

最新文章

  1. Python --深入浅出Apriori关联分析算法(二) Apriori关联规则实战
  2. Android 中使用MediaRecorder进行录像详解(视频录制)
  3. python+flask搭建CNN在线识别手写中文网站
  4. 解决iview中</Input>标签报错的方法
  5. Oracle 执行计划篇
  6. Unity中光照的实践与总结
  7. 计算机跳过硬盘检查,电脑开启时怎样才能跳过磁盘检测?
  8. 【Codeforces613D】Kingdom and its Cities【虚树】【Tree DP】
  9. matlab将图片旋转的代码_我的MATLAB魔方新玩法:拼出任意图案!
  10. 2019华为网络精英挑战赛参赛体验
  11. 维特比算法的简单实现
  12. 智能家居云服务器设计规格
  13. bulk of the 用法_初中英语语法总结:冠词用法详解
  14. ThreadPoolExecutor线程池的使用
  15. 场景麻将识别开发_基于EMGUCV(一)
  16. android毕业设计——基于Android+Tomcat+JavaEE的旧物交易平台设计与实现(毕业论文+程序源码)——旧物交易平台
  17. ssm查询,错误Could not find result map cn.itcast.ssm.po.ItemsCustom
  18. Maven搭建多子项目工程
  19. idea配置xml约束问题
  20. 手撕公司SSO登陆原理

热门文章

  1. 从Cell学单细胞转录组分析(一):开端
  2. Android SDK开发技术分享
  3. 我想转行做软件测试,有必要报培训班吗?我听说好多人说自学就行...
  4. Serial Box for Mac(软件序列号查询软件)
  5. abb机器人指令手册_ABB机器人控制器死机故障维修
  6. 清空mailq 队列里面的邮件
  7. 通过代码学习 VQ-VAE
  8. Tauri 引入element-plus
  9. opencv mingw-32 编译常见问题
  10. 湖北省经济和信息化委员会主任欧阳万坤调研达梦数据库