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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java集合迭代器Iterator中的remove陷阱
java插入排序 Insert sort实例
Java事件处理机制和适配器全面解析
![](https://img.aspxhome.com/file/2023/0/73070_0s.png)
利用Java计算某个日期是星期几
Jenkins+Maven+SVN自动化部署java项目
![](https://img.aspxhome.com/file/2023/7/61837_0s.png)
详解Spring Security如何配置JSON登录
![](https://img.aspxhome.com/file/2023/3/72353_0s.png)
详解Java实现设计模式之责任链模式
![](https://img.aspxhome.com/file/2023/7/72557_0s.jpg)
Java简单实现调用命令行并获取执行结果示例
![](https://img.aspxhome.com/file/2023/1/77941_0s.png)
JAVA回顾:封装,继承,多态
![](https://img.aspxhome.com/file/2023/7/67627_0s.png)
C#特性-对象集合初始化器介绍
Mybatis如何获取最新插入数据的id
java中单例模式讲解
![](https://img.aspxhome.com/file/2023/5/60335_0s.png)
Java实战之制作在线音乐网站
![](https://img.aspxhome.com/file/2023/5/71145_0s.jpg)
C#中类的使用教程详解
使用jenkins部署springboot项目的方法步骤
![](https://img.aspxhome.com/file/2023/0/62760_0s.png)
IDEA离线安装maven helper插件的图文教程
![](https://img.aspxhome.com/file/2023/3/78053_0s.jpg)
SpringBoot处理接口幂等性的两种方法详解
Java使用fill()数组填充的实现
Java线程安全解决方案(synchronized,ReentrantLock,Atomic)
Java @Accessors注解图文详解
![](https://img.aspxhome.com/file/2023/2/73072_0s.png)