简介


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的更多相关文章

  1. Spring Cloud Stream整合RabbitMQ

    简介 Spring Cloud Stream是一个构建消息驱动微服务的框架,应用程序通过input(相当于consumer).output(相当于producer)来与Spring Cloud Str ...

  2. Spring Cloud Alibaba - Spring Cloud Stream 整合 RocketMQ

    Spring Cloud Stream 简介 在微服务的开发过程中,可能会经常用到消息中间件,通过消息中间件在服务与服务之间传递消息,不管你使用的是哪款消息中间件,比如RabbitMQ.Kafka和R ...

  3. spring cloud stream整合

    spring cloud stream整体架构核心概念图: 图一:消息的发送端和接收端可以是不同的中间件 图二: 图三:在消息的发送之前和消息的接收端套了一层管道 @Output:输出注释,用于定义发 ...

  4. spring cloud stream集成rabbitmq

    pom添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  5. RabbitMQ与Spring的框架整合之Spring Cloud Stream实战

    1.RabbitMQ与Spring Cloud Stream整合实战.SpringCloud Stream整体结构核心概念图,如下所示: 图示解释:Outputs输出,即消息的发送端.Inputs输入 ...

  6. 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...

  7. 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...

  8. Spring Cloud Stream 使用延迟消息实现定时任务(RabbitMQ)

    应用场景 通常在应用开发中我们会碰到定时任务的需求,比如未付款订单,超过一定时间后,系统自动取消订单并释放占有物品. 许多同学的第一反应就是通过spring的schedule定时任务轮询数据库来实现, ...

  9. Spring Cloud Stream如何处理消息重复消费?

    最近收到好几个类似的问题:使用Spring Cloud Stream操作RabbitMQ或Kafka的时候,出现消息重复消费的问题.通过沟通与排查下来主要还是用户对消费组的认识不够.其实,在之前的博文 ...

随机推荐

  1. NLP(二十四)使用LSTM构建生成式聊天机器人

    准备 数据集:AIML数据集 下载数据集并用Notepad++打开,复制到txt文件中方便打开 代码实现 数据很少,训练轮次不多,结果不好,仅当示例 import numpy as np import ...

  2. POJ-1062 昂贵的聘礼 (最短路)

    POJ-1062 昂贵的聘礼:http://poj.org/problem?id=1062 题意: 有一个人要到1号点花费最少的钱,他可以花费一号点对应的价格,也可以先买下其他一些点,使得费用降低. ...

  3. HDU 6634 网络流最小割模型 启发式合并

    如果我们先手拿完所有苹果再去考虑花费的话. S -> 摄像头 -> 苹果 -> T 就相当于找到一个最小割使得S和T分开. ans = sum - flow. 然后对于这一个模型, ...

  4. CodeForces 592D Super M DP

    Super M 题解: 定义 dp[u][0] 为遍历完u中的所有节点, 但不回到u点的路径花费值. 定义 dp[u][1] 为遍历完u中的所有节点, 且要回到u点的路径花费值. 转移方程. dp[u ...

  5. ACM-ICPC 2017 Asia Urumqi:A. Coins(DP) 组合数学

    Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the head ...

  6. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  7. uiautomator2 实现App九宫格解锁

    App九宫格解锁 之前在testerhome社区看见codeskyblue大佬写过一种方法,但是这种办法存在一个弊端,那就是多个点的坐标是写死的,也就是说要是换了部手机,九宫格解锁就行不通了,于是就想 ...

  8. Flask源码浅析

    前言 学习一样东西,要先知其然,然后知其所以然. 这次,我们看看Flask Web框架的源码.我会以Flask 0.1的源码为例,把重点放在Flask如何处理请求上,看一看从一个请求到来到返回响应都经 ...

  9. tomcat,nginx日志定时清理

    1. Crontab定时任务 Crontab 基本语法 t1 t2 t3 t4 t5 program 其中 t1 是表示分钟,t2 表示小时,t3 表示一个月份中的第几日,t4 表示月份,t5 表示一 ...

  10. python 中的while循环、格式化、编码初始

    while循环 循环:不断重复着某件事就是循环 while 关键字 死循环:while True: ​ 循环体 while True: # 死循环# print("坚强")# pr ...