通过Java / Spring Boot连接到Docker Elasticsearch实例

时间:2017-09-01 08:32:44

标签: java elasticsearch docker spring-boot docker-compose

我正在从Docker运行Elasticsearch实例。图像来自jHipster docker hub repo:jhipster/jhipster-elasticsearch/ - 我正在使用图像v1.3.2,因为我需要Elasticsearch 2.4.0(与项目的Spring Boot版本一致)。

我正在使用docker-compose启动ES容器以及Logstash和Kibana图像。这是启动ES容器的设置:

jhipster-elasticsearch:
    image: jhipster/jhipster-elasticsearch:v1.3.2
    ports:
        - 9400:9200
        - 9500:9300
    volumes:
       - ./log-es-config/elasticsearch_custom.yml:/usr/share/elasticsearch/config/elasticsearch.yml

所以我使用9400用于REST,9500用于传输通信。

这是elasticsearch_custom.yml中安装到ES config:

的配置
cluster.name: "log-cluster"
node.name: "log-node"
http.host: 0.0.0.0
transport.host: 127.0.0.1
transport.tcp.port: 9500
transport.publish_port: 9500

当我启动容器时,这是我从http://localhost:9400/_nodes得到的:

"cluster_name": "log-cluster",
  "nodes": {
    "xLsGj2DyTdCF89I7sAToVw": {
      "name": "log-node",
      "transport_address": "127.0.0.1:9500",
      "host": "127.0.0.1",
      "ip": "127.0.0.1",
      "version": "2.4.0",
      "build": "ce9f0c7",
      "http_address": "172.18.0.5:9200",
      "settings": {
        "cluster": {
          "name": "log-cluster"
        },
        ... (I can put all response if needed)

JAVA API:

现在我正在尝试连接到此ES节点,如下所示:

    @Bean
    public ElasticsearchOperations logsElasticsearchOperations() throws UnknownHostException {
        Settings settings = Settings.settingsBuilder()
            .put("cluster.name", "log-cluster")
            .put("node.name", "log-node")
            .build();

        Client client = TransportClient.builder()
            .settings(settings)
            .build()
            .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9500)));


        ElasticsearchTemplate template = new ElasticsearchTemplate(client);
        template.createIndex(ProcessLog.class);
        log.debug("Elasticsearch for logs configured.");
        return template;
    }

我得到的错误是最着名的错误:

Caused by: org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9500}]

我用Google搜索并尝试了不同的配置方法,同时将client.transport.sniff设置为false,但没有设置为docker-compose.yml。现在我花了很多时间尝试配置这个,我仍然缺少一些东西。

提前感谢您的帮助。

更新

启动应用程序时还会运行嵌入式ES实例。所以主机端口配置是这样的:

  • 嵌入式ES:9200(http),9300(tcp)
  • Docker的ES:9400(http),9500(tcp)

这是完整的 version: '2' services: jhipster-elasticsearch: # elasticsearch 2.4.0 - to be in line with spring boot version image: jhipster/jhipster-elasticsearch:v1.3.2 ports: - 9400:9200 - 9500:9300 volumes: - ./log-es-config/elasticsearch_custom.yml:/usr/share/elasticsearch/config/elasticsearch.yml jhipster-logstash: image: jhipster/jhipster-logstash:v2.2.1 command: logstash -f /conf/logstash_custom.conf ports: - 5000:5000/udp - 6000:6000/tcp volumes: - ./logstash-log-es-conf/:/conf jhipster-console: image: jhipster/jhipster-console:v2.0.1 ports: - 5601:5601 jhipster-zipkin: image: jhipster/jhipster-zipkin:v2.0.1 ports: - 9411:9411 environment: - ES_HOSTS=http://jhipster-elasticsearch:9400 - ZIPKIN_UI_LOGS_URL=http://localhost:5601/app/kibana#/dashboard/logs-dashboard?_g=(refreshInterval:(display:Off,pause:!f,value:0),time:(from:now-1h,mode:quick,to:now))&_a=(filters:!(),options:(darkTheme:!f),panels:!((col:1,id:logs-levels,panelIndex:2,row:1,size_x:6,size_y:3,type:visualization),(col:7,columns:!(stack_trace),id:Stacktraces,panelIndex:7,row:1,size_x:4,size_y:3,sort:!('@timestamp',desc),type:search),(col:11,id:Log-forwarding-instructions,panelIndex:8,row:1,size_x:2,size_y:3,type:visualization),(col:1,columns:!(app_name,level,message),id:All-logs,panelIndex:9,row:4,size_x:12,size_y:7,sort:!('@timestamp',asc),type:search)),query:(query_string:(analyze_wildcard:!t,query:'{traceId}')),title:logs-dashboard,uiState:())

<body>
  <h1>Learning functions and forms</h1>
  <form id="make-average">
  <label for="average">Give me first number</label>
  <input type="text" name="user-entry" id="user-entry-1" placeholder="e.g., 3">
  <label for="average">Give me second number</label>
  <input type="text" name="user-entry" id="user-entry-2" placeholder="e.g., 3">
  <input type="submit" id="additems">
  <br><br>
  <div id="show-result">  </div>
</body>
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="function.js"></script>

3 个答案:

答案 0 :(得分:2)

我设法通过在transport.host内将0.0.0.0定义为elasticsearch_custom.yml来实现此功能,因此实例绑定到容器的ip。

也许这也应该是项目的github repoelasticsearch.yml的默认设置。

答案 1 :(得分:0)

从docker compose文件中,主机上的端口9500映射到容器内的端口9300,即:

elasticsearch_custom.yml

因为端口9500是Docker容器之外的TCP端口,所以在transport.tcp.port: 9300 transport.publish_port: 9300 配置文件中你应该有这个

.text

或者只是将这两行留下,因为9300是默认的TCP端口。

答案 2 :(得分:0)

我建议退一步并安装Head插件(https://github.com/mobz/elasticsearch-head)以便您可以获得群集视图,它将显示群集名称的详细信息。

也可以尝试从命令行发送一个简单的索引请求,以确保您可以连接到您的群集。