spring Sping Boot 应用程序返回“Error creating bean with name 'cassandraSession'”和NoHostAvailableException

v09wglhw  于 2023-04-04  发布在  Spring
关注(0)|答案(2)|浏览(232)

我正在写一个Springboot项目来实现使用cassandra dbISO20022
我的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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>untitled</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Simple Wallet</name>
    <description>Powered with ISO20022</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
        </dependency>

        <!-- API, java.xml.bind module -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>

        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>

</project>

我的Cassandra配置:

@Configuration
@EnableReactiveCassandraRepositories(basePackages = "org.example.repository")
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {

    @Override
    protected String getKeyspaceName() {
        return "isodb";
    }

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = super.cluster();
        cluster.setContactPoints("127.0.0.1");
        cluster.setPort(9142);
        return cluster;
    }

    @Bean
    public CassandraMappingContext cassandraMapping() {
        return new CassandraMappingContext();
    }

    @Override
    protected boolean getMetricsEnabled() { return false; }

}

appliction.properties:

spring.data.cassandra.keyspace-name=isodb
spring.data.cassandra.schema-action=create_if_not_exists

运行cassandra的nodetool状态的输出:

Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens  Owns (effective)  Host ID                               Rack
UN  127.0.0.1  69.07 KiB  16      100.0%            34bb5a10-bd19-46ea-8ece-a68b239becf4  rack1

当我运行这个项目时,我得到这个错误:

org.springframework.beans.factory.BeanCreationException: \
  Error creating bean with name 'cassandraSession' defined in class path resource \
 [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: \
    Invocation of init method failed; nested exception is \
    com.datastax.driver.core.exceptions.NoHostAvailableException: \ 
    All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042 \
    (com.datastax.driver.core.exceptions.TransportException: \
    [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042 \
    (com.datastax.driver.core.exceptions.TransportException: \
    [localhost/127.0.0.1:9042] Cannot connect))

我该怎么弥补我错过了什么

uubf1zoe

uubf1zoe1#

看到这个:

Cannot connect), localhost/127.0.0.1:9042

还有这个

cluster.setContactPoints("127.0.0.1");
cluster.setPort(9142);

不确定Cassandra希望连接到哪个端口,但9142只用于〉= 4.0版本的SSL。否则,它是9042。此外,我不会在代码中指定端口或联系人点,而是在src/main/resources/application.[yaml|properties]文件中指定。这些设置可能会发生冲突。

<java.version>1.8</java.version>

此外,Cassandra JVM的Java版本不必与服务层代码中使用的JVM版本相匹配。我强烈建议至少使用Java 17。
接下来,我看到的一件大事是spring-boot-starter-parent版本已经有5年的历史了:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Sping Boot 版本3与2.x不兼容,所以现在我建议使用2.7.10:

<version>2.7.10</version>

我猜您也必须对spring-boot-autoconfigure进行相同的版本调整。
正确地设置这个父版本应该会带来正确的驱动程序来与Cassandra 4.0.8一起工作。

编辑

所以我认为你不需要CassandraConfig类,只要你构建你的仓库,让它们扩展ReactiveCassandraRepository,并在application.properties文件中定义连接属性,它就应该工作。
Baeldung有一篇文章介绍了如何使用Sping Boot Cassandra Reactive库,我建议您看一下:https://www.baeldung.com/spring-data-cassandra-reactive

5t7ly7z5

5t7ly7z52#

TransportException是Java驱动程序抛出的一个非常具体的错误,这意味着它无法到达集群。这清楚地表明您的应用无法连接到CQL端口9042上的IP 127.0.0.1上的集群。
需要注意的是,如果您的应用程序与Cassandra节点不在同一台主机上运行,它将无法连接。根据定义,localhost只能在本地主机上访问。
否则,您需要调查为什么您的应用和集群之间没有网络连接。一个好的故障排除方法是运行lsofnetstat等Linux实用程序,以验证Cassandra示例确实(1)在localhost上运行,并且(2)在端口9042上侦听。
运行以下命令之一并查看输出,以验证Cassandra进程的IP +端口:

$ sudo lsof -nPi -sTCP:LISTEN
$ netstat -tnlp

相关问题