Spring boot 问题Spring Boot Redis使用数据的多个应用程序

Spring boot 问题Spring Boot Redis使用数据的多个应用程序,spring-boot,redis,spring-data-redis,Spring Boot,Redis,Spring Data Redis,我有两个应用程序,我正在使用SpringBoot和Redis。 我正在从这两个应用程序向Redis生成数据 问题Spring Boot Redis应用程序1生成的数据不适用于Redis应用程序2,反之亦然 Redis正在本地运行 两种应用程序的应用程序YAML相同- spring: redis: host: localhost port: 6379 模型类- @RedisHash(timeToLive = 300,value = "alerts") @Data @NoArg

我有两个应用程序,我正在使用SpringBoot和Redis。 我正在从这两个应用程序向Redis生成数据

问题Spring Boot Redis应用程序1生成的数据不适用于Redis应用程序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 + '\'' +
                '}';
    }
}
是否遗漏了一些参数?? 如果有任何疑问,请告诉我。
SpringBoot-2.2.0版本。

我认为SpringBoot创建的配置将使用您的应用程序名称命名这些缓存键。因此,它们彼此不可见。要解决此问题,您必须进行自己的重新配置,并通过
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;
    }
@Bean
公共RedisCacheManager cacheManager(RedisConnectionFactory RedisConnectionFactory,
资源加载器(ResourceLoader){
RedisCacheManager.RedisCacheManagerBuilder=RedisCacheManager
.builder(重新连接工厂)
.cacheDefaults(determineConfiguration(resourceLoader.getClassLoader());
List cacheNames=this.cacheProperties.getCacheNames();
如果(!cacheNames.isEmpty()){
builder.initialCacheNames(新LinkedHashSet(cacheNames));
}
返回builder.build();
}
私有重新缓存配置确定配置(
类加载器(类加载器){
if(this.redisCacheConfiguration!=null){
返回此.redisCacheConfiguration;
}
CacheProperties.Redis redisProperties=this.CacheProperties.getRedis();
RedisCacheConfiguration=RedisCacheConfiguration.defaultCacheConfig();
ObjectMapper映射器=新Jackson2ObjectMapperBuilder()
.modulesToInstall(新的SimpleModule().addSerializer(新的NullValueSerializer(null)))
.failOnEmptyBeans(false)
.build();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.PROPERTY);
ObjectMapper映射器=新Jackson2ObjectMapperBuilder()
.modulesToInstall(新的SimpleModule().addSerializer(新的NullValueSerializer(null)))
.failOnEmptyBeans(false)
.build();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.PROPERTY);
GenericJackson2JsonRedisSerializer=新的GenericJackson2JsonRedisSerializer(映射器);
//获取映射器b/c他们注册了一些内部模块
config=config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromsserializer(serializer));
if(redisProperties.getTimeOlive()!=null){
config=config.entryTtl(redisProperties.getTimeOlive());
}
if(redisProperties.getKeyPrefix()!=null){
config=config.prefixKeysWith(redisProperties.getKeyPrefix());
}
如果(!redisProperties.isCacheNullValues()){
config=config.disableCachingNullValues();
}
如果(!redisProperties.isUseKeyPrefix()){
config=config.disableKeyPrefix();
config=config.computePrefixWith(cacheName->cacheName+“:”);
}
返回配置;
}

你能给我看一些代码吗。关于topicsI中的发布和消费,我不使用它作为发布子模型…有一个带有@Data annotation的模型类spring boot将如何添加它,无论如何,我可以获取所有键,但所有键都将为空。您需要在您自己的@Configuration类中添加它,以覆盖默认的Redis配置。