Springboot2.x集成单节点Redis

说明

在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客户端来操作Redis。Springboot 提供了RedisTemplate来统一封装了对Redis操作,开发者只需要使用RedisTemplate 就可以实现,而不用关心具体的操作实现。

准备条件

pom.xml中引入相关jar

		<!-- 集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <!-- Jedis 客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency> <!-- lettuce客户端需要使用到 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

application.yml配置属性示例。

spring:
redis:
host: 192.168.8.121
port: 6379
password: enjoyitlife
timeout: 30000
jedis:
pool:
max-active: 256
max-wait: 30000
max-idle: 64
min-idle: 32
lettuce:
pool:
max-active: 256
max-idle: 64
max-wait: 30000
min-idle: 32

单机模式下的整合教程

Jedis客户端整合

JedisSingleConfig.java 相关配置

package top.enjoyitlife.redis.jedis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration
@Profile("jedisSingle")
public class JedisSingleConfig { @Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis; @Bean
public JedisConnectionFactory redisConnectionFactory() throws Exception{
RedisStandaloneConfiguration rc= new RedisStandaloneConfiguration();
rc.setHostName(host);
rc.setPort(port);
rc.setPassword(password);
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(rc);
return connectionFactory;
} @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
} }

Lettuce客户端整合

package top.enjoyitlife.redis.lettuce;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration
@Profile("lettuceSingle")
public class LettuceSingleConfig { @Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password; @Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration rc= new RedisStandaloneConfiguration();
rc.setHostName(host);
rc.setPort(port);
rc.setPassword(password);
// 关键区别点
return new LettuceConnectionFactory(rc);
} @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
} }

Springboot通过RedisStandaloneConfiguration来统一了连接方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。

单元测试

Jedis单元测试

JedisSingleTest.java 单元测试代码。

package top.enjoyitlife.redis.jedis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles; @SpringBootTest
@ActiveProfiles("jedisSingle")
class JedisSingleTest { @Autowired
private RedisTemplate<String, Object> redisTemplate; @Test
void contextLoads() {
String name=redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
}
}

Lettuce 单元测试

package top.enjoyitlife.redis.lettuce;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles; @SpringBootTest
@ActiveProfiles("lettuceSentinel")
class LettuceSingleTest { @Autowired
private RedisTemplate<String, Object> redisTemplate; @Test
void contextLoads() {
String name = redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
} }

补充

Jedis在是直接连接的的redis实例,如果在多线程环境下是非线程安全的,只有使用连接池,为每个Jedis增加物理连接。

而Lettuce的连接是基于Netty事件驱动模型的,连接实例可以在多个线程间并发访问,StatefulRedisConnection是线程安全的,可以满足多线程环境下的并发访问。

从Springboot 2.X默认采用了Lettuce客户端来看,官方还是建议使用Lettuce。

Springboot2.x集成单节点Redis的更多相关文章

  1. kubernetes环境部署单节点redis

    kubernetes部署redis数据库(单节点) redis简介 Redis 是我们常用的非关系型数据库,在项目开发.测试.部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存.这里介绍 ...

  2. springboot2.1.3集成单节点elasticsearch6.4.0

    本案例写了一个关于医生医院搜索的例子,包括求和模式下的打分(分值与相关性有关)搜索,单节点时切勿配置节点名称和节点ip.github地址:https://github.com/zhzhair/spri ...

  3. Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)

    文/朱季谦 背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯 ...

  4. centos7.2 下 部署单节点redis 3.2.5

    #tar -xvf redis.3.2.5.tar.gz –C /usr/local/ #cd /usr/local/ #mv redis.3.2.5 redis #cd redis #make &a ...

  5. 单节点Redis使用 Python pipline大批量插入数据

    方法一: import redis import time filename = 'redis_resulit.txt' def openPool():     pool = redis.Connec ...

  6. dcoker 搭建单节点redis

    1.安装docker 1.检查内核版本,必须是3.10及以上 [root@localhost ~]# uname -r 2.安装docker [root@localhost ~]# yum insta ...

  7. Redis单节点数据同步到Redis集群

    一:Redis集群环境准备 1:需要先安装好Redis集群环境并配置好集群 192.168.0.113 7001-7003 192.168.0.162 7004-7006 2:检查redis集群 [r ...

  8. C# Redis分布式锁 - 单节点

    为什么要用分布式锁? 先上一张截图,这是在浏览别人的博客时看到的. 在了解为什么要用分布式锁之前,我们应该知道到底什么是分布式锁. 锁按照不同的维度,有多种分类.比如 1.悲观锁,乐观锁; 2.公平锁 ...

  9. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

随机推荐

  1. FCC 成都社区·前端周刊 第 6 期

    01. JS 引擎 V8 v6.6 的更新 最新 v6.6 版本的 V8 JavaScript 引擎更新了方法 Function.prototype.toString(),改进了代码缓存机制.异步性能 ...

  2. Windows 2012 R2 DataCenter服务器 重启之后,其他加域电脑无法访问域账户

    需在域控服务器重启,服务Kerberos Key

  3. Vue3.0 Function API---------引用

    1.了解 Vue 3.0 是否有 break change,就像 Python 3 / Angular 2 一样? 不,100% 兼容 Vue 2.0,且暂未打算废弃任何 API(未来也不).之前有草 ...

  4. shiro框架学习-4- Shiro内置JdbcRealm

    1.  JdbcRealm 数据库准备 JdbcRealm就是用户的角色,权限都从数据库中读取,也就是用来进行用户认证授权的安全数据源更换为从数据库中读取,其他没有差别,首先在数据库创建三张表: CR ...

  5. msyql 优化之五不要

    1.尽量不要有空判断的语句,因为空判断将导致全表扫描,而不是索引扫描. 对于空判断这种情况,可以考虑对这个列创建数据库默认值 //空判断将导致全表扫描 select small_id from sma ...

  6. QT 问题提问网站

    https://stackoverflow.com/questions/tagged/qt

  7. golang rabbitmq实践 (二 实现简单的消息收发)

    1:驱动 本来打算自己写一个驱动的,后来发现github上面已经有了,那我就直接拿现成的了, 驱动采用 github.com/streadway/amqp ,直接import就可以啦! 2:excha ...

  8. docker安装xxl-job

    一 安装mysql root@localhost tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD= ...

  9. Codeforces Round #403---C题(DFS,树)

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  10. 打开远程桌面时总提示无法打开连接文件default.rdp

    删除C:\Users\Administrator\Documents\default.rdp,再启动远程就好了 http://www.chahushequ.com/read-topic-94-2fa9 ...