spring boot集成redis

package com.qmtt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        setSerializer(template);// 设置序列化工具
        template.afterPropertiesSet();
        return template;
    }

    private void setSerializer(StringRedisTemplate template) {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
    }

    // 订阅pubsub:queue
    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory factory) {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        MessageListenerAdapter listener = new MessageListenerAdapter(new RedisMessageListener());
        container.addMessageListener(listener, new ChannelTopic("pubsub:queue"));
        return container;
    }

    public class RedisMessageListener implements MessageListener {
        @Override
        public void onMessage(final Message message, final byte[] pattern) {
            System.out.println("Message received: " + message.toString());
        }
    }
}
#redis相关配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=111
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=100
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=20
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=10
# 连接超时时间(毫秒)
spring.redis.timeout=0

原文地址:https://www.cnblogs.com/wujf/p/8473892.html

时间: 2024-08-30 08:16:39

spring boot集成redis的相关文章

Spring Boot 2.X(六):Spring Boot 集成 Redis

Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcached )相比有以下三个特点: 1.Redis 支持数据的持久化,它可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. 2.Redis 不仅仅支持简单的 key-value 类型的数据,同时还提供 list,set,zset,hash 等数据结构的存储. 3.Redis 支持数据的备份

【spring boot】【redis】spring boot 集成redis的发布订阅机制

一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送给对自己感兴趣的 消息订阅者们,进行消费. 2.redis的发布订阅和专业的MQ相比较 1>redis的发布订阅只是最基本的功能,不支持持久化,消息发布者将消息发送给频道.如果没有订阅者消费,消息就丢失了. 2>在消息发布过程中,如果客户端和服务器连接超时,MQ会有重试机制,事务回滚等.但是Red

spring boot 集成 redis lettuce(jedis)

spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot redis 缓存引入 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactI

spring boot集成redis缓存

spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.3.RELEASE</version> </depen

Spring boot集成Redis(1)—进行增加,更新,查询,批量删除等操作

前言:最近工作中使用到了redis缓存,故分享一点自己总结的东西,这篇文章使用的是StringRedisTemplate进行学习,这里值的说的是,(1)StringRedisTemplate在进行批量删除操作时我们需对template进行序列化,(2)更新操作与添加操作一样,接下来上代码:1.建立Spring boot项目,引入Redis依赖(pom.xml如下): <?xml version="1.0" encoding="UTF-8"?> <p

Spring boot集成Redis(2)—RedisTemplate的使用来存储Map集合

前言:上一篇文章我们用的是StringRedisTemplate,但是它存在一点问题,也迫使我重新写了代码,问题是:在我们往缓存中存入数字形式的String类型时,我们在利用Spring could将获取到的数据发送到另一服务时,我们发现数据已经被强转为Integer类型了,因为我们可能传输的数据庞大,类型多样,为了统一类型,以及开发方便,所以我将缓存改成RedisTemplate这种类型,进行增删改查的操作,文中没有特别举例更新操作,其更新操作与添加操作一样,当key一样时进行添加就会覆盖原v

spring boot 集成 Redis

前提:你已经安装了Redis 1.创建一个spring boot 工程 2.pom 引入依赖:spring-boot-starter-data-redis <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 3.在application

Spring Boot集成redis完整实例

添加依赖: (Spring Data Redis) 启动redis: 配置文件中进行配置: redis基本使用思路: redis中不存在就查询数据库然后存入redis: 查看日志: 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10850025.html

Spring Boot学习笔记——Spring Boot与Redis的集成

一.添加Redis缓存 1.添加Redis起步依赖 在pom.xml中添加Spring Boot支持Redis的依赖配置,具体如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </