SpringCloud学习笔记(九、SpringCloud Stream)
目录:
- 什么是SpringCloud Stream
- 如何使用SpringCloud Stream
- 消息分流
什么是SpringCloud Stream:
SpringCloud Stream是一个用于构建消息驱动的微服务应用框架。它通过注入,输入、输出通道来与外界通信;因此它很容易实现消息的中转,并且在更换消息中间件的时候不需要该代码,仅需要修改配置即可。支持的消息中间件如RabbitMQ、Kafka等等。
如何使用SpringCloud Stream(以RabbitMQ为例):
1、增加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>
2、增加properties配置
spring.application.name=stream
server.port=7070 # rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest # stream
spring.cloud.stream.bindings.input.destination=customer
spring.cloud.stream.bindings.output.destination=customer
3、启动类加上本工程的消息代理类型
@EnableBinding({Processor.class})
@EnableBinding分为三种类型
)org.springframework.cloud.stream.messaging.Processor:接收和发送消息
)org.springframework.cloud.stream.messaging.Source:仅支持发送消息
)org.springframework.cloud.stream.messaging.Sink:仅支持接收消息
4、加上Controller及Service
@RestController
@AllArgsConstructor
public class ProcessorController { private final ProcessorService processorService; @GetMapping("/testProcessor/{message}")
public boolean testProcessor(@PathVariable("message") String message) {
return processorService.send(message);
}
}
@Service
@AllArgsConstructor
public class ProcessorService { private final Processor processor; public boolean send(String message) {
return processor.output().send(MessageBuilder.withPayload(message).build());
} public boolean subscribe(MessageHandler handler) {
return processor.input().subscribe(handler);
}
}
5、在任意bean下写上接收逻辑或另起一个工程(另一个工程的mq需要配成一个哦)
@StreamListener(Sink.INPUT)
public void receive(String message) {
System.err.println("receive message: " + message);
}
然后我们启动项目,访问http://localhost:7070/testProcessor/hello,此时就会在控制台看到receive message: hello的字样。
消息分流(kafka特性):
@GetMapping("/testMessageShunt/{type}")
public boolean testMessageShunt(@PathVariable("type") String type) {
String header = "a".equalsIgnoreCase(type) ? "msg1" : "msg2";
return processorService.send(type, header);
}
/**
* RabbitMQ不支持消息分流
*/
@StreamListener(value = Sink.INPUT, condition = "headers['contentType']=='mgs1'")
public void receiveMessage1(@Payload Message<String> message) {
System.err.println("receive message1: " + message.getPayload());
} /**
* RabbitMQ不支持消息分流
*/
@StreamListener(value = Sink.INPUT, condition = "headers['contentType']=='mgs2'")
public void receiveMessage2(@Payload Message<String> message) {
System.err.println("receive message2: " + message.getPayload());
}
SpringCloud学习笔记(九、SpringCloud Stream)的更多相关文章
- SpringCloud学习笔记:SpringCloud简介(1)
1. 微服务 微服务具有的特点: ◊ 按照业务划分服务 ◊ 每个微服务都有独立的基础组件,如:数据库.缓存等,且运行在独立的进程中: ◊ 微服务之间的通讯通过HTTP协议或者消息组件,具有容错能力: ...
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...
- SpringCloud学习笔记(2):使用Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...
- SpringCloud学习笔记(3):使用Feign实现声明式服务调用
简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...
- SpringCloud学习笔记(4):Hystrix容错机制
简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...
- SpringCloud学习笔记(6):使用Zuul构建服务网关
简介 Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强.服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可 ...
- SpringCloud学习笔记(7):使用Spring Cloud Config配置中心
简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...
- SpringCloud学习笔记:服务支撑组件
SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...
- 多线程学习笔记九之ThreadLocal
目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...
- MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九
<Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次 SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...
随机推荐
- tornado跨域解决方法
代码 class BaseHandler(tornado.web.RequestHandler): # 允许跨域访问的地址 def allowMyOrigin(self): allow_list = ...
- appium---App页面滑动
我们操作app的过程中都会进行页面滑动,那么这个过程通过python怎么实现呢? 如何滑动 大家都非常的清楚我们手动在app上是如何滑动的,然后自动化只是模仿了手工的方法去实现,我们通过一个图来分析 ...
- Navicat Premium 破解方法
最新Navicat Premium12 破解方法,亲测可用 1.下载Navicat Premium 官网https://www.navicat.com.cn/下载最新版本下载安装(文末,网盘地址有64 ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- vue中is的使用
:is作用 1.动态切换不同组件 <div id="app"> <button @click="changeComponent('component1' ...
- 第四章 返回结果的HTTP状态码
第四章 返回结果的HTTP状态码 HTTP状态码负责表示客户端HTTP请求的返回结果.标记服务端的处理是否正常.通知出现的错误等. 1.状态码的类别 2. 2XX成功 200 OK 表示服务端已正常 ...
- Linux设备树文件结构与解析深度分析
Copy from :https://blog.csdn.net/woyimibayi/article/details/77574736 正文开始 1. Device Tree简介 设备树就是描述单板 ...
- iptraf: command not found
在Linux上安装iptraf,然后执行命令时报错,iptraf: command not found 解决办法:iptraf-ng包的二进制文件是iptraf-ng.使用命令iptraf-ng即可 ...
- MySQL基础之Natural Join用法
Natural join即自然连接,natural join等同于inner join或inner using,其作用是将两个表中具有相同名称的列进行匹配 用https://www.w3resourc ...
- 10-Django中间件
中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入和输出. 中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Djang ...