无法连接到dockerized卡桑德拉

时间:2018-10-19 13:45:37

标签: spring docker cassandra jhipster

代码抛出以下异常:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraConfiguration': Invocation of init method failed; nested exception is 

 com.datastax.driver.core.exceptions.NoHostAvailableException:
 All host(s) tried for query failed
 (
   tried:

   localhost/127.0.0.1:9042
   (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042]
    Cannot connect),

   localhost/0:0:0:0:0:0:0:1:9042
   (com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042]
    Cannot connect)
 )

我正在使用JHipster example found at GitHub。运行mvnw clean test不会返回任何失败。

端口9042和9160已在防火墙中打开。

依赖性是cassandra-driver-extras,cassandra-driver-mapping和spring-boot-starter-data-cassandra。

1 个答案:

答案 0 :(得分:0)

这是application-dev.yml的重要部分:

spring:
    data:
        cassandra:
            contactPoints: localhost
            protocolVersion: V4
            compression: LZ4
            keyspaceName: demo_ks
            entityPackage: com.demo.domain
            port: 9042

必须使用docker-compose文件,该文件启动容器cassandra:3.11.3,端口9042、9160、7199、7000、7001和卷cassandra_data:/data。稍后,我将其全部放在某个GitHub存储库中,并使用完整的代码编辑此答案。

我的CassandraConfiguration类具有返回CassandraCustomConversionsClusterBuilderCustomizerSession的bean,还自动连接了ClusterMetricRegistry

pom.xml的重要部分:

<properties>
    <spring-boot.version>2.0.5.RELEASE</spring-boot.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.cassandraunit</groupId>
        <artifactId>cassandra-unit-spring</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
    </dependency>

    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-extras</artifactId>
    </dependency>

    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-mapping</artifactId>
    </dependency>
</dependencies>