SpringCloud学习之SpringCloudBus
一。spring-cloud-bus是什么?
回答这个问题之前,我们先回顾先前的分布式配置,当配置中心发生变化后,我们需要利用spring-boot-actuator里的refresh端点进行手动刷新:

根据上述示例情况:我们每次要获取最新配置时,要一个一个的通过refresh刷新服务节点,这种方式是不是非常low而且非常麻烦,那该怎么办呢?
大家还记得zookeeper中watch是做什么用的吗?当监控的节点数据发生变化了,那么是不是所有订阅到该节点的客户端都会触发一个订阅回调呢?这其实也类似于我们的消息总线。在微服务架构的系统中,我们通常会使用轻量级的消息代理来构建一个公有的消息主题让系统中所有微服务实例都连接上来,由于该主题中产生的消息会被所有实例监听和消费,所以我们称它为消息总线。
那么在分布式配置中,我们的所有服务都订阅消息总线的话,当配置改变时,配置中心通知消息总线,然后所有的服务节点接收到订阅消息后,在从配置中心获取最新的配置,是不是一个一劳永逸的过程?那么可以得到如下结构图:

二、实现分布式配置的消息总线
我先贴出:项目结构图

我们统一把配置放在config目录下
1、在对应的服务添加对应的依赖
首先我们先配置config-server。gradle配置文件:
dependencies {
// testCompile group: 'junit', name: 'junit', version: '4.12'
compile('org.springframework.cloud:spring-cloud-config-server')
compile('org.springframework.kafka:spring-kafka')
compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
注意我们使用kafka作为消息总线的中间件
然后我们依次在所需的服务中添加对应的依赖
dependencies {
// testCompile group: 'junit', name: 'junit', version: '4.12'
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.kafka:spring-kafka')
compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
2、配置对应的application.yml
config-server的yml文件:
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: file://${user.home}/IdeaProjects/spring-cloud
repos:
local:
pattern: '**/local'
uri: file://${user.home}/IdeaProjects/spring-cloud
searchPaths: config
search-paths: config
label: master
kafka:
bootstrap-servers: localhost:9092
server:
port: 8888
endpoints:
refresh:
sensitive: false
bus:
sensitive: false
这里面注意要把端点先开放出来,然后进行kafka的相关配置,其余服务的配置文件也进行这样的操作,使其能与消息总线通讯。
3、依次启动服务
当服务启动成功时,SpringBootActuator给我们提供一个/bus/refresh端点,同时我们可以在kafka主题里面找到相应的topics,其名字为springCloudBus:
4、访问Config-Server的/bus/refesh
我们先使用kafka的消费端来监听一下消息内容。运行:
./kafka-console-consumer.sh --bootstrap-server localhost: --topic springCloudBus
紧接着我们在访问 http://localhost:8888/bus/refresh 刷新config-server的配置
然后我们可以发现消费端,订阅到如下消息:

其中:type为消息的事件类型
timestamp 为消息的时间戳
orginService:消息的来源服务实例
destinationService:消息的目标服务实例,**代表了总线上的所有服务实例
在本例子中,我们可以看到每个服务的ackId都来自于 type为RefreshRemoteApplicationEvent的服务ID
5、运行服务
我们先通过gradle的build任务进行打包会得到如下文件:xxxx.jar与xxx.jar.orginal
那么进入到对应目录下 启动两个服务并注册到注册中心 命令如下:
java -Dserver.port= -Dspring.profiles.active=local -jar xxxx.jar
java -Dserver.port= -Dspring.profiles.active=local -jar xxxx.jar
注意一定不要在application.yml配置如上参数,否则通过(-Dxxx=xxx)系统变量设置的值将不会生效
此时我们更改config对应的配置并commit后,在运行步骤4,就可以拿到最新的结果了,再次附上相关代码:
StudentConfig:
package com.hzgj.lyrk.order.config; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "student")
@Configuration
public class StudentConfig { private String name; private String age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} @Override
public String toString() {
return "StudentConfig{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
order-server-local:
student:
name: student_local
age: 17
SpringCloud学习之SpringCloudBus的更多相关文章
- SpringCloud学习之Ribbon
一.负载均衡与Ribbon 负载均衡,在集群中是很常见的一个“名词”,顾名思义是根据一定的算法将请求分摊至对应的服务节点上,常见的算法有如下几种: 轮询法:所有请求被依次分发到每台应用服务器上,每台服 ...
- SpringCloud学习之feign
一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...
- SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)
前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...
- SpringCloud学习(二):微服务入门实战项目搭建
一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...
- SpringCloud学习系列之七 ----- Zuul路由网关的过滤器和异常处理
前言 在上篇中介绍了SpringCloud Zuul路由网关的基本使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由 ...
- springCloud学习总览
写完最后一篇特意去看了看第一篇是什么时候写的---2018/11/19,到现在三个月多一点,总的来说这三个月通过<Spring 微服务实战>这本书,算是对微服务进行了一次扫盲学习. ...
- SpringCloud学习笔记(2):使用Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...
- SpringCloud学习笔记(3):使用Feign实现声明式服务调用
简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...
- SpringCloud学习笔记(4):Hystrix容错机制
简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...
随机推荐
- Count on a tree
bzoj 2588: Spoj 10628. Count on a tree http://www.lydsy.com/JudgeOnline/problem.php?id=2588 Descrip ...
- 移动端H5活动页优化方案
背景 项目:移动端H5电商项目 痛点:慢!!! 初始方案:最基本的图片懒加载,静态资源放到cdn,predns等等已经都做了.但是还是慢,慢在哪? 显而易见的原因:由于前后端分离,所有的数据都由接口下 ...
- SQL SERVER 字符串按数字排序
需求是这样的: 数据库表里面有一个字段类型是nvachar,存的值是数字和字符混合的,要实现先按数字排序,再按字母倒序. 思路: 考虑这个字段的值是否是有规律可循的,把要按数字排序的部分转换为数字,再 ...
- 深度学习之 seq2seq 进行 英文到法文的翻译
深度学习之 seq2seq 进行 英文到法文的翻译 import os import torch import random source_path = "data/small_vocab_ ...
- ( 转 ) CORS 有一次 OPTIONS 请求的原理
刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS-- 目前的工作中,HEAD.PUT.DELE ...
- emqtt 试用(四)emq 的主题访问控制 acl.conf
访问控制(ACL) EMQ 消息服务器通过 ACL(Access Control List) 实现 MQTT 客户端访问控制. ACL 访问控制规则定义: 允许(Allow)|拒绝(Deny) 谁(W ...
- Spring Security 入门(1-8)缓存EhCache
- 新概念英语(1-73)The way to King Street
The way to King Street 到国王街的走法Why did the man need a phrasebook?Last week Mrs. Mills went to London. ...
- CodeForces 1B-字符串,进制转换与数学
一个萌新的成长之路 Background 同学们都回家了,只有我和wjh还有邢神在机房敲代码,吃random口味的方便面-- Description Translated by @PC_DOS fro ...
- requests+正则爬取豆瓣图书
#requests+正则爬取豆瓣图书 import requests import re def get_html(url): headers = {'User-Agent':'Mozilla/5.0 ...