Spring Cloud Stream 整合 RabbitMQ
简介
Spring Cloud Stream是一个构建消息驱动微服务的框架,应用程序通过input(相当于consumer)、output(相当于producer)来与Spring Cloud Stream中Binder交互,而Binder负责与消息中间件交互;因此,我们只需关注如何与Binder交互即可,而无需关注与具体消息中间件的交互。

使用
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>2.1..RELEASE</version>
</dependency>
2、配置
provider配置(采用动态路由键方式)
server:
port:
spring:
cloud:
stream:
binders:
pro:
type: rabbit
environment:
spring:
rabbitmq:
addresses: localhost
port:
username: test
password: test
virtual-host: test
bindings:
myOutPut:
destination: myOutPut
content-type: application/json
default-binder: test
rabbit:
bindings:
myOutPut:
producer:
exchangeType: topic
routing-key-expression: headers.routeId
consumer配置
server:
port:
spring:
cloud:
stream:
rabbit:
bindings:
input:
consumer:
bindingRoutingKey: routeKey1
acknowledge-mode: manual
binders:
protest:
type: rabbit
environment:
spring:
rabbitmq:
addresses: localhost
port:
username: test
password: test
virtual-host: test
bindings:
input:
destination: myOutPut
content-type: application/json
default-binder: protest
group: group-cus1
3、java端
provider
public interface MqMessageSource {//自定义通道
String MY_OUT_PUT = "myOutPut";
@Output(MY_OUT_PUT)
MessageChannel testOutPut();
}
@EnableBinding(MqMessageSource.class)
public class MessageProviderImpl implements IMessageProvider {
@Autowired
@Output(MqMessageSource.MY_OUT_PUT)
private MessageChannel channel; @Override
public void send(Company company) {
channel.send(MessageBuilder.withPayload(company).setHeader("routeId", company.getTitle()).build());
}
}
consumer
@Component
@EnableBinding(Sink.class)
public class MessageListener {
@StreamListener(Sink.INPUT)
public void input(Message<Company> message) throws IOException {
Channel channel = (com.rabbitmq.client.Channel)message.getHeaders().get(AmqpHeaders.CHANNEL);
Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
channel.basicAck(deliveryTag, false);
System.err.println(JSON.toJSONString(message.getPayload()));
}
}
END
Spring Cloud Stream 整合 RabbitMQ的更多相关文章
- Spring Cloud Stream整合RabbitMQ
简介 Spring Cloud Stream是一个构建消息驱动微服务的框架,应用程序通过input(相当于consumer).output(相当于producer)来与Spring Cloud Str ...
- Spring Cloud Alibaba - Spring Cloud Stream 整合 RocketMQ
Spring Cloud Stream 简介 在微服务的开发过程中,可能会经常用到消息中间件,通过消息中间件在服务与服务之间传递消息,不管你使用的是哪款消息中间件,比如RabbitMQ.Kafka和R ...
- spring cloud stream整合
spring cloud stream整体架构核心概念图: 图一:消息的发送端和接收端可以是不同的中间件 图二: 图三:在消息的发送之前和消息的接收端套了一层管道 @Output:输出注释,用于定义发 ...
- spring cloud stream集成rabbitmq
pom添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...
- RabbitMQ与Spring的框架整合之Spring Cloud Stream实战
1.RabbitMQ与Spring Cloud Stream整合实战.SpringCloud Stream整体结构核心概念图,如下所示: 图示解释:Outputs输出,即消息的发送端.Inputs输入 ...
- 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...
- 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...
- Spring Cloud Stream 使用延迟消息实现定时任务(RabbitMQ)
应用场景 通常在应用开发中我们会碰到定时任务的需求,比如未付款订单,超过一定时间后,系统自动取消订单并释放占有物品. 许多同学的第一反应就是通过spring的schedule定时任务轮询数据库来实现, ...
- Spring Cloud Stream如何处理消息重复消费?
最近收到好几个类似的问题:使用Spring Cloud Stream操作RabbitMQ或Kafka的时候,出现消息重复消费的问题.通过沟通与排查下来主要还是用户对消费组的认识不够.其实,在之前的博文 ...
随机推荐
- AVL自平衡二叉树
详细的具体步骤 : 一篇讲的很好博客 AVL,红黑树优先博客-Never 先对二叉树的不平衡结构进行总结: 各种旋转 特别注意字母含义(结构)和其旋转操作之间的区别 二叉树不平衡结构 性质 平衡操作 ...
- [2018CCPC吉林赛区(重现赛)- 感谢北华大学] 补题记录 躁起来
1007 High Priestess 埃及分数 1008 Lovers 线段树维护取膜意义下的区间s和. 每个区间保存前缀lazy和后缀lazy. #include <iostream> ...
- A-Graph Games_2019牛客暑期多校训练营(第三场)
题意 给出一张无向图,定义S[x]表示与点x直接相连的点集,有两个操作 1 x y表示将第x到第y条边状态变化(若存在则删除,不存在则建立) 2 x y询问S[x]与S[y]是否相等 题解 有一个技巧 ...
- CodeForces 809B Glad to see you!
Glad to see you! 题解: 交互题一般都是需要用二分去完成. 在二分时候每次检查左边和右边哪边会和答案更近, 然后在更近的那段新区间去重复检查, 知道区间长度为1. 在检查的时候,可以使 ...
- PythonI/O进阶学习笔记_3.2面向对象编程_python的封装
前言: 本篇相关内容分为3篇多态.继承.封装,这篇为第三篇 封装. 本篇内容围绕 python基础教程这段: 在面向对象编程中,术语对象大致意味着一系列数据(属性)以及一套访问和操作这些数据的方法.使 ...
- 当React开发者初次走进React-Native的世界
RN千机变 1.技术体系问题 RN和React共用一套抽象层,相对于前端,RN其实更接近Node的运行环境 ReactNative =React +IOS +Android 看RN文档时,我会发现入门 ...
- Go语言基础之单元测试
不写测试的开发不是好程序员.我个人非常崇尚TDD(Test Driven Development)的,然而可惜的是国内的程序员都不太关注测试这一部分. 这篇文章主要介绍下在Go语言中如何做单元测试和基 ...
- 055 Python第三方库安装
目录 一.概述 二.看见更大的Python世界 2.1 Python社区 2.1.1 PyPI 2.1.2 实例:开发与区块链相关的程序 2.2 安装Python第三方库 三.第三方库的pip安装方法 ...
- 使用 Docker 安装 showdoc
一.简介 ShowDoc 是一个非常适合IT团队在线共享文档的工具,在线访问地址为:https://www.showDoc.cc 本来也可以直接 pull showdoc 镜像到本地,使用 docke ...
- charles 开始/暂停模拟慢网速
本文参考:charles 开始/暂停模拟慢网速 1.2. stop/start throttling 和 2.2 throttling settings 暂时开始慢网速,这个说实话,我自己是不常用的, ...