小编Ven*_*kat的帖子

从不同的微服务读取相同的缓存

我使用 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;
}
Run Code Online (Sandbox Code Playgroud)

微服务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 …
Run Code Online (Sandbox Code Playgroud)

spring-data-redis spring-boot azure-redis-cache

5
推荐指数
1
解决办法
2710
查看次数