发行Spring Boot Redis多个使用数据的应用程序

时间:2019-10-31 14:25:55

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

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

问题,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版本。

1 个答案:

答案 0 :(得分:0)

我认为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;
    }