You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向OpenSearch索引创建文档时遇I/O reactor STOPPED异常求助

OpenSearch 创建文档时抛出"I/O reactor status: STOPPED"异常的解决方法

问题背景

尝试向OpenSearch创建新文档时触发异常,已降级open-search依赖版本匹配课程要求,但问题仍未解决。
预期结果:成功在OpenSearch索引中创建条目/文档

异常栈信息

java.lang.RuntimeException: Request cannot be executed; I/O reactor status: STOPPED

at org.opensearch.client.RestClient.extractAndWrapCause(RestClient.java:915)
at org.opensearch.client.RestClient.performRequest(RestClient.java:301)
at org.opensearch.client.RestClient.performRequest(RestClient.java:289)
at org.opensearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1762)
at org.opensearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1728)
at org.opensearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1696)
at org.opensearch.client.RestHighLevelClient.index(RestHighLevelClient.java:961)
at com.aviorn36.consumer.KafkaOpenSearchConsumer.createDocsInOpenSearch(KafkaOpenSearchConsumer.java:73)
at com.aviorn36.consumer.KafkaOpenSearchConsumer.main(KafkaOpenSearchConsumer.java:54)
Caused by: java.lang.IllegalStateException: Request cannot be executed; I/O reactor status: STOPPED
at org.apache.http.util.Asserts.check(Asserts.java:46)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.ensureRunning(CloseableHttpAsyncClientBase.java:90)
at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:123)
at org.opensearch.client.RestClient.performRequest(RestClient.java:297)
7 more

客户端代码

private static RestHighLevelClient getRestHighLevelClient() {
    String openSearchServerUrl = "http://localhost:9200/";
    URI openSearchUri = URI.create(openSearchServerUrl);
    RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(openSearchUri.getHost(), openSearchUri.getPort(), openSearchUri.getScheme()));
    if(openSearchUri.getUserInfo() != null){
        restClientBuilder.setHttpClientConfigCallback(
                httpAsyncClientBuilder -> {
                        String[] authDetails = openSearchServerUrl.split(":");
                        CredentialsProvider basicCreds = new BasicCredentialsProvider();
                        basicCreds.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(authDetails[1],authDetails[2]));
                        httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCreds).setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy());
                        return httpAsyncClientBuilder;
                    }
                );
    }
    return new RestHighLevelClient(restClientBuilder);
}

问题分析

该异常说明OpenSearch客户端的I/O reactor已停止,常见触发原因:

  • 客户端实例被频繁创建销毁,导致连接池异常终止
  • 认证逻辑错误,触发连接池崩溃
  • 客户端未正确复用,生命周期管理混乱

修复步骤

1. 确保客户端单例复用

不要在每次请求时创建新的RestHighLevelClient实例,频繁启停会导致I/O reactor进入STOPPED状态。应在应用启动时初始化一次客户端,全局复用,直到应用关闭时再执行关闭操作。

2. 修复认证逻辑错误

当前代码通过拆分URL字符串获取认证信息的方式存在缺陷,正确做法是解析URI.getUserInfo():

if(openSearchUri.getUserInfo() != null){
    restClientBuilder.setHttpClientConfigCallback(
            httpAsyncClientBuilder -> {
                    String[] authDetails = openSearchUri.getUserInfo().split(":");
                    if(authDetails.length != 2){
                        throw new IllegalArgumentException("OpenSearch URL中的认证信息格式错误");
                    }
                    CredentialsProvider basicCreds = new BasicCredentialsProvider();
                    basicCreds.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(authDetails[0], authDetails[1]));
                    httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCreds)
                            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
                    return httpAsyncClientBuilder;
                }
            );
}

3. 优雅关闭客户端

在应用生命周期结束时,调用client.close()释放资源,避免连接池泄漏:

public static void main(String[] args) throws IOException {
    RestHighLevelClient client = getRestHighLevelClient();
    try {
        // 执行文档创建等业务逻辑
        createDocsInOpenSearch(client);
    } finally {
        if(client != null){
            client.close();
        }
    }
}

4. 检查服务连通性

确认OpenSearch服务正常运行,端口9200可正常访问,可通过以下命令测试:

curl http://localhost:9200

内容的提问来源于stack exchange,提问作者aviorn36

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:13:16