Springboot2.x集成单节点Redis
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的更多相关文章
- kubernetes环境部署单节点redis
kubernetes部署redis数据库(单节点) redis简介 Redis 是我们常用的非关系型数据库,在项目开发.测试.部署到生成环境时,经常需要部署一套 Redis 来对数据进行缓存.这里介绍 ...
- springboot2.1.3集成单节点elasticsearch6.4.0
本案例写了一个关于医生医院搜索的例子,包括求和模式下的打分(分值与相关性有关)搜索,单节点时切勿配置节点名称和节点ip.github地址:https://github.com/zhzhair/spri ...
- Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)
文/朱季谦 背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯 ...
- 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 ...
- 单节点Redis使用 Python pipline大批量插入数据
方法一: import redis import time filename = 'redis_resulit.txt' def openPool(): pool = redis.Connec ...
- dcoker 搭建单节点redis
1.安装docker 1.检查内核版本,必须是3.10及以上 [root@localhost ~]# uname -r 2.安装docker [root@localhost ~]# yum insta ...
- Redis单节点数据同步到Redis集群
一:Redis集群环境准备 1:需要先安装好Redis集群环境并配置好集群 192.168.0.113 7001-7003 192.168.0.162 7004-7006 2:检查redis集群 [r ...
- C# Redis分布式锁 - 单节点
为什么要用分布式锁? 先上一张截图,这是在浏览别人的博客时看到的. 在了解为什么要用分布式锁之前,我们应该知道到底什么是分布式锁. 锁按照不同的维度,有多种分类.比如 1.悲观锁,乐观锁; 2.公平锁 ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
随机推荐
- 甘特图 dhtmlx 插件
https://dhtmlx.com/docs/products/demoApps/advanced-gantt-chart/
- thinkPHP模型定义
批量新增 ArrayAccess类的属性当做数组访问 插入语句 这段代码说明,User继承的Model类的isupdate属性默认是isupdate,而User::get(1)把这一字段属性更新为tr ...
- linux 环境下安装MySQL5.7(yum)
安装环境: CentOS7 64位,MySQL5.7 原文链接:https://blog.csdn.net/xyang81/article/details/51759200 1. 配置yum源 在My ...
- LOJ-6277-数列分块入门1(分块)
链接: https://loj.ac/problem/6277 题意: 给出一个长为 的数列,以及 个操作,操作涉及区间加法,单点查值. 思路: 线段树可以解决,用来学习分块. 分块概念就是,将序列分 ...
- Docker(3)--常用命令
1.docker -h 帮助 2.获取镜像 docker pull NAME[:TAG] [root@node3 ~]#docker pull centos:latest 3.启动Container盒 ...
- 【NOIP2016提高A组五校联考1】挖金矿
题目 分析 我们二分答案 设\(sum_{i,j}\)表示的i列前个数的和, 假设当前出的二分答案为x,第i列挖了\(h_j\)层,则 \[\dfrac{\sum_{i=1}^{n}sum_{i,h_ ...
- 【GDKOI2013选拔】大LCP
题目 LCP就是传说中的最长公共前缀,至于为什么要加上一个大字,那是因为-你会知道的. 首先,求LCP就要有字符串.既然那么需要它们,那就给出n个字符串好了. 于是你需要回答询问大LCP,询问给出一个 ...
- jvm——CMS 垃圾回收器(未完)
https://matt33.com/2018/07/28/jvm-cms/ 阶段1:Initial Mark stop-the-wolrd 标记那些直接被 GC root 引用或者被年轻代存活对象所 ...
- OI实用网址
LaTeX公式编辑器 https://www.codecogs.com/latex/eqneditor.php http://latex.91maths.com/ 图.坐标系绘制 https://cs ...
- windows与ubuntu双系统的安装
将ubuntu镜像烧录至U盘,从U盘启动电脑 选择自定义安装,不要选择它本身的双系统选项. 我的方案分区: 1. 挂载点/:主分区:安装系统和软件:大小为30G:分区格式为ext4:2. 挂载点/ho ...