Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NEST访问Elasticsearch Docker实例_Docker_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Docker Compose_Nest - Fatal编程技术网 elasticsearch,docker-compose,nest,Docker,elasticsearch,Docker Compose,Nest" /> elasticsearch,docker-compose,nest,Docker,elasticsearch,Docker Compose,Nest" />

使用NEST访问Elasticsearch Docker实例

使用NEST访问Elasticsearch Docker实例,docker,elasticsearch,docker-compose,nest,Docker,elasticsearch,Docker Compose,Nest,我使用Docker Compose运行了一个简单的Elasticsearch实例: --- version: '2' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1 hostname: elasticsearch environment: - cluster.name=docker-cluster - bootstrap.me

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

---
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实例通信

来自Fiddler:

  • 它返回一个json,其中包含以下行:
    “host”:“172.18.0.4”
  • 然后它尝试使用这个IP地址,我的请求失败,因为它无法解析那个IP地址
  • 为了能够从C#应用程序成功连接到Elasticsearch实例,我应该做哪些更改


    嵌套版本:5.5.0

    我可以通过在我的设置中将
    SniffingConnectionPool
    更改为
    StaticConnectionPool
    来解决它。

    我可以通过在我的设置中将
    SniffingConnectionPool
    更改为
    StaticConnectionPool
    来解决它。

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

    SniffingConnectionPool
    将在连接池中播种时使用。这意味着客户端必须可以访问http发布地址。如果未明确设置,它将使用
    http.host
    中的值,如果未设置,将使用
    网络.host
    ,该地址将是专用网络上的地址

    使用docker compose配置,如

    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:9200
    es02
    localhost:9201
    。我们本可以指定
    es02
    在9200上的容器中运行,并将其映射到9201的主机端口,但这样做的问题是
    es02
    的http.publish\u地址仍然是
    127.0.0.1:9200
    ,这是
    嗅探连接池
    在设定节点种子时将最终使用的。为了避免这种情况,我们在与
    es01
    不同的端口上运行
    es02
    ,因此http发布地址将不同

    使用上述配置,
    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
    将起作用

    private static void Main()
    {
    var defaultIndex=“posts”;
    var uris=new[]
    {
    新Uri(“http://localhost:9200"),
    新Uri(“http://localhost:9201")
    };
    var pool=新的嗅探连接池(URI);
    var设置=新连接设置(池)
    .DefaultIndex(默认索引);
    var客户端=新的ElasticClient(设置);
    var response=client.Nodes.Info();
    foreach(响应中的var节点.Nodes)
    {
    WriteLine($“{node.Key}http发布地址为:{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 7.1.0和Elasticsearch 7.2.0,但基本概念相同)。

    SniffingConnectionPool
    将在连接池中播种时使用。这意味着客户端必须可以访问http发布地址。如果未明确设置,它将使用
    http.host
    中的值,如果未设置,将使用
    网络.host
    ,该地址将是专用网络上的地址

    使用docker compose配置,如

    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:9200
    es02
    localhost:9201
    。我们本可以指定
    es02
    在9200上的容器中运行,并将其映射到9201的主机端口,但这样做的问题是
    es02
    的http.publish\u地址仍然是
    127.0.0.1:9200
    ,这是
    嗅探连接池
    在设定节点种子时将最终使用的。为了避免这种情况,我们在与
    es01
    不同的端口上运行
    es02
    ,因此http发布地址将不同

    使用上述配置,
    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
    将起作用

    private static void Main()
    {
    var defaultIndex=“posts”;
    var uris=new[]
    {
    新Uri(“http://localhost:9200"),
    新Uri(“http://localhost:9201")
    };
    var pool=新的嗅探连接池(URI);
    var设置=新连接设置(池)
    .DefaultIndex(默认索引);
    var客户端=新的ElasticClient(设置);
    var response=client.Nodes.Info();
    foreach(响应中的var节点.Nodes)
    {
    WriteLine($“{node.Key}http发布地址为:{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
    

    很好,您已将其排序。
    SniffingConnectionPool
    使用
    publish\u地址
    ,或第一个
    bound\u地址
    (如果没有
    publish\u地址
    )嗅探集群中的节点,以确定它可以向哪些节点发送请求。是的。当我询问uestion然后我记得我可以使用不止一个连接池。查看文档并找到了您所说的内容。感谢您的澄清!谢谢@EvaldasBuinauskas,我整个上午都在诅咒docker和NEST。很好,您已经对其进行了排序。
    嗅探连接池
    嗅探集群中的节点以确定使用
    发布地址
    ,或第一个
    绑定地址
    (如果没有
    发布地址
    )作为向群集发出请求的地址,它可以向哪些节点发送请求:。是的。当我问这个问题时,我记得有多个连接池可以连接