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. MyEclipse使用教程:使用主题自定义工作台外观

    [MyEclipse CI 2019.4.0安装包下载] 您可以通过选择Window>Preferences>General>Appearance来使用主题自定义的工作台外观,可以从 ...

  2. oracle 创建表的规则

    1.表明首字母 应该为字母 2.表名的最大长度为30个字符 3.不能使用oracle保留字和关键字来作表名 4.同一用户下的不同表不能具有相同的名称 5.可以使用下划线.数字和字母,但不能使用空格与单 ...

  3. Linux系统中的硬件问题如何排查?(1)

    在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工作,即使是经验相当丰富的用户有时也会遇上自己搞不定的状况,本文分享一些实用的技巧与处理方法,希望有助于读者朋友理解.查明并最终搞 ...

  4. Spring 跨重定向请求传递数据

    在处理完POST请求后, 通常来讲一个最佳实践就是执行一下重定向.除了其他的一些因素外,这样做能够防止用户点击浏览器的刷新按钮或后退箭头时,客户端重新执行危险的POST请求. 在控制器方法返回的视图名 ...

  5. jvm——内存模型

    这是我理解的jvm内存模型,这一整块可以简单理解为虚拟内存空间: jvm代码.jvm数据:是运行jvm所用到的代码和数据,并不是我们自己编程得到的代码和数据 direct memory:主要是NIO在 ...

  6. make all age=20 makefile 传参数

    你可以直接在Make的时候,在命令行下面传入一个参数或者变量,它会覆盖掉Makefile里面的变量或者参数.比如,在你的Makefile里面定义了一个变量age. .PHONY:all AGE = a ...

  7. smooth L1损失函数

    当预测值与目标值相差很大时,L2 Loss的梯度为(x-t),容易产生梯度爆炸,L1 Loss的梯度为常数,通过使用Smooth L1 Loss,在预测值与目标值相差较大时,由L2 Loss转为L1  ...

  8. 简单实现骨架屏 (Skeleton Screens)

          近年,国内外各大网站都引入骨架屏(Skeleton Screens)技术来提高用户体验.相比于之前的Loading动画,骨架屏页面更容易让用户产生一种错觉,页面快加载完了.骨架屏实现原理很 ...

  9. 浅谈javaweb三大框架和MVC设计模式

    一.MVC设计模式 1.MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(Vie ...

  10. mysql 5.7 安装配置及无法启动的问题解决

    (用这篇配置就能正常配置成功) mysql 免安装版配置方法: https://www.jb51.net/article/134452.htm 参考:https://blog.csdn.net/qq_ ...