【问题标题】:Issue Spring Boot Redis Multiple Applications Consuming Data发布 Spring Boot Redis 多个应用程序消耗数据
【发布时间】:2020-02-26 22:31:24
【问题描述】:

我有两个使用 Spring Boot 和 Redis 的应用程序。 从我正在生成数据的两个应用程序到 Redis。

ISSUE Spring Boot Redis Application 1 生成的数据不适用于 Redis Application 2,反之亦然。

Redis 正在本地运行。

两个应用程序的应用程序 YAML 相同 -

spring:
  redis:
    host: localhost
    port: 6379

模型类-

@RedisHash(timeToLive = 300,value = "alerts")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RedisModel {

    @Id
    private String id;

    private String message;

    public RedisModel(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "RedisModel{" +
                "id='" + id + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}

是否遗漏了一些参数? 如有任何疑问,请告诉我。 Spring Boot - 2.2.0 版本。

【问题讨论】:

  • 你能显示一些代码吗?关于主题的发布和消费
  • 我没有将它用作 pub 子模型...有一个带有 @Data 注释的模型类

标签: spring-boot redis spring-data-redis


【解决方案1】:

我认为 Spring Boot 创建的 RedisConfiguration 会将这些缓存键命名为您的应用程序名称。因此,它们彼此不可见。为了解决这个问题,你必须自己做 RedisConfiguration 并通过 disableKeyPrefix() 禁用前缀,然后使用 computePrefixWith(...) 设置你自己的前缀。这是一个例子:

    @Bean
    public RedisCacheManager cacheManager( RedisConnectionFactory redisConnectionFactory,
                                           ResourceLoader resourceLoader ) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
                .builder( redisConnectionFactory )
                .cacheDefaults( determineConfiguration( resourceLoader.getClassLoader() ) );
        List<String> cacheNames = this.cacheProperties.getCacheNames();
        if ( !cacheNames.isEmpty() ) {
            builder.initialCacheNames( new LinkedHashSet<>( cacheNames ) );
        }
        return builder.build();
    }

    private RedisCacheConfiguration determineConfiguration(
            ClassLoader classLoader ) {
        if ( this.redisCacheConfiguration != null ) {
            return this.redisCacheConfiguration;
        }
        CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        ObjectMapper mapper = new Jackson2ObjectMapperBuilder()
                .modulesToInstall( new SimpleModule().addSerializer( new NullValueSerializer( null ) ) )
                .failOnEmptyBeans( false )
                .build();
        mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY );

        ObjectMapper mapper = new Jackson2ObjectMapperBuilder()
                .modulesToInstall( new SimpleModule().addSerializer( new NullValueSerializer( null ) ) )
                .failOnEmptyBeans( false )
                .build();
        mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY );

        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer( mapper );

        //get the mapper b/c they registered some internal modules
        config = config.serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer( serializer ) );

        if ( redisProperties.getTimeToLive() != null ) {
            config = config.entryTtl( redisProperties.getTimeToLive() );
        }
        if ( redisProperties.getKeyPrefix() != null ) {
            config = config.prefixKeysWith( redisProperties.getKeyPrefix() );
        }
        if ( !redisProperties.isCacheNullValues() ) {
            config = config.disableCachingNullValues();
        }
        if ( !redisProperties.isUseKeyPrefix() ) {
            config = config.disableKeyPrefix();
            config = config.computePrefixWith( cacheName -> cacheName + "::" );
        }
        return config;
    }

【讨论】:

  • spring boot 将如何添加它,无论如何我都可以获取所有密钥,但所有密钥都为空
  • 你需要在你自己的 @Configuration 类中添加它来覆盖默认的 Redis 配置。
猜你喜欢
  • 2021-06-05
  • 2013-09-23
  • 2021-01-01
  • 2023-03-09
  • 2015-11-25
  • 2015-07-10
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多