【发布时间】:2019-03-21 06:17:02
【问题描述】:
我正在使用 spring-data-redis(2.1.5.RELEASE) 和 jedis(2.10.2) 客户端从作为 spring-boot 应用程序运行的不同服务连接到我的 azure redis 实例。
两个服务具有相同的缓存方法,并通过实现以下配置指向相同的缓存。我面临的问题是当一个服务试图读取另一个服务创建的缓存值时,会发生反序列化异常。
例外:
org.springframework.data.redis.serializer.SerializationException:无法反序列化;嵌套异常是 org.springframework.core.serializer.support.SerializationFailedException: 无法反序列化有效负载。字节数组是 DefaultDeserializer 对应序列化的结果吗?嵌套异常是 org.springframework.core.NestedIOException: 无法反序列化对象类型;嵌套异常是 java.lang.ClassNotFoundException
注意:我只使用redis来缓存从我的数据库中读取的数据。
微服务1的Redis缓存配置
public RedisCacheWriter redisCacheWriter(RedisConnectionFactory connectionFactory) {
return RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
}
@Bean
public RedisCacheManager cacheManager() {
Map<String, RedisCacheConfiguration> cacheNamesConfigurationMap = new HashMap<>();
cacheNamesConfigurationMap.put("employers", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(90000)));
cacheNamesConfigurationMap.put("employees", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(90000)));
RedisCacheManager manager = new RedisCacheManager(redisCacheWriter(), RedisCacheConfiguration.defaultCacheConfig(), cacheNamesConfigurationMap);
manager.setTransactionAware(true);
manager.afterPropertiesSet();
return manager;
}
微服务2的Redis缓存配置
public RedisCacheWriter redisCacheWriter(RedisConnectionFactory connectionFactory) {
return RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
}
@Bean
public RedisCacheManager cacheManager() {
Map<String, RedisCacheConfiguration> cacheNamesConfigurationMap = new HashMap<>();
cacheNamesConfigurationMap.put("employees", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(90000)));
RedisCacheManager manager = new RedisCacheManager(redisCacheWriter(), RedisCacheConfiguration.defaultCacheConfig(), cacheNamesConfigurationMap);
manager.setTransactionAware(true);
manager.afterPropertiesSet();
return manager;
}
两种服务中的缓存方法
@Cacheable(value = "employees", key = "#employeesId")
public Employee getEmployee(String employeesId) {
//methods
}
两种服务中的员工类
public class Employee implements Serializable {
private String id;
private String name;
}
【问题讨论】:
-
你能发布你得到的确切异常吗?
-
@MuhammadInshal 我已经添加了我面临的异常。
-
@Venkat 是这两个独立的应用程序,还是同一个应用程序部署了两次?
-
@Jerome 这两个是独立的应用程序,如果两次部署同一个应用程序,服务能够读取缓存数据(由该服务创建)
-
发现问题。 Employee.java 在不同的包中
标签: spring-boot spring-data-redis azure-redis-cache