在 .NET Core 6.0 Docker 应用程序中连接到 Elasticsearch 时出现“无法分配请求的地址”错误

问题描述 投票:0回答:0

我目前正在开发 .NET Core 6.0,以利用

Elasticsearch
进行索引和搜索功能。该应用程序与 Elasticsearch 和 SQL Server 一起在 Docker 容器内运行。但是,我在尝试从 .NET Core 应用程序连接到 Elasticsearch 时遇到了问题。

我有一个

Docker Compose
文件,用于为我的应用程序设置服务,包括 Elasticsearch。 Elasticsearch 服务是使用必要的环境变量和卷定义的。我的
appsettings.json
配置文件中 Elasticsearch 的连接 URI 设置为“http://localhost:9200”。我还实现了一个自定义 ElasticsearchClient 类来处理 Elasticsearch 连接。

当我运行应用程序时,它抛出以下异常:

从不成功的 () 低级别调用构建的无效 NEST 响应 得到: /_cluster/health?wait_for_nodes=1&wait_for_active_shards=1&timeout=50s

此 API 调用的审计跟踪:

  • 1 ProductCheckOnStartup:拍摄:00:00:00.0870458
  • [2] ProductCheckFailure:节点:http://localhost:9200/ 获取:00:00:00.0777851

OriginalException:Elasticsearch.Net.ElasticsearchClientException:客户端无法验证服务器是否为 Elasticsearch,因为

产品检查电话不成功。有些功能可能没有 如果服务器运行的是不受支持的产品,则兼容。称呼: 未知状态代码:GET / ---> Elasticsearch.Net.PipelineException:客户端无法验证 由于产品检查不成功,服务器是 Elasticsearch 称呼。如果服务器不兼容,某些功能可能不兼容 运行不受支持的产品。 ---> System.Net.Http.HttpRequestException:无法分配请求的地址 (localhost:9200) ---> System.Net.Sockets.SocketException (99): 不能 分配请求的地址 System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError 错误,CancellationToken 取消令牌)...

我认为该问题与连接设置或我尝试从 Docker 容器内部访问 Elasticsearch 的方式有关。我已经检查过以下内容:

Elasticsearch 服务已启动并正在运行,并且在 Docker Compose 文件中正确设置了端口映射。

有人可以帮助我确定此问题的原因并提供有关如何从我的 .NET Core 6.0 Docker 应用程序正确连接到 Elasticsearch 的指导吗?

以下是我的 Docker Compose 文件的相关部分:

version: '3.4'

services:
# ... hidden because too long ...

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.9.0
    container_name: elasticsearch
    environment:
      - xpack.security.enabled=false
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    restart: always
    # depends_on:
    #   - book-store-db
    ports:
      - "9200:9200"
    networks:
      - elastic

# ... Rest of the Docker Compose file ...

volumes:
  # mssql-data:
  #   driver: local
  elasticsearch-data:
    driver: local

networks:
  elastic:
    driver: bridge

这是我的 appsettings.json 配置的相关部分:

{
  "Application": {
    "Elasticsearch": {
      "Uri": "http://elasticsearch:9200",
      "IndexName": "documents"
    },
    "Indexer": {
      "IndexDelay": 10
    }
  },

  // ... Other configurations ...
}

创建并初始化弹性搜索集群的代码。

public class ElasticsearchClient
{
    private readonly ILogger<ElasticsearchClient> logger;
    private readonly IElasticClient client;
    private readonly string indexName;

    public ElasticsearchClient(ILogger<ElasticsearchClient> logger, IOptions<ElasticsearchOptions> options)
        : this(logger, CreateClient(options.Value.Uri), options.Value.IndexName)
    {

    }

    public ElasticsearchClient(ILogger<ElasticsearchClient> logger, IElasticClient client, string indexName)
    {
        this.logger = logger;
        this.indexName = indexName;
        this.client = client;
    }
    private static IElasticClient CreateClient(string uriString)
    {
        var connectionUri = new Uri(uriString);
        var connectionPool = new SingleNodeConnectionPool(connectionUri);
        var connectionSettings = new ConnectionSettings(connectionPool);

        return new ElasticClient(connectionSettings);
    }
}

任何有关解决此问题的帮助或见解将不胜感激。预先感谢您的时间和帮助!

c# elasticsearch docker-compose nest
© www.soinside.com 2019 - 2024. All rights reserved.