使用docker-compose创建的es集群,怎么使用Java连接进行查询?

我采用官方文档中说明的,使用了docker-compose搭建了一个es集群。
采用curl 方式访问都没有任何问题,使用elasticsearch-head工具监控也是正常的。
但是使用Java编写代码链接集群出现异常,由于搭建es集群默认使用的是docker-compose中指定的网络链接方式,IP也是docker-compose自动分配的。与当前机器都不再同一个网段内,因此不知道怎么访问这个docker搭建的es集群? 亲们有谁知道麻烦告知一下。 先谢谢啦。

官方文档的地址 :https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
docker-compose.yml 设置:

version: '2'
services:
  es_balance:
    image: elasticsearch_master:2.0
    container_name: es_balance
    environment:
      - cluster.name=elasticsearch-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - node.master=false 
      - node.data=false 
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    mem_limit: 1g
    cap_add:
      - IPC_LOCK
    ports:
      - 9200:9200
    networks:
      - esnet
  es_cluster:
    image: elasticsearch_slave:2.0
    environment:
      - cluster.name=elasticsearch-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=es_balance"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    mem_limit: 1g
    cap_add:
      - IPC_LOCK
    networks:
      - esnet
networks:
  esnet:
    driver: bridge

访问 http://local:9200/_cat/nodes?v
https://cloud.githubusercontent.com/assets/20079928/23781270/0dba057c-0587-11e7-944f-2897be43b6dd.png

访问http://local:9200/_cat/indices?v

JavaCode:

String masterHost = "172.18.0.3";
String bakHost = "172.18.0.2";
// on startup
Settings settings = Settings.builder().put("cluster.name", "elasticsearch-cluster").build();

TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(masterHost), 9300));
//.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(bakHost), 9300));
//Get API
GetResponse response = client.prepareGet("face", "summary", "AVqxwjBi2zdNu9G4HdIc").get();
Map<String, Object> source = response.getSource();
source.forEach((k,v)->System.out.println("------"+k));

Exception:

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{BrxMkowhS9S9dZwmZcaEWg}{bigdata4.gds.com}{192.168.0.204:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:344)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:242)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:356)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:403)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62)
at gds.bigdata.elk.TestAPIDemo.main(TestAPIDemo.java:26)

docker-compose.yml中使用的镜像都是从官方下载下来的,我重命名过。
有知道怎么设置的亲们,帮忙给看看原因吧,谢谢啦~

最终我们采用jest通过9200端口操作es集群。 不知道这么做是不是合适~

你的配置文件里面貌似没有把9300也映射出来,所以连接失败,用http协议连接集群也是OK的。

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.