Unable to connect to cassandra from Docker contain

2019-07-27 13:48发布

I am using spring-data-cassandra to connect to our cassandra db which is in aws cluster. Using CassandraTemplate I am able to connect locally and get the data but while deploying application in docker container I am getting below error

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

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

}

Please let me know if I am doing anything wrong here.

0条回答
登录 后发表回答