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的方式, 查看對應實現
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。
推薦閱讀:
- SpringBoot整合SpringDataRedis的示例代碼
- springboot2.5.0和redis整合配置詳解
- springboot 集成redis哨兵主從的實現
- SpringBoot整合RedisTemplate實現緩存信息監控
- SpringBoot集成Redis並實現主從架構的實踐