前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson

Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson

作者头像
小小工匠
发布2021-08-17 16:35:59
发布2021-08-17 16:35:59
2.7K00
代码可运行
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构
运行总次数:0
代码可运行

官网

https://spring.io/projects/spring-data-redis

我们知道常用的Redis客户端 https://redis.io/clients#java

怎么还有 Spring Data Redis ?

莫慌,小兄弟, 来看个关系图 帮你捋一捋


Jedis VS Lettuce

在 spring-boot-starter-data-redis 项目 2.X 版本中 ,默认使用 Lettuce 作为 Java Redis 工具库 , 为啥不用jedis 了?

Why is Lettuce the default Redis client used in Spring Session Redis? #789

也有可能跟jedis 有一段时间不更新了有关系~


Jedis Code

POM依赖

代码语言:javascript
代码运行次数:0
运行
复制
  >

        <!-- 实现对 Spring Data Redis 的自动化配置 -->
        
            org.springframework.boot
            spring-boot-starter-data-redis
            
                
                
                    io.lettuce
                    lettuce-core
                
            
        

        <!--  Jedis -->
        
            redis.clients
            jedis
        

        <!-- 单元测试 -->
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        <!-- 使用 fastjson 作为 JSON 序列化的工具 -->
        
            com.alibaba
            fastjson
            1.2.61
        

        <!-- Spring Data Redis 默认使用 Jackson 作为 JSON 序列化的工具 -->
        
            com.fasterxml.jackson.core
            jackson-databind
        
 
    >

配置文件

代码语言:javascript
代码运行次数:0
运行
复制
spring:
  # 对应 RedisProperties 类
  redis:
    host: 127.0.0.1
    port: 6379
    password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
    database: 0 # Redis 数据库号,默认为 0 。
    timeout: 0 # Redis 连接超时时间,单位:毫秒。
    # 对应 RedisProperties.Jedis 内部类
    jedis:
      pool:
        max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。
        max-idle: 8 # 默认连接数最小空闲的连接数,默认为 8 。使用负数表示没有限制。
        min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。
        max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。

配置类

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2021/2/16 21:07
 * @mark: show me the code , change the world
 */


@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory connectionFactory;


    public RedisTemplate<String, Object> redisTemplate() {
 
	  		RedisTemplate<String,Object> template = new RedisTemplate();
	        template.setConnectionFactory(connectionFactory);
	        // 序列化工具
	        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
	        ObjectMapper om = new ObjectMapper();
	        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	        jackson2JsonRedisSerializer.setObjectMapper(om);
	
	        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
	        template.setKeySerializer(stringRedisSerializer);
	        template.setValueSerializer(jackson2JsonRedisSerializer);
	
	        template.setHashKeySerializer(jackson2JsonRedisSerializer);
	        template.setHashValueSerializer(jackson2JsonRedisSerializer);
	
	        template.afterPropertiesSet();

        return template;
    }
}

单元测试

代码语言:javascript
代码运行次数:0
运行
复制
 package com.artisan;



import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2021/2/16 20:56
 * @mark: show me the code , change the world
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class JedisTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testStringSetKey() {
        // set
        stringRedisTemplate.opsForValue().set("artisan", "good1");
        // get
        String artisan = stringRedisTemplate.opsForValue().get("artisan");
        System.out.println(artisan);
    }


}

Lettuce Code

2.x 以上默认 使用的客户端为Lettuce , 参考 Spring Session - 使用Spring Session从零到一构建分布式session

这里就不赘述了。


Redisson Code

POM依赖

代码语言:javascript
代码运行次数:0
运行
复制
	<dependencies>

        <!--  Redisson  -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.11.3</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>


    </dependencies>

配置文件

【application.yml】

代码语言:javascript
代码运行次数:0
运行
复制
spring:
  # 对应 RedisProperties 类
  redis:
    host: 127.0.0.1
    port: 6379
#    password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
    database: 0 # Redis 数据库号,默认为 0 。
    timeout: 0 # Redis 连接超时时间,单位:毫秒。
    # 对应 RedissonProperties 类  如果为空 需要注释掉
#    redisson:
#      config: classpath:redisson.yml # 具体的每个配置项,见 org.redisson.config.Config 类。

使用 Spring Boot 整合 Redisson 时候,通过该配置项,引入一个外部的 Redisson 相关的配置文件 ,引入了 classpath:redisson.yaml 配置文件

引入的 redisson.config 对应的配置文件,对应的类是 org.redisson.config.Config 类。因为示例中,我们使用的比较简单,所以就没有做任何 Redisson 相关的自定义配置。 如果没有配置任何内容,需要在 application.yml 里注释掉 redisson.config 。

具体配置信息可参考 Spring Boot2.x 整合lettuce redis 和 redisson


配置类

同上

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2021/2/16 21:07
 * @mark: show me the code , change the world
 */


@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory connectionFactory;


    public RedisTemplate<String, Object> redisTemplate() {
 
	  		RedisTemplate<String,Object> template = new RedisTemplate();
	        template.setConnectionFactory(connectionFactory);
	        // 序列化工具
	        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
	        ObjectMapper om = new ObjectMapper();
	        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	        jackson2JsonRedisSerializer.setObjectMapper(om);
	
	        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
	        template.setKeySerializer(stringRedisSerializer);
	        template.setValueSerializer(jackson2JsonRedisSerializer);
	
	        template.setHashKeySerializer(jackson2JsonRedisSerializer);
	        template.setHashValueSerializer(jackson2JsonRedisSerializer);
	
	        template.afterPropertiesSet();

        return template;
    }
}

单元测试

代码语言:javascript
代码运行次数:0
运行
复制
   @Test
    public void test() {
        stringRedisTemplate.opsForValue().set("uuu", "xxxxx");
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 官网
  • Jedis VS Lettuce
  • Jedis Code
    • POM依赖
    • 配置文件
    • 配置类
    • 单元测试
  • Lettuce Code
  • Redisson Code
    • POM依赖
    • 配置文件
    • 配置类
    • 单元测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档