【问题标题】:Failed to connect to Dockerized elasticsearch via java-client无法通过 java-client 连接到 Dockerized elasticsearch
【发布时间】:2015-07-25 17:13:25
【问题描述】:

我使用 OFFICIAL REPO elasticsearch docker 镜像设置了一个 elasticsearch 容器。然后运行它

docker run -dP elasticsearch

简单而有效。 ps信息是

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                              NAMES
658b49ed9551        elasticsearch:latest   "/docker-entrypoint.   2 seconds ago       Up 1 seconds        0.0.0.0:32769->9200/tcp, 0.0.0.0:32768->9300/tcp   suspicious_albattani  

我可以通过端口 32769->9200 使用 http-client 访问服务器

baihetekiMacBook-Pro:0 baihe$ curl 10.211.55.100:32769
{
  "status" : 200,
  "name" : "Scorpia",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.5",
    "build_hash" : "2aaf797f2a571dcb779a3b61180afe8390ab61f9",
    "build_timestamp" : "2015-04-27T08:06:06Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

现在我需要我的 JAVA 程序来使用 dockerized elasticsearch。 java Node客户端只能通过32768->9300(集群节点通话端口)连接elasticsearch。所以我像这样在我的java中配置传输客户端

    Settings settings = ImmutableSettings.settingsBuilder()
            .put("client.transport.sniff", true)
            .put("client.transport.ignore_cluster_name", true).build();
    client = new TransportClient(settings);
    ((TransportClient) client)
    .addTransportAddress(new InetSocketTransportAddress(
            "10.211.55.100", 32768));

然后我在控制台中收到以下错误:

Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
    at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:86)
    at org.elasticsearch.client.support.AbstractIndicesAdminClient.exists(AbstractIndicesAdminClient.java:170)
    at org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder.doExecute(IndicesExistsRequestBuilder.java:53)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
    at cct.bigdata.yellowbook.service.impl.ResourceServiceImpl.<init>(ResourceServiceImpl.java:49)
    at cct.bigdata.yellowbook.config.YellowBookConfig.resourceService(YellowBookConfig.java:21)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e.CGLIB$resourceService$0(<generated>)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e$$FastClassBySpringCGLIB$$72e3e213.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at cct.bigdata.yellowbook.config.YellowBookConfig$$EnhancerBySpringCGLIB$$e7d2ff3e.resourceService(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 31 common frames omitted

当我直接在主机中运行弹性搜索时。一切都好。

我在 docker hub 上查看了所有 elasticsearch 的 dockerfile。似乎他们都只是做以下事情:

EXPOSE 9200 9300

我想知道有没有人试图做类似的事情。 9300是普通的TCP端口还是UDP端口?运行容器时我需要做一些特殊的事情吗?谢谢!

【问题讨论】:

    标签: java elasticsearch docker


    【解决方案1】:

    如果您将"client.transport.sniff" 设置为false,它应该可以工作。

    如果您仍想使用嗅探,请按照以下说明操作: https://github.com/olivere/elastic/wiki/Docker

    在这里详细讨论:https://github.com/olivere/elastic/issues/57#issuecomment-88697714

    【讨论】:

      【解决方案2】:

      这对我有用(在 docker-compose.yml 中)。

      version: "2"
      services:
          elasticsearch5:
              image: docker.elastic.co/elasticsearch/elasticsearch:5.5.3
              container_name: elasticsearch5
              environment:
                  - cluster.name=elasticsearch5-cluster
                  - http.host=0.0.0.0
                  - network.publish_host=127.0.0.1
                  - transport.tcp.port=9700
                  - discovery.type=single-node
                  - xpack.security.enabled=false
              ports:
                  - "9600:9200"
                  - "9700:9700"
      

      指定 network.publish_host 和 transport.tcp.port 似乎可以解决问题。 sniff=true 仍然有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-16
        • 2019-03-24
        • 2019-11-05
        • 2016-03-10
        • 1970-01-01
        • 2018-06-10
        • 2021-09-14
        • 1970-01-01
        相关资源
        最近更新 更多