无法使用弹簧数据卡桑德拉连接到从码头工人容器卡桑德拉:NoHostAvailableExceptio

2019-09-30 11:00发布

我使用的spring-data-cassandra连接到我们的卡桑德拉分贝这是在aws cluster 。 使用CassandraTemplate我能够在本地连接并获取数据,但在部署应用docker container我得到以下错误

UnsatisfiedDependencyException: Error creating bean with name 'cassandraTemplate' defined in class path resource 
[org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: 
Unsatisfied dependency expressed through method 'cassandraTemplate' parameter 0; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'session' defined in class path resource [utils/CassandraConfig.class]: 
Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed 
(tried: /IP:9042 (com.datastax.driver.core.TransportException: [/IP:9042] Error writing))

这里是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mus-cassadra-read</artifactId>
        <groupId>com.rogs.mus</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rogs.mus</groupId>
    <artifactId>external-endpoints</artifactId>

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
        <version>1.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.rogs.mus</groupId>
        <artifactId>model-cassadra-read</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
        <version>1.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

    <!-- cassandra dependency  -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
        <version>1.4.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.6.Final</version>
    </dependency>
    </dependencies>
</project>

CassandraConfig.java

@Configuration
@EnableCassandraRepositories(basePackages = { "org.spring.cassandra.example.repo" })
public class CassandraConfig {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @Value("${cassandra.hostnames}")
    private String hostnames ;
    @Value("${cassandra.username}")
    private String username ;
    @Value("${cassandra.password}")
    private String password ;
    @Value("${cassandra.keyspace}")
    private String keyspace ;
    @Value("${cassandra.isSsl}")
    private boolean isSsl ;
    @Value("${cassandra.port}")
    private int port;

    @Bean
    Public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        PlainTextAuthProvider authProvider = new PlainTextAuthProvider(username, password);
        cluster.setContactPoints(hostnames);
        cluster.setPort(port);
        cluster.setUsername(username);
        cluster.setPassword(password);
        return cluster;
    }

    @Bean
    Public CassandraMappingContext mappingContext() {
        return new BasicCassandraMappingContext();
    }

    @Bean
    Public CassandraConverter converter() {
        return new MappingCassandraConverter(mappingContext());
    }

    @Bean
    Public CassandraSessionFactoryBean session() throws Exception {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(keyspace);
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);
    return session;
    }

    @Bean
    Public CassandraOperations cassandraTemplate() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

}

请让我知道如果我在这里做什么错事。

文章来源: Unable to connect to cassandra from Docker container using spring-data-cassandra: NoHostAvailableException: All host(s) tried for query failed