簡體   English   中英

由於 Spring 引導版本升級到 2.3.1,導致 Spring 數據 cassandra 出現問題

[英]Issue with Spring data cassandra due to Spring boot version upgrade to 2.3.1

由於 Spring 啟動的 2.3 版本中對 docker 映像支持的改進,我們決定遷移到此版本。 在我們的項目中,我們使用 Cassandra 作為數據庫之一。 但似乎 spring 數據 cassandra 發生了很大變化,除了遷移到 cassandra 驅動程序版本 4。

 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(); }

但是我仍然被卡住並且無法啟動應用程序。 如何解決這個問題?

我使用的是反應式 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;
  }

}

使用此配置對我有用

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);
    }

Apache Cassandra 實際上需要不同的配置才能真正使用新的 spring 版本。 我花了大約 50 個小時的搜索才在本文檔中找到了這一點 - https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.cassandra-java-config

您需要創建一個在那里指定的新配置文件。 為此,請執行以下操作:

Create a new package named config Create a.java class file in the package named - CassandraConfig Copy and paste the below code into it.

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

請注意,將 Keyspace 名稱更改為您在屬性文件中定義的 Keyspace 名稱。 如果您還沒有創建密鑰空間,請在您的系統上啟動 Cassandra 服務器,通過發出 cqlsh 登錄到終端上的cqlshcqlsh ,然后發出以下命令 -

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

現在,當您啟動 spring 啟動應用程序時,它應該可以正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM