Spring 使用 ReactiveElasticsearchClient 的应用程序无限期卡住,即使 Elasticsearch 服务器是 运行

Spring application using ReactiveElasticsearchClient is stuck indefinitely even though Elasticsearch server is running

我一直在尝试使用 ReactiveElasticsearchClient 在我的 spring 应用程序中索引一些文档。当我尝试使用命令 java -jar reactive-es-1.0-SNAPSHOT.jar application.ReactiveEsApplication

运行 jar 时,我的应用程序无限期地卡住了

这是 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>reactive-es</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>application.ReactiveEsApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


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


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>4.1.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <version>5.3.6</version>
        </dependency>

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

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>1.0.6</version>
        </dependency>
    </dependencies>

</project>

这是 ESClientConfiguration class

package application.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
import org.springframework.stereotype.Component;

@Component
public class ESClientConfiguration {

    @Value("${es.host}")
    private String host;

    @Bean
    public ReactiveElasticsearchClient getReactiveClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(host)
                .build();
        return ReactiveRestClients.create(clientConfiguration);
    }
}

这里是 ReactiveEsApplication class

package application;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;

import java.util.HashMap;

@Slf4j
@SpringBootApplication
public class ReactiveEsApplication implements CommandLineRunner {
    private final ReactiveElasticsearchClient reactiveElasticsearchClient;

    @Autowired
    public ReactiveEsApplication(ReactiveElasticsearchClient reactiveElasticsearchClient) {
        this.reactiveElasticsearchClient = reactiveElasticsearchClient;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReactiveEsApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("============Started running============");
        try {
            System.out.println("============Started indexing============");
            insertDocument("my-index");
        } catch (Exception e) {
            log.error("Error!", e);
        }
    }

    public void insertDocument(final String indexName) {

        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(getDocument(), XContentType.JSON);
        reactiveElasticsearchClient
                .index(indexRequest)
                .subscribe(System.out::println, System.out::println, () -> System.out.println("completed"));
    }

    private HashMap<String, String> getDocument() {
        return new HashMap<String, String>() {{
            put("name", "maria");
            put("age", "29");
            put("experience", "7");
        }};
    }
}

这里是application.properties

es.host=10.47.20.112:9200

当我 运行 命令 java -jar reactive-es-1.0-SNAPSHOT.jar application.ReactiveEsApplication 时,应用程序卡住了,这是控制台日志


  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.5.RELEASE)

2021-04-30 17:51:33.244  INFO 16868 --- [           main] a.ReactiveEsApplication                  : Starting ReactiveEsApplication v1.0-SNAPSHOT on ip-172-31-46-44 with PID 16868 (/home/user/reactive-es-1.0-SNAPSHOT.jar started by user in /home/user)
2021-04-30 17:51:33.249  INFO 16868 --- [           main] a.ReactiveEsApplication                  : No active profile set, falling back to default profiles: default
2021-04-30 17:51:33.838  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2021-04-30 17:51:33.855  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 Elasticsearch repository interfaces.
2021-04-30 17:51:33.860  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2021-04-30 17:51:33.862  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1ms. Found 0 Reactive Elasticsearch repository interfaces.
2021-04-30 17:51:34.808  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.geo.Point to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.geo.Point as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface org.springframework.data.elasticsearch.core.geo.GeoJson to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to interface org.springframework.data.elasticsearch.core.geo.GeoJson as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonLineString to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonLineString as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonPolygon to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonPolygon as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPolygon to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPolygon as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Spring Data Elasticsearch: 4.1.8
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Elasticsearch Client in build: 7.9.3
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Elasticsearch Client used: 7.6.2
2021-04-30 17:51:34.884  WARN 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version mismatch in between Elasticsearch Clients build/use: 7.9.3 - 7.6.2
2021-04-30 17:51:35.476  INFO 16868 --- [           main] a.ReactiveEsApplication                  : Started ReactiveEsApplication in 2.679 seconds (JVM running for 3.448)
============Started running============
============Started indexing============

代码没有超出此范围并卡在此处。

我确认elasticsearch服务器是否在10.47.20.112上运行;它正在工作。这是对 http://10.47.20.112:9200/my-index/_search?pretty=true&q=*:*

的回复
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "JXBYInkBorO7pC2lkzAR",
        "_score" : 1.0,
        "_source" : {
          "age" : 32,
          "experience" : 10,
          "name" : "john"
        }
      }
    ]
  }
}

问题是您混合了不匹配且无法协同工作的库版本。

您将父级定义为 Spring Boot 2.3.5,该版本使用 Spring 5.2,但稍后将 webflux 版本定义为 5.3.6。 Spring Data Elasticsearch 4.1.8 版也是使用 Spring 5.3.2 构建的。我没有详细检查 recactor 和 netty 版本,但也可能存在一些冲突 - 似乎在你的 pom 中你用 Spring Boot 2.4.5 中的版本定义了这些依赖关系,但保留了父版本在 2.3.5.

您的应用程序使用以下 pom 运行(我将父级设置为 2.4.5 并删除了不需要的显式依赖项):

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>reactive-es</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>application.ReactiveEsApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>

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

    </dependencies>

</project>