如何在 spring 中连接 Redis 和 Couchbase

How to connect Redis and Couchbase in spring

如何将 redis 和 couchbase 连接到我的 spring 应用程序。 我收到此错误 Parameter 0 of method couchbaseMappingContext in org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration required a single bean, but 2 were found: - couchbaseCustomConversions: defined by method 'customConversions' in class path resource [{classPath}/chat/config/CouchbaseConfiguration.class] - redisCustomConversions: defined in null 我只需要在一个包中使用 redis 'look',而其他包只需要与 couchbase 连接。

Redis 配置

  @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter(mapper);
        return converter;
    }

    @Bean
    public RedisTemplate<Long, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Long, ?> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        // Add some specific configuration here. Key serializers, etc.
        return template;
    }

Couchbase 配置

@EnableCouchbaseRepositories
@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {


    @Value("${spring.data.couchbase.bucket-name}")
    private String bucketName;
    
    @Value("${spring.couchbase.username}")
    private String username;

    @Value("${spring.couchbase.password}")
    private String password;
    
    @Value("${spring.couchbase.connection-string}")
    private String connectionString;

    @Override
    public String getConnectionString() {

        return this.connectionString;
    }


    @Override
    public String getUserName() {

        return this.username;
    }


    @Override
    public String getPassword() {

        return this.password;
    }

    @Override
    public String getBucketName() {
          
        return this.bucketName;
    }

}

当我第一次在终端启动我的应用程序时,有以下信息:Spring Data Redis - Could not safely identify store assignment for repository candidate interface

为了解决采用 customConversions bean 的歧义,我们可以告诉 couchbase 配置 class 如何创建 customConversions bean。将以下代码添加到扩展 AbstractCouchbaseConfiguration 的 class 应该可以解决问题

@Bean
  public CustomConversions customConversions() {
        return super.customConversions();
  }