Spring Cloud Stream RabbitMQ普通实现+延时消息+死信队列

配置和代码示例

参考文档
Spring Cloud Stream RabbitMQ Binder Reference Guide
依赖

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-stream-rabbitartifactId>
dependency>

配置

spring:
	cloud:
	    stream:
	      #RabbitMQ服务器地址配置
	      binders:
	        windrabbit:
	          type: rabbit
	          environment:
	            spring:
	              rabbitmq:
	                addresses: localhost
	                port: 15672
	                username: guest
	                password: guest
	      bindings:
	        order-output:
	          destination: order.exchange
	          binder: windrabbit
	          content-type: application/json
	          group: order
	        order-input:
	          destination: order.exchange
	          binder: windrabbit
	          content-type: application/json
	          group: order
	        delay-output:
	          destination: delay.exchange
	          binder: windrabbit
	          content-type: application/json
	          group: delay-order
	          producer:
	            requiredGroups: delay-order
	        delay-input:
	          destination: delay.exchange
	          binder: windrabbit
	          content-type: application/json
	          group: delay-order
	      #这一部分是给上边声明的bindings添加配置的,例如队列的ttl,还有要不要给队列配置死信队列
	      rabbit:
	        bindings:
	          delay-output:
	            producer:
	              ttl: 120000#队列里的消息如果120000ms之后还没被消费,就会成为死信,这个参数生效的前提是spring.cloud.stream.bindings里边声明了requiredGroups
	              autoBindDlq: true#这个参数为true的时候会自动为当前的队列创建一个死信队列,以dlq结尾

新建一个Processor来映射上边配置的bindings

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.stereotype.Component;

@Component
public interface MQOrderProcessor {
    String INPUT="order-input";
    @Input(MQOrderProcessor.INPUT)
    SubscribableChannel orderInput();
//    String DELAYINPUT="delay-input";
//    @Input(MQOrderProcessor.DELAYINPUT)
//    SubscribableChannel delayOrderInput();
    String OUTPUT="order-output";
    @Output(MQOrderProcessor.OUTPUT)
    MessageChannel orderOutput();
    String DELAYOUTPUT="delay-output";
    @Output(MQOrderProcessor.DELAYOUTPUT)
    MessageChannel delayOrderOutput();
}

发送消息的代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import com.wind.valley.mq.processor.MQOrderProcessor;
import org.springframework.messaging.support.MessageBuilder;

@EnableBinding(MQOrderProcessor.class)
public class OrderMessageProvider  {
    @Autowired
    private MQOrderProcessor processor;
    public boolean sendCancelOrderMessage(){
        System.out.println("start send message to mq");
        return processor.delayOrderOutput().send(MessageBuilder.withPayload("{message:'cancel order'}").build());
    }
}

监听消息的代码

import com.alibaba.fastjson.JSON;
import com.wind.valley.mq.processor.MQOrderProcessor;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

@EnableBinding(MQOrderProcessor.class)
public class OrderMessageConsumer {
    @StreamListener(MQOrderProcessor.INPUT)
    public void process(Object obj) {
        System.out.println("get message"+JSON.toJSONString(obj));
    }
//    @StreamListener(MQOrderProcessor.DELAYINPUT)
//    public void delayProcess(Object obj) {
//        System.out.println("get delay message"+JSON.toJSONString(obj));
//    }
}

普通队列的实现步骤

生产者

  1. 引入依赖
  2. 配置rabbitmq服务端spring.cloud.stream.binders
  3. 配置bindings spring.cloud.stream.bindings.xx-output
  4. 为bindings配置Processor(接口)
public interface MQOrderProcessor {
//对应bindings的名称
	String OUTPUT="order-output";
	@Output(MQOrderProcessor.OUTPUT)
    MessageChannel orderOutput();
}
  1. 发送消息的示例代码
@EnableBinding(MQOrderProcessor.class)
public class OrderMessageProvider  {
    @Autowired
    private MQOrderProcessor processor;
    public boolean sendCancelOrderMessage(){
        System.out.println("start send message to mq");
        return processor.delayOrderOutput().send(MessageBuilder.withPayload("{message:'cancel order'}").build());
    }
}

消费者

  1. 引入依赖
  2. 配置rabbitmq服务端spring.cloud.stream.binders
  3. 配置bindings spring.cloud.stream.bindings.xx-input(destination指向生产者destination)
  4. 为bindings配置Processor(接口)
public interface MQOrderProcessor {
    String INPUT="order-input";
    @Input(MQOrderProcessor.INPUT)
    SubscribableChannel orderInput();
}
  1. 监听Processor的input,这一步是添加具体实现,如果不做这一步,消息就会被Processor消费掉
@EnableBinding(MQOrderProcessor.class)
public class OrderMessageConsumer {
    @StreamListener(MQOrderProcessor.INPUT)
    public void process(Object obj) {
        System.out.println("get message"+JSON.toJSONString(obj));
    }
//    @StreamListener(MQOrderProcessor.DELAYINPUT)
//    public void delayProcess(Object obj) {
//        System.out.println("get delay message"+JSON.toJSONString(obj));
//    }
}

延迟队列与死信队列实现30分钟未支付,取消订单

  1. 在普通队列的基础上,配置延迟队列的关键属性spring.cloud.stream.rabbit.bindings.xx-output.producer.ttl,spring.cloud.stream.bindings.xx-output.producer.requiredGroups
  2. 为延迟队列添加属性spring.cloud.stream.rabbit.bindings.xx-output.producer.autoBindDlq=true,就会自动创建一个死信队列,延迟队列里的超时未消费的消息就会移动到这个死信队列里,自动添加的死信队列的名称为延迟队咧的destination+.dlq
  3. 写一个消费者消费死信队列里的消息就好了
    Spring Cloud Stream RabbitMQ普通实现+延时消息+死信队列_第1张图片

你可能感兴趣的:(Spring,Cloud)