`

SpringCloud分布式微服务b2b2c电子商务(十二)在springboot中用redis实现消息队列

阅读更多

准备阶段

安装redis,可参考我的另一篇文章

java 1.8

maven 3.0

idea

环境依赖

创建一个新的springboot工程,了解springcloud架构可以加求求:三五三六二四七二五九。在其pom文件,加入spring-boot-starter-data-redis依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 创建一个消息接收者

REcevier类,它是一个普通的类,需要注入到springboot中。

public class Receiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String message) {
        LOGGER.info("Received <" + message + ">");
        latch.countDown();
    }
}

 注入消息接收者

@Bean
    Receiver receiver(CountDownLatch latch) {
        return new Receiver(latch);
    }

    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

 注入消息监听容器

在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:

一个连接工厂
一个消息监听容器
Redis template
上述1、3步已经完成,所以只需注入消息监听容器即可:

@Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

 

 测试

在springboot入口的main方法:

public static void main(String[] args) throws Exception{
        ApplicationContext ctx =  SpringApplication.run(SpringbootRedisApplication.class, args);

        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
        CountDownLatch latch = ctx.getBean(CountDownLatch.class);

        LOGGER.info("Sending message...");
        template.convertAndSend("chat", "Hello from Redis!");

        latch.await();

        System.exit(0);
    }

 

分享到:
评论

相关推荐

    微服务SpringBoot整合Redis基于Redis的Stream消息队列实现异步秒杀下单

    【微服务SpringBoot整合Redis基于Redis的Stream消息队列实现异步秒杀下单】这篇文章主要讲解了如何在微服务架构中使用SpringBoot整合Redis来构建一个基于Redis Stream的消息队列,以此来实现实时、高效的异步秒杀...

    SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码

    SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务项目Common通用依赖模块抽离示例代码 SpringCloud分布式微服务...

    SpringBoot利用redis集成消息队列的方法

    SpringBoot集成Redis实现消息队列的方法 Spring Boot框架提供了对Redis的支持,使得开发者可以轻松地将Redis集成到应用程序中,实现消息队列的功能。本文将详细介绍如何使用Spring Boot框架将Redis集成到应用程序中...

    SpringBoot中利用Redis实现消息队列,代码亲测可用, 可以传输字符串,或java对象都可以

    SpringBoot中利用Redis实现消息队列,代码亲测可用, 可以传输字符串,或java对象都可以

    springboot整合redis代码

    springboot整合redis.算是比较全面的一种整合方式了. springboot整合redis.算是比较全面的一种整合方式了.

    SpringBoot + Redis实现事件的发布订阅功能

    通过这种方式,SpringBoot结合Redis的发布订阅功能,可以在分布式系统中有效地传递消息,提高系统的响应速度和扩展性。同时,由于解耦了发布者和订阅者,系统更加灵活,易于维护和扩展。 总的来说,结合SpringBoot...

    基于SpringCloud分布式微服务+微信小程序实现短视频社交app设计源码.zip

    《基于SpringCloud分布式微服务+微信小程序实现短视频社交App设计源码详解》 在现代互联网技术的推动下,短视频社交应用已经成为人们日常生活中不可或缺的一部分。本文将深入探讨如何利用SpringCloud分布式微服务...

    springboot基于redis分布式锁

    本教程将深入探讨如何在SpringBoot应用中实现基于Redis的分布式锁。 首先,Redis之所以常被用作分布式锁的实现,是因为其具有以下优点: 1. **高可用性**:Redis支持主从复制,可以确保在单点故障时仍有服务可用。...

    Qt 应用Redis 实现消息队列

    在IT领域,消息队列是一种常见的中间件技术,用于在分布式系统中解耦组件,提高系统的可扩展性和响应速度。本篇文章将详细讲解如何在Qt应用程序中利用Redis来实现一个高效的消息队列,以实现点对点的生产者-消费者...

    springboot_redis

    6. **消息队列支持**:Redis也可以作为消息中间件,Spring Boot提供了`RedisMessageListenerContainer`和`SimpleMessageConverter`等组件来实现发布/订阅模式。 7. **Sentinel或Cluster支持**:如果需要高可用性,...

    spring-redis-mq, 基于 Spring 和 Redis 的分布式消息队列(MessageQueue)实现.zip

    本项目"spring-redis-mq"正是结合了这两者,构建了一个基于Spring和Redis的分布式消息队列实现。 首先,让我们深入理解Spring框架在分布式消息队列中的作用。Spring框架提供了一套完整的声明式事务管理、依赖注入、...

    springboot+redis实现查询附近商铺功能.zip

    在本项目"springboot+redis实现查询附近商铺功能.zip"中,主要展示了如何利用Spring Boot框架结合Redis缓存系统来实现在web应用中查询附近的商业店铺。以下将详细阐述涉及的关键知识点: 1. **Spring Boot**: ...

    毕业设计,基于SpringCloud分布式微服务+微信小程序开发的短视频社交App,内含Java完整源代码,数据库脚本

    基于SpringCloud分布式微服务+微信小程序实现短视频社交app设计 开发软件: Idea + 微信web开发者工具 + mysql5.6 + redis 1 微信小程序端 (1)用户信息模块:包含:注册、登陆、发布短视频、编辑短视频,其中编辑...

    mqtt+springBoot+redis消息处理,亲测整理上线

    在本文中,我们将深入探讨如何使用`MQTT`(Message Queuing Telemetry Transport)协议与`SpringBoot`框架集成,并利用`Redis`作为缓存来处理消息。`MQTT`是一种轻量级的发布/订阅消息协议,常用于物联网(IoT)设备...

    springboot整合redis集群零配置

    Redis则是一款高性能的键值存储系统,常用于数据缓存、消息队列等场景。当Spring Boot与Redis结合时,可以实现高效的数据处理和应用性能提升。本篇文章将深入探讨如何在Spring Boot中实现对Redis集群的零配置整合,...

    基于SpringBoot+Redis实现的仿QQ伪桌面聊天系统.zip

    这个毕业设计项目旨在模仿QQ的桌面聊天功能,通过使用SpringBoot作为后端开发框架,Redis作为缓存和消息队列,实现了一个基本的即时通讯系统。 首先,SpringBoot是Spring框架的一个轻量级版本,它简化了创建独立的...

    springboot整合Redis哨兵,实现消息队列场景

    springboot整合Redis哨兵,实现消息队列场景

    springboot+redis+token保持登录

    在现代Web应用开发中,保持用户登录状态是一个常见的需求,"springboot+redis+token保持登录"的主题就涉及到了如何利用Spring Boot、Redis以及Token技术来实现这一功能。本文将详细探讨这一技术栈的实现原理和步骤。...

    SpringBoot集成Redis

    SpringBoot集成Redis是一个常见的开发需求,它使得应用可以利用Redis的高性能、内存存储特性进行数据缓存和快速...在实际项目中,还可以结合Spring Session等框架,利用Redis实现分布式会话管理,进一步提升系统性能。

Global site tag (gtag.js) - Google Analytics