【问题标题】:Issue with Spring data cassandra due to Spring boot version upgrade to 2.3.1由于 Spring 引导版本升级到 2.3.1,Spring 数据 cassandra 出现问题
【发布时间】:2020-10-09 18:31:33
【问题描述】:

由于 Spring boot 2.3 版本中对 docker 镜像支持的改进,我们决定迁移到该版本。在我们的项目中,我们使用 Cassandra 作为数据库之一。但是除了迁移到 cassandra 驱动程序版本 4 之外,spring data cassandra 似乎发生了很多变化。问题是这个异常不允许应用程序启动,

 java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter)

现在,我在网上搜索,发现有人建议:

将此属性添加到我的 application.properties:

spring.data.cassandra.local-datacenter=datacenter1 (in my case since in the exception that it throws, it's mentioned that the local datacenter is - datacenter1)

并在创建我正在做的 CqlSession bean 时指定它:

public CqlSession session() {

     String containerIpAddress = getContactPoints();
     int containerPort = getPort();
     InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);

    return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
            .addContactPoint(containerEndPoint)
            .withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
            .withKeyspace(getKeyspaceName()).build(); }

但我仍然卡住,无法启动应用程序。如何解决这个问题?

【问题讨论】:

    标签: spring-boot spring-data-cassandra cassandra-driver


    【解决方案1】:

    我使用的是 Reactive Cassandra,但基本相同。新的数据中心字段可以通过登录到您的 cassandra 实例并运行

    select data_center from system.local;
    

    我的配置是这样的:

    @Configuration
    @EnableReactiveCassandraRepositories
    public class CassandraConfig {
    
      @Value("${spring.data.cassandra.contact-points}")
      private String hostList;
    
      @Value("${spring.data.cassandra.datacenter}")
      private String datacenter;
    
      @Value("${spring.data.cassandra.keyspace-name}")
      private String keyspaceName;
    
      @Value("${spring.data.cassandra.username}")
      private String userName;
    
      @Value("${spring.data.cassandra.password}")
      private String password;
    
      @Value("${spring.data.cassandra.port}")
      private Integer port;
    
      @Bean
      public CqlSessionFactoryBean getCqlSession() {
        CqlSessionFactoryBean factory = new CqlSessionFactoryBean();
        factory.setUsername(userName);
        factory.setPassword(password);
        factory.setPort(port);
        factory.setKeyspaceName(keyspaceName);
        factory.setContactPoints(hostList);
        factory.setLocalDatacenter(datacenter);
        return factory;
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      使用此配置对我有用

      public CqlSession session() {
              return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
                      .addContactPoint(InetSocketAddress.createUnresolved(getContactPoints(), 9042))
                      //.withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
                      .withKeyspace(getKeyspaceName()).build();
          }
      
      
      @Bean
          @ConditionalOnMissingBean
          public CassandraTemplate cassandraTemplate(CqlSession session) throws Exception {
              return new CassandraTemplate(session);
          }
      

      【讨论】:

        【解决方案3】:

        Apache Cassandra 实际上需要一个不同的配置才能真正与新的 Spring 版本一起工作。我花了大约 50 个小时搜索才在本文档中找到这一点 - https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.cassandra-java-config

        您需要创建一个在那里指定的新配置文件。为此,请遵循以下步骤:

        创建一个名为config的新包 在名为 - CassandraConfig 的包中创建一个 .java 类文件 将以下代码复制并粘贴到其中。

        @Configuration public class CassandraConfig { public @Bean CqlSession session() { return CqlSession.builder().withKeyspace("mykeyspacename").build(); } }
        

        注意,将 Keyspace 名称更改为您在属性文件中定义的 Keyspace 名称。如果您尚未创建 Keyspace,请在系统上启动 Cassandra 服务器,在终端上登录 cqlsh 或通过发出 cqlsh 登录 cmd,然后发出此命令 -

        CREATE KEYSPACE IF NOT EXISTS mykeyspacename WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 } AND DURABLE_WRITES = true ;
        

        现在,当您启动 spring boot 应用程序时,它应该可以正常运行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-23
          • 2021-12-30
          • 2016-09-01
          • 2020-11-01
          • 2020-12-02
          • 2021-12-29
          • 2023-04-01
          • 1970-01-01
          相关资源
          最近更新 更多