简介

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>.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()));
    }
}

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. Unable to find a single main class from the following candidates

    关于start-class,spring boot官方手册是这么说明的: The plugin rewrites your manifest, and in particular it manages ...

  2. QWidget 之paint部分杂记(从Qt4.0到4.8的进化,在Qt 4.4中,Alien Widget诞生了)

    Qt 4.0 automatically double-buffers Qt 4.1 QWidget::autoFillBackground Qt 4.2 delayed widget creatio ...

  3. CP_ACP : CP_OEMCP

    // filesystem windows_file_codecvt.cpp -----------------------------------------// // Copyright Bema ...

  4. Qt、Qte与Qtopia(Qt嵌入式的发展历程)

    Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序也必须是GPL的.不过自从qt ...

  5. 1 WCF 一个基础理论 以及如何实现一个简单wcf服务

    1 SOA : service oriented architecture 面向服务的架构 2 web service标准 3 概念理解图 4 WCF类库 项目的 wcf简单实现 首先创建一个简单的w ...

  6. HPC —— 高性能计算

    CUDA,目前只有 NVIDIA 支持: OpenCL,CUDA Tesla 卡很贵: 1. 术语及概念 SMP:"对称多处理"(Symmetrical Multi-Process ...

  7. Editplus配置Python的开发环境

    Python 有很多集成开发工具,商业的有komodo,Wingide,Boa 等,还有Python 自带的集成环境IDLE,Windows 下还有PythonWin 等.但是,这些工具有的是过于复杂 ...

  8. python 教程 第九章、 类与面向对象

    第九章. 类与面向对象 1)    类 基本类/超类/父类被导出类或子类继承. Inheritance继承 Inheritance is based on attribute lookup in Py ...

  9. Matlab随笔之分段线性函数化为线性规划

    原文:Matlab随笔之分段线性函数化为线性规划 eg: 10x,            0<=x<=500 c(x)=1000+8x,    500<=x<=1000 300 ...

  10. Win32 键盘事件 - 击键消息、字符消息、插入符号(光标)

    注:以下内容为学习笔记,多数是从书本.资料中得来,只为加深印象,及日后参考.然而本人表达能力较差,写的不好.因非翻译.非转载,只好选原创,但多数乃摘抄,实为惭愧.但若能帮助一二访客,幸甚! 以下内容主 ...