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笔记系列--目录索 ...
随机推荐
- Linux系统学习 七、网络基础—常用网络命令
1.ifconfig #查看网络(设置IP临时生效) 2.hostname [主机名] #查看或设置主机名 默认的是localhost ...
- java8-14-时间API
原来的时间类 1.默认值 我们使用起来不方便 2.在不同包 不规范 在java.util和java.sql的包中都有日期类,此外用于格式化和解析的类在java.text包中定义 3.可变 线程 ...
- Leetcode 1239. 串联字符串的最大长度
地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/s ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- LVS负载均衡实现双向热备
一.LVS1服务器配置 安装ipvsadm,keepalived [root@localhost ~]# yum -y install ipvsadm keepalived 配置keepalivedd ...
- 《Java面试全解析》505道面试题详解
<Java面试全解析>是我在 GitChat 发布的一门电子书,全书总共有 15 万字和 505 道 Java 面试题解析,目前来说应该是最实用和最全的 Java 面试题解析了. 我本人是 ...
- golang数据结构之环形队列
目录结构: circlequeue.go package queue import ( "errors" "fmt" ) //CircleQueue 环型队列 ...
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 11
23.6 使用第三方接口服务实例 接供服务的第三方接口平台有很多,现在的项目中也经常用到一些第三方接口,如支付宝.微信.短信.邮件接口等,我们需要借助第三方的能力来实现产品的某些功能.如果自己已经掌 ...
- Linux vi文档操作
使用操作 a 在光标后插入 A 插入行末 i 在光标前插入 I 插入行首 o 向下切换一行 O 向上开一行 dd 删除一整行 x 删除光标后一个字符 X 删除光标前一个字符 shif ...
- Web前端——css
css 推荐的样式编写顺序: Positioning:定位 Box model:盒子模型.大小等 Typographic:文字系列.排印等 Visual:可视化.背景等 Misc:其它混杂模式 居中 ...