JAVA中 redisTemplate 和 jedis的配合使用操作
首先项目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的方式, 查看对应实现
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。