无法使用CassandraConfiguration连接到cassandra

时间:2019-07-12 07:43:58

标签: java spring-boot cassandra

我已遵循CassandraOperations将记录插入到cassandra中。由于我需要在一行中插入TTL值,因此我选择了这种方式。 CrudRespository无法具有存储TTL值的功能。

下面是Cassandra Config类

@Override
@Bean
public CassandraClusterFactoryBean cluster() {
    final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(cassandra_contactPoints);
    cluster.setPort(Integer.parseInt(cassandra_port));
    cluster.setUsername(cassandra_username);
    cluster.setPassword(cassandra_password);
    LOGGER.info("Cluster created with contact points [" + cassandra_port + "] " + "& port ["
            + Integer.parseInt(cassandra_port) + "].");
    return cluster;
}

@Override
@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
    return new BasicCassandraMappingContext();
}

@Override
protected String getKeyspaceName() {
    return cassandra_keyspaceName;
}

启动应用程序时,它将引发以下异常

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.core.CassandraAdminTemplate]: Factory method 'cassandraTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/sample/session/config/CassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/sample/session/config/CassandraConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/codahale/metrics/JmxReporter 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/sample/session/config/CassandraConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/codahale/metrics/JmxReporter
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/sample/session/config/CassandraConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/codahale/metrics/JmxReporter
Caused by: java.lang.ClassNotFoundException: com.codahale.metrics.JmxReporter

1 个答案:

答案 0 :(得分:2)

这在Java driver's documentation中有记录-您要么需要创建一个不包含JMX度量标准报告的集群对象-这可以通过将调用.withoutJMXReporting()添加到集群生成器中来完成,或者添加Maven依赖项:

<dependency>
  <groupId>io.dropwizard.metrics</groupId>
  <artifactId>metrics-jmx</artifactId>
  <version>4.0.2</version>
</dependency>