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. [bzoj4358]permu:莫队+线段树/回滚莫队

    这道题是几天前水过去的,现在快没印象了,水一发. 首先我们看到它让求解的是最长的值域 连续段长度,很好. 然后就想到了山海经,但但是我还没有做. 然后又想到了很久以前的一次考试的T3旅馆hotel(我 ...

  2. vue项目中引入vant 使用rem布局问题

    postcss.config.js const autoprefixer = require('autoprefixer') const pxtorem = require('postcss-pxto ...

  3. mysql视图创建

    create or replace view partner_v as select a.id,vendorname,address,nation,contactperson,tel1,tel2,jy ...

  4. es6 Object.assign(target, ...sources)

    Object.assign() 方法用于将所有可枚举属性(对象属性)的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...sources) ...

  5. Arduino连接MPU6050陀螺仪

    一.线路连接 Arduino MPU6050 VCC 3.3V/5V GND GND SCL A5 SDA A4 INT D2 二.库下载 https://pan.baidu.com/s/1nvt75 ...

  6. linux安装配置Redis,Swoole扩展

    我是使用的是lnmp环境(php5.6.3) 一.安装redis数据库(参考w3c手册) 下载地址:http://redis.io/download 本教程使用的最新文档版本为 2.8.17,下载并安 ...

  7. Redis实战(十七)Redis各个版本新特性

    序言 Redis1.0 Redis2.0 Redis3.0 Redis4.0 Redis5.0 资料

  8. codevs 1126 数字统计 2010年NOIP全国联赛普及组 x

    题目描述 Description 请统计某个给定范围[L, R]的所有整数中,数字2出现的次数. 比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21 ...

  9. mvn deploy上传私服Nexus时遇到的坑--ReasonPhrase: Forbidden.

    用maven deploy上传jar包时,遇到了ReasonPhrase: Forbidden 错误,截图如下: 折腾了好久,终于解决,现把步骤简单记录如下: 1.自建仓库eagle-sky: 2.设 ...

  10. 快速排序和二分查找(Go)

    package main import ( "fmt" "sync") var waitGroup sync.WaitGroup func main() { d ...