使用NEST访问Elasticsearch Docker实例 [英] Accessing Elasticsearch Docker instance using NEST

查看:125
本文介绍了使用NEST访问Elasticsearch Docker实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Docker Compose运行一个简单的Elasticsearch实例:

I run a simple Elasticsearch instance using Docker Compose:

---
version: '2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1
    hostname: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
    ports:
      - 9200:9200

  kibana:
    image: docker.elastic.co/kibana/kibana:6.1.1
    environment:
      SERVER_NAME: "0.0.0.0"
      ELASTICSEARCH_URL: http://elasticsearch:9200
    ports:
      - 5601:5601

我可以使用localhost从浏览器访问它,但是当我运行我的应用程序并连接到它时,遇到了一些问题.从我能够跟踪到的内容来看,应用程序似乎成功连接到Elasticsearch实例,然后解析了它绑定到的IP,然后使用该IP地址与Elasticsearch实例进行通信.

I can access it from browser using localhost, however when I run my application and connect to it, I'm experiencing some issues. From what I was able to track it seems that application successfully connects to Elasticsearch instance, then resolves IP it is bound to and then uses that IP address to communicate with Elasticsearch instance.

来自Fiddler:

  1. http://10.0.75.2:9200/_nodes/http ,设置?flat_settings& timeout = 2s
  2. 它返回具有以下行的json:"host": "172.18.0.4"
  3. 然后它尝试使用该IP地址,但由于无法解析该IP地址,我的请求失败了
  1. http://10.0.75.2:9200/_nodes/http,settings?flat_settings&timeout=2s
  2. It returns a json that has the following line: "host": "172.18.0.4"
  3. Then it tries to use this IP address and my requests fail because it cannot resolve that IP address

为了能够从C#应用程序成功连接到我的Elasticsearch实例,我应该改变什么?

What should I change in order to be able to successfully connect to my Elasticsearch instance from C# application?

NEST版本:5.5.0

NEST version: 5.5.0

推荐答案

(注意:此答案使用NEST 7.1.0和Elasticsearch 7.2.0,但基本概念相同).

SniffingConnectionPool将使用 <节点的c2> 植入连接池中时.这意味着客户端必须可以访问http发布地址.如果未明确设置,它将使用http.host中的值,如果未设置,将使用network.host,它将是专用网络上的地址.

SniffingConnectionPool will use the http.publish_address of the node when seeded in the connection pool. This means that the http publish address must be reachable by the client. If it's not explicitly set, it'll use the value from http.host, which if not set, will use the network.host, which will be the address on the private network.

使用docker之类的配置

With a docker compose configuration like

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    container_name: es01
    environment:
      - node.name=es01
      - discovery.seed_hosts=es02
      - cluster.initial_master_nodes=es01,es02
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "http.port=9200"
      - "http.publish_host=_local_"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
    container_name: es02
    environment:
      - node.name=es02
      - discovery.seed_hosts=es01
      - cluster.initial_master_nodes=es01,es02
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "http.port=9201"
      - "http.publish_host=_local_"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata02:/usr/share/elasticsearch/data
    ports:
      - 9201:9201
    networks:
      - esnet

volumes:
  esdata01:
    driver: local
  esdata02:
    driver: local

networks:
  esnet:

es01节点映射到localhost:9200es02映射到localhost:9201.我们本来可以指定es02在9200上的容器中运行,并将其映射到9201的主机端口,但是这样做的问题是es02的http.publish_address仍然是127.0.0.1:9200,这SniffingConnectionPool最终将在为节点设定种子时使用.为避免这种情况,我们在与es01不同的端口上运行es02,以使http发布地址不同.

es01 node is mapped to localhost:9200 and es02 to localhost:9201. We could have just specified that es02 runs in the container on 9200, and mapped this to the host port of 9201, but the problem with doing this is that es02's http.publish_address would still be 127.0.0.1:9200, which is what the SniffingConnectionPool will end up using when seeding the node. To avoid this, we run es02 on a different port to es01, so that the http publish addresses will be different.

使用上述配置,http://localhost:9200/_nodes?filter_path=nodes.*.http返回

{
  "nodes": {
    "CSWncVnxS1esOm1KQtOR3A": {
      "http": {
        "bound_address": ["0.0.0.0:9200"],
        "publish_address": "127.0.0.1:9200",
        "max_content_length_in_bytes": 104857600
      }
    },
    "rOAp0T57TgSI_zU1L-T-vw": {
      "http": {
        "bound_address": ["0.0.0.0:9201"],
        "publish_address": "127.0.0.1:9201",
        "max_content_length_in_bytes": 104857600
      }
    }
  }
}

(如果尝试此操作,节点名称将有所不同).现在,SniffingConnectionPool将起作用

(node names will be different if you try this). Now, SniffingConnectionPool will work

private static void Main()
{
    var defaultIndex = "posts";
    var uris = new[]
    {
        new Uri("http://localhost:9200"),
        new Uri("http://localhost:9201")
    };

    var pool = new SniffingConnectionPool(uris);

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    var response = client.Nodes.Info();

    foreach (var node in response.Nodes)
    {
        Console.WriteLine($"{node.Key} http publish_address is: {node.Value.Http.PublishAddress}");
    }
}

打印

CSWncVnxS1esOm1KQtOR3A http publish_address is: 127.0.0.1:9200
rOAp0T57TgSI_zU1L-T-vw http publish_address is: 127.0.0.1:9201

这篇关于使用NEST访问Elasticsearch Docker实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆