springcloudstream整合rabbitmq使用之使用延时队列实现消息发送消费

1.添加依赖

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

2.消息发送端配置文件

  # 配置中心
  spring:
     cloud:
       stream:
         bindings:
           OUT1:  #Queue
             content-type: application/json
             destination: test-exchange  #exchange 绑定删除用于相关信息的交换机  相当天topky
         rabbit:
           bindings:
             OUT1:
               producer:
                 delayed-exchange: true #延迟队列
         binders:
           rabbitbinder:
             type: rabbit
             environment:
               spring:
                 rabbitmq:
                   host: 127.0.0.1
                   port: 5672
                   username:
                   password: 
                   virtual-host: /

3.消息发送端绑定通道

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
public interface EventSink {
    //消息通道,发送增加注册用户成长值的消息
    String OUT1 = "OUT1";
    @Output(OUT1)
    MessageChannel output3Message();
}

4.发送消息

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
@EnableBinding(EventSink.class)
@Component
@Slf4j
public class EventOutPutStream {
    @Autowired
    @Output(EventSink.OUT3)
    private MessageChannel channel;
    public boolean sendMsg(Long id){
        log.info("发送的消息为id={}",id);
        //添加成功后60秒发送消息
        boolean send = channel.send(MessageBuilder.withPayload(id).setHeader("x-delay",1000*60).build());
        log.info("{},发送的消息为id={},发送结果为send={}",EventSink.OUT3,id,send);
        return send;
    }
}

5.消费端配置

   spring:
	  cloud:
	    stream:
	      bindings:
	        OUT1:  #Queue
	          content-type: application/json
	          destination: test-exchange  #exchange 绑定删除用于相关信息的交换机  相当天topky
	      rabbit:
	        bindings:
			    EVENT_INPUT1:  #Queue
		          destination: test-exchange   #myExhange
		          group: test-queue            #myQueue 消费组
		          consumer:     #消息者
		            max-attempts: 1
		            auto-bind-dlq: true
		            dlq-ttl: 5000
	      binders:
	        rabbitbinder:
	          type: rabbit
	          environment:
	            spring:
	              rabbitmq:
	                host: 127.0.0.1
	                port: 5672
	                username:
	                password: 
	                virtual-host: /
	               rabbit:
	        bindings:
	          EVENT_INPUT1:
	            producer:
	              delayed-exchange: true #延迟队列

6.消费端绑定通道

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface EventInPutSource {
    String EVENT_INPUT1 = "EVENT_INPUT1";
    @Input(EVENT_INPUT1)
    SubscribableChannel chanel1();
}

7.消费端消费消息

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.support.AmqpMessageHeaderAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
@EnableBinding(EventInPutSource.class)
@Component
@Slf4j
public class EventInputStream {
    @Autowired
    private TbUserGrowthService tbUserGrowthService;
    @SneakyThrows
    @StreamListener(EventInPutSource.EVENT_INPUT1)
    public void handleMsg1(Long id) {
        try {
            log.info("接收到的消息为ir={}", id);
            Integer integer = tbUserGrowthService.addUserGrowth(id, MemberGrowthType.register.getCode());
            log.info("处理消息的结果为result={}", integer);
        } catch (Exception e) {
            e.printStackTrace();
             //抛出异常,由其进行死信队,在死信队中会进行重试
            throw new RuntimeException(e);
        }
    }

8.启动发送消息服务和消费消息服务就可以啦
9.补充:可以在rabbitmq客户端查看消费失败的消息,可在rabbitmq安装目录下lunix操作系统中(cd /usr/lib/rabbitmq/bin)执行 rabbitmq-plugins enable rabbitmq_shovel rabbitmq_shovel_management 命令即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值