无法使用spring-data-cassandra从Docker容器连接到cassandra:NoHostAvailableException:尝试查询的所有主机都失败

时间:2016-12-12 19:23:42

标签: docker cassandra spring-data-cassandra

我使用spring-data-cassandra连接到aws cluster中的cassandra数据库。使用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());
    }

}

如果我在这里做错了,请告诉我。

0 个答案:

没有答案