《RabbitMQ系列教程-第十三章-消息的幂等性》_利用redis实现rabbitmq 幂等性

application.yml:
spring:
  rabbitmq:
    host: 192.168.133.151
    port: 5672
    username: guest
    password: guest
    virtual-host: /
  redis:
    host: 127.0.0.1

启动类:
package com.lscl.rabbitmq;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public RedisTemplate redisTemplate(RedisTemplate redisTemplate){

        // value可见
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // key可见
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        return redisTemplate;
    }

    // 定义测试队列
    @Bean
    public Queue testQueue(){
        return QueueBuilder.durable("test\_queue").build();
    }
}

监听器:
package com.lscl.rabbitmq.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class TestListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = "test\_queue")
    public void test\_queue\_confirm(Message message) {

        String messageId = message.getMessageProperties().getMessageId();

        if (null == messageId) {

            System.out.println("消息id为null!");
            return;
        }

        if (redisTemplate.opsForValue().setIfAbsent(messageId, new String(message.getBody()))) {

            // 代表第一次消费消息
            System.out.println("消息消费成功: " + new String(message.getBody()));
        } else {
            System.out.println("消息已经被消费过了!");
        }
    }
}

消息发送端:
package com.lscl.rabbitmq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以点击这里获取](https://bbs.csdn.net/topics/618540462)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值