cassandra+springboot,java.lang.classnotfoundexception:com.datastax.oss.protocol.internal.segmentcodec

aor9mmx1  于 2021-06-09  发布在  Cassandra
关注(0)|答案(1)|浏览(699)

我正在尝试使用cassandradb创建一个spring应用程序。但是当我尝试运行应用程序时出现以下错误。

Caused by: java.lang.NoClassDefFoundError: com/datastax/oss/protocol/internal/SegmentCodec
    ... 103 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.datastax.oss.protocol.internal.SegmentCodec
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_152-release]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152-release]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_152-release]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152-release]
    ... 103 common frames omitted

spring boot版本:2.3.4.0版本
cassandra(spring boot starter data cassandra)版本:3.0.4
application.yml文件:

spring:

  data:
    cassandra:
      keyspaceName: my_keyspace
      contactPoints: localhost
      port: 9042
      schema-action: CREATE_IF_NOT_EXISTS
      local-datacenter: datacenter1

cassandraconfig.java文件

@Configuration
@EnableCassandraRepositories(basePackages = "com.myproject.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${spring.data.cassandra.keyspaceName}")
    private String keyspace;

    @Value("${spring.data.cassandra.contactPoints}")
    private String contactPoints;

    @Value("${spring.data.cassandra.port}")
    private int port;

    @Value("${spring.data.cassandra.local-datacenter}")
    private String dataCenter;

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }

    @Override
    protected SessionBuilderConfigurer getSessionBuilderConfigurer() {
        return new SessionBuilderConfigurer() {
            @Override
            public CqlSessionBuilder configure(CqlSessionBuilder cqlSessionBuilder) {

                return cqlSessionBuilder
                        .addContactPoint(new InetSocketAddress(
                                contactPoints,
                                getPort()))
                        ;

            }
        };
    }

    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        return Arrays.asList(CreateKeyspaceSpecification
                .createKeyspace(getKeyspaceName())
                .ifNotExists(true)
                .withSimpleReplication()
                .with(KeyspaceOption.DURABLE_WRITES));

    }

    @Override
    protected KeyspacePopulator keyspacePopulator() {
        ResourceKeyspacePopulator keyspacePopulate = new ResourceKeyspacePopulator();
        keyspacePopulate.setSeparator(";");
        keyspacePopulate.setScripts(new ClassPathResource("table-schema.cql"));
        return keyspacePopulate;
    }

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

    @Override
    protected int getPort(){
        return port;
    }

    @Override
    public String[] getEntityBasePackages() {
        return new String[]{"com.myproject.entity"};
    }

    @Override
    protected String getLocalDataCenter() {
        return dataCenter;
    }

}

表-schema.cql

CREATE TABLE IF NOT EXISTS orders (order_id text, user_name text, created_at timestamp, item_id text, PRIMARY KEY ((order_id), item_id));

pom.xml中存在以下依赖项(driver.version为4.9.0)

<dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-core</artifactId>
            <version>${driver.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-query-builder</artifactId>
            <version>${driver.version}</version>
        </dependency>
        <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-mapper-runtime</artifactId>
            <version>${driver.version}</version>
        </dependency>
tjjdgumg

tjjdgumg1#

尝试添加此依赖项以获取最新的本机协议版本:

<!-- https://mvnrepository.com/artifact/com.datastax.oss/native-protocol -->
    <dependency>
        <groupId>com.datastax.oss</groupId>
        <artifactId>native-protocol</artifactId>
        <version>1.4.11</version>
    </dependency>

springboot似乎链接到一个旧的协议版本,这导致了这个异常。

相关问题