JAVA中 redisTemplate 和 jedis的配合使用操作
作者:周永发 发布时间:2022-05-01 23:38:46
首先项目A,也就是SpringBOOT项目中使用redisTemplate 来做REDIS的缓存时,你会发现存到REDIS里边的KEY和VALUE,redisTemplat使用jdkSerializeable存储二进制字节编码
项目B中使用jedis时,存储起来的是字符串,导致项目A要调用项目缓存的键值对时,获取不到
解决方案:
修改项目A的redisTemplate的序列方式
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
/**
* redis模板,存储关键字是字符串,值是Jdk序列化
* @param factory
* @return
* @Description:
*/
@Bean
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//key序列化方式;但是如果方法上有Long等非String类型的话,会报类型转换错误;
RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
//默认使用JdkSerializationRedisSerializer序列化方式;会出现乱码,改成StringRedisSerializer
StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
}
补充:RedisTemplate初始化和创建(非Spring注入方式)
概述
在工作中, 可能会在非Spring项目(如Spark,Flink作业)中去操作Redis, 重复造轮子去写工具类没有太大的意义, 使用RedisTemplate已经足够丰富和完善了,使用New的方式进行创建即可, 不同的spring-data-redis的版本会略有不同, 下面以2.3.0和1.8.9做为示例.
2.3.0
maven
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
代码
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
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.core.ValueOperations;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class RedisTest {
public static void main(String[] args) {
//单机模式
RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
rsc.setPort(6379);
rsc.setPassword("123456");
rsc.setHostName("192.168.1.1");
//集群模式
RedisClusterConfiguration rcc = new RedisClusterConfiguration();
rcc.setPassword("123456");
List<RedisNode> nodes = Collections.singletonList(new RedisNode("192.168.1.1", 6379));
rcc.setClusterNodes(nodes);
RedisTemplate<String, String> template = new RedisTemplate<>();
//单机模式
JedisConnectionFactory fac = new JedisConnectionFactory(rsc);
//集群模式
//JedisConnectionFactory fac = new JedisConnectionFactory(rcc);
fac.afterPropertiesSet();
template.setConnectionFactory(fac);
template.setDefaultSerializer(new StringRedisSerializer());
template.afterPropertiesSet();
ValueOperations<String, String> op = template.opsForValue();
final String key = "123_tmp";
final String value = "abc";
template.delete(key);
op.set(key, value);
assert Objects.equals(op.get(key), value);
}
}
集群方式运行报错
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled
解决
在redis.conf下将cluster-enabled改为yes
如果只有一个节点, 改为单机模式
1.8.9
maven
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.9.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
代码
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import java.util.Objects;
public class RedisTest {
public static void main(String[] args) {
RedisTemplate<String, String> template = new RedisTemplate<>();
JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());
JedisShardInfo shardInfo = new JedisShardInfo("192.168.1.1", 6379);
shardInfo.setPassword("123456");
fac.setShardInfo(shardInfo);
template.setConnectionFactory(fac);
template.setDefaultSerializer(new StringRedisSerializer());
template.afterPropertiesSet();
ValueOperations<String, String> op = template.opsForValue();
final String key = "123_tmp";
final String value = "abc";
template.delete(key);
op.set(key, value);
assert Objects.equals(op.get(key), value);
}
}
这里有个小细节, 如果不调用setShardInfo()方法, 那么需要执行下面的代码, afterPropertiesSet()用来初始化
JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());
fac.setPort(6379);
fac.setPassword("123456");
fac.setHostName("192.168.1.1");
fac.afterPropertiesSet();
说明
RedisTemplate的构造方法有多种, 上面所举例子为其中的一种; 不通过SpringBoot自动装配的方式, 必须手动去执行afterPropertiesSet()进行初始化; 可参考SpringBoot整合redis的方式, 查看对应实现
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://www.cnblogs.com/yvanBk/p/10497542.html


猜你喜欢
- 本文实例讲述了Android Dialog对话框用法。分享给大家供大家参考,具体如下:Activities提供了一种方便管理的创建、保存、回
- 目前的设计中是支持单体声和立体声自动切换的。切换是需要在一定的条件下满足才会进行切换,切换的条件和电台的信号强度RSSI、信号稳定性CQI等
- Java 定时器在JAVA中实现定时器功能要用的二个类是Timer,TimerTaskTimer类是用来执行任务的类,它接受一个
- Spark_SQL的UDF使用用户自定义函数,也叫UDF,可以让我们使用Python/Java/Scala注册自定义函数,并在SQL中调用。
- 最近一门课要求编写一个上位机串口通信工具,我基于Java编写了一个带有图形界面的简单串口通信工具,下面详述一下过程,供大家参考 ^_^一:首
- 本文实例讲述了C#实现发送邮件的三种方法。分享给大家供大家参考。具体方法分析如下:一、问题:最近公司由于一个R&I项目的需要,用户要
- 学习spring依赖注入的时候碰到这个坑,折腾了许久,记录一下以防其他小伙伴入坑!该异常主要原因是因为JDK与Spring版本不一致。要么更
- 看了Android版QQ的自定义头像功能,决定自己实现,随便熟悉下android绘制和图片处理这一块的知识。先看看效果:思路分析:这个效果可
- Android CheckBox中设置padding无效解决办法CheckBox使用本地图片资源CheckBox是Android中用的比较多
- 前言在Mac中用android studio 导出jar包最重要的是需要配置gradle ,它包的导出也是通过gradle命令进行的。所以,
- 对上次的三个问题的个人理解: 1) 程序首先是从main函数开始执行的,假设main 函数不是 static ,就要先实例化这个类,然后调用
- 前言初识Android PowerManagerService省电模式 让我们省电模式的概念有了初步的认识,Androi
- 前言大富翁,又名地产大亨。是一种多人策略图版游戏。参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地、建楼以赚取租金。英文原名monopo
- 1.用途在SpringBoot中,通过jasypt可以进行加密解密. 这个是双向的, 且可以配置密钥.2.使用:2.1通过UT创建工具类,并
- private void button2_Click(object sender, EventArgs e) &nbs
- 在上篇文章给大家介绍了Android Studio 3.6 正式版终于发布了,快来围观,需要的朋友可以点击查看,今天给大家分享我安装Andr
- 本文实例讲述了C#实现winform自动关闭MessageBox对话框的方法。分享给大家供大家参考。具体实现方法如下:using Syste
- 自从使用 HttpClient 和 Jsoup 配合编写了几个简单的入门爬虫之后,发现对于绝对路径的需求是很频繁的,因为大部分的网页都写相对
- 一、日志1、配置日志级别日志记录器(Logger)的行为是分等级的。如下表所示:分为:OFF、FATAL、ERROR、WARN、INFO、D
- 目录示例原理DefaultBeanDefinitionDocumentReaderparseDefaultElementimportBean