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的时候,出现消息重复消费的问题.通过沟通与排查下来主要还是用户对消费组的认识不够.其实,在之前的博文 ...
随机推荐
- React-native 关于 android真机 出现连不上服务器
我们都知道使用RN开发移动端应用时,我们要在手机端运行程序,可以下载 expo 这个软件进行扫描二维码连接到开发的APP上 有时会有突然连不上之前连上过的应用,出现如下画面 首先保证你的电脑和你的手机 ...
- Mybatis基于注解实现多表查询
对应的四种数据库表关系中存在四种关系:一对多,多对应,一对一,多对多.在前文中已经实现了xml配置方式实现表关系的查询,本文记录一下Mybatis怎么通过注解实现多表的查询,算是一个知识的补充. 同样 ...
- 从0到1体验Jenkins+Docker+Git+Registry实现CI自动化发布
一.前言 Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建.测试和部署软件.Jenkins 支持各种运行方式,可通过系统包.Docker 或者通过一个独立的 Java ...
- git@github.com: Permission denied (publickey)
1. 检查SSH key是否已经存在,如存在走第3步 : ls ~/.ssh/ 2. 如果第1步中的SSH key不存在,生成一个新的SSH key: ssh-keygen - ...
- 2019nc#4
题号 标题 已通过代码 题解 通过率 团队的状态 A meeting 点击查看 树直径 604/2055 B xor 点击查看 线段树维护线性基交 81/861 未通过 C sequence 点击 ...
- ZOJ-3964 Yet Another Game of Stones
Yet Another Game of Stones 题意: Alice 和 Bob 在进行取石子游戏, 现在一共有n堆石子, 每堆石头有ai个, 然后每堆石头有一个bi属性, 如果bi == 0, ...
- Mysql使用SSL连接
最近项目中用到了SSL连接,记录一下,环境为windows10,Mysql版本为5.6 查看是否支持 SSL 首先在 MySQL 上执行如下命令, 查询是否 MySQL 支持 SSL: mysql&g ...
- PB级数据实现秒级查询ES的安装
什么是ES?ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java语言开发的, ...
- Java抽象类构造方法
java中抽象类的子类的构造方法会隐含父类的无参构造方法. package com.zempty.abstractclass; public class AbstractDemo01 { public ...
- Python集训营45天—Day04 (函数)
目录 1. 函数介绍 2. 函数的参数 3. 模块与函数 4. 递归函数 5. 匿名函数 6. 多返回值 python 的学习已经进入到第四天,前面几章我们已经学会了基本的变量操作,以及分支结构和循环 ...