java之springboot集成redis实现消息队列(代码片段)

蓝盒子itbluebox 蓝盒子itbluebox     2022-11-29     620

关键词:

Java之SpringBoot集成redis实现消息队列

一、设置好Redis的配置文件(application.yml)

spring:
  # redis 配置
  redis:
    # 地址
    host: localhost
    # 端口,默认为6379
    port: 6379
    # 数据库索引
    database: 2
    # 密码
    #password: root
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms

二、消息接收者实体类(RedisMessage )

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息接收者实体类
 * @author
 * @date itbluebox
 */
@Slf4j
@Component
public class RedisMessage implements MessageListener 
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 测试代码
     */
    @Override
    public void onMessage(Message message, byte[] pattern) 
        RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
        String msg = serializer.deserialize(message.getBody());
        System.out.println("接收到的消息是1:" + msg);
    



三、消息队列 订阅者(RedisSubConfig)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 消息队列  订阅者  redis配置
 * @author
 * @date itbluebox
 */
@Configuration
public class RedisSubConfig 
    /**
     * 创建连接工厂
     * @param connectionFactory
     * @param adapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(
            RedisConnectionFactory connectionFactory, MessageListenerAdapter adapter) 
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(adapter, new PatternTopic("userInfo"));
        container.addMessageListener(adapter, new PatternTopic("org"));
        return container;
    
    /**
     * @param message
     * @return
     */
    @Bean
    public MessageListenerAdapter adapter(RedisMessage message)
        // onMessage 如果RedisMessage 中 没有实现接口,这个参数必须跟RedisMessage中的读取信息的方法名称一样
        return new MessageListenerAdapter(message, "onMessage");
    

四、发送消息(消息生产者)


@RestController
public class TestController 
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage()
        stringRedisTemplate.convertAndSend("userInfo", "Hello world-userInfo");
        stringRedisTemplate.convertAndSend("org", "Hello world-org");
        return "ok";
    

java之springboot集成redis实现消息队列(代码片段)

Java之SpringBoot集成redis实现消息队列一、设置好Redis的配置文件(application.yml)二、消息接收者实体类(RedisMessage)三、消息队列订阅者(RedisSubConfig)四、发送消息(消息生产者)一、设置好Redis的... 查看详情

springboot之集成redis:springcache+redis

前言来啦老铁!笔者学习SpringBoot有一段时间了,附上SpringBoot系列学习文章,欢迎取阅、赐教:5分钟入手SpringBoot;SpringBoot数据库交互之SpringDataJPA;SpringBoot数据库交互之Mybatis;SpringBoot视图技术;SpringBoot之整合Swagger;SpringBoot之junit单... 查看详情

springboot集成redisson操作redis

参考技术A每个Redisson对象实例都会有一个与之对应的Redis数据实例,可以通过调用getName方法来取得redis数据实例的名称(key),所有于Rediskey相关的操作都归纳在RKeys这个接口里。具体demo其中,getKeysByPattern是基于redis的scan命令实现... 查看详情

springboot2.0实战(23)整合redis之集成缓存springdatacache

SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情

重学springboot系列之集群多节点应用session共享,redis分布式锁(代码片段)

重学springboot系列之集群多节点应用session共享,redis分布式锁springsession共享的实现原理集成Springsession引入spring-session-redis的maven依赖配置启用`Redis`的`httpSession`配置redis链接信息(application.yml)测试一个项目多个端口... 查看详情

springboot入门:集成redis哨兵模式,实现mybatis二级缓存

本片文章续《SpringBoot入门(九):集成Quartz定时任务》。本文主要基于redis实现了mybatis二级缓存。较redis缓存,mybaits自带缓存存在缺点(自行谷歌)。本文是基于docker安装redis主从模式。1.redis安装(1)首先安装redis集群模式,... 查看详情

5.springcloud微服务架构搭建之《springboot集成hystrix》(代码片段)

1.springcloud微服务架构搭建之《springboot自动装配Redis》2.springcloud微服务架构搭建之《springboot集成nacos注册中心》3.springcloud微服务架构搭建之《springboot自动装配ribbon》4.springcloud微服务架构搭建之《springboot集成openFeign》目录1.项目... 查看详情

shiro整合springboot缓存之redis实现(代码片段)

Shiro整合Springboot缓存之Redis实现Redis下载安装引入redis依赖配置redis连接启动redis服务自定义redis缓存的实现自定义shiro缓存管理器自定义salt实现实现序列化接口自定义Realm改造开启redis缓存Redis下载安装Windows系统中Redis下载安装其它... 查看详情

springboot集成redis实现缓存(代码片段)

前言在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。一键获取源码地址准备工作当前项目工具及环境开发工具... 查看详情

springboot集成redis实现缓存(代码片段)

前言在此章,我们将SpringBoot集成Redis缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库、缓存和消息代理,在本章仅讲解缓存集成。一键获取源码地址准备工作当前项目工具及环境开发工具... 查看详情

springboot集成redis并实现主从架构(代码片段)

...会写bug的程序猿!今天这篇文章来和大家分享一下在springboot中如何集成redis,并实现主从架构,进行数据的简单存储。我的Redis是部署在Windows系统下面的,所以在这里附上Redis在Windows环境下的安装地址和安装说明... 查看详情

springboot怎么集成redis

...illattempttoconnecttoaRedisserverusinglocalhost:6379:参考技术A完整的springboot集成redis方案介绍:SpringBoot整合Redis 查看详情

springboot2.x集成单节点redis

Springboot2.x集成单节点Redis说明在Springboot1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot2.x版本中,默认使用Lettuce客户端来操作Redis。Springboot提供了RedisTemplate来统一封装了对Redis操作,开发者只需要使用RedisTemplate就可以... 查看详情

springboot集成redis来实现缓存技术方案

概述在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求。 Redis简介Redis是... 查看详情

springboot2.x集成redis哨兵模式

Springboot2.x集成Redis哨兵模式说明Redis哨兵模式是Redis高可用方案的一种实现方式,通过哨兵来自动实现故障转移,从而保证高可用。准备条件pom.xml中引入相关jar<!--集成Redis--><dependency><groupId>org.springframework.boot</group... 查看详情

redis7之springboot集成redis(代码片段)

十一SpringBoot集成Redis1.配置文件redis.conf配置文件,改完后确保生效,记得重启,记得重启默认daemonizeno改为daemonizeyes默认protected-modeyes改为protected-modeno默认bind127.0.0.1改为直接注释掉(默认bind127.0.0.1只能本机访问)或改成... 查看详情

springboot之集成邮件服务.

...现JavaMailSenderImpl,它会使用JavaMailAPI来发送Email。Spring或SpringBoot 查看详情

springboot系列之mongotemplate加pagehelper分页实现(代码片段)

SpringBoot系列之MongoDB分页接口实现spring-boot-starter-data-mongodb也有集成基于SpringData的分页实现,但是习惯了用PageHelper,所以基于PageHelper集成一下mongodb,下面给出实现代码例子环境准备开发环境JDK1.8SpringBoot2.2.1Maven3.2+开发工具Intelli... 查看详情