RabbitMQ和Redis在SpringBoot中的使用

本文介绍了如何在SpringBoot项目中使用RabbitMQ进行消息队列管理和Redis进行数据缓存,包括依赖导入、配置设置、生产者发送消息到队列以及消费者从队列接收并存储到Redis的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RabbitMQ和Redis在SpringBoot中的使用

  1. RabbitMQ
  • 导入依赖
<!-- rabbtiMq,使用rabbitTemplate-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
  • application.yml配置
spring:
  rabbitmq:
    port: 5672
    host: localhost
    username: username
    password: password
    #这个配置是保证提供者确保消息推送到交换机中,不管成不成功,都会回调
    publisher-confirm-type: correlated
    #保证交换机能把消息推送到队列中
    publisher-returns: true
    virtual-host: /
    #这个配置是保证消费者会消费消息,手动确认
    listener:
      simple:
        acknowledge-mode: manual
    template:
      mandatory: true
  • RabbitMqConfig配置文件
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 该类只用于Bean注入
@Configuration
public class RabbitMqConfig {
	// 读取application.yml文件中的配置
    @Value("${spring.rabbitmq.port}")
    private int port;
    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;


    @Bean
    public CachingConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory=new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }
    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }
    @Bean
    public Queue myQueue(){
        // 默认值
        return new Queue("goods");
    }
    @Bean
    public DirectExchange myExchange(){
        // 默认值
        return new DirectExchange("goods");
    }
    @Bean
    public Binding queueToExchangeBinding(Queue myQueue,DirectExchange myExchange){
        return BindingBuilder.bind(myQueue).to(myExchange).with("goods");
    }
}

  1. 实例使用

① 引入

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

② 调用方法
【发布/订阅者模式】

生产者

    //定时任务发布商品
    public void sentGoods(List<Goods> list){
        //首先查询数据
        //生产者
        for(Goods goods:list){
            //指定队列
            rabbitTemplate.convertAndSend("goods",goods);
        }
    }

消费者【使用Redis】

    @RabbitListener(queues = "goods")
    @RabbitHandler
    public void sentGoodsToRedis(Goods goods){
        String key=goods.getId().toString();
        //使用Redis实现数据缓存【此处用的hash格式放入数据】  
        redisTemplate.opsForHash().put(key,"goods",goods);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值