通过Java / Spring Boot连接到Docker Elasticsearch实例 [英] Connecting to Docker Elasticsearch instance through Java/Spring Boot

查看:115
本文介绍了通过Java / Spring Boot连接到Docker Elasticsearch实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Docker运行Elasticsearch实例。该图片来自jHipster码头中心仓库: jhipster / jhipster-elasticsearch / -我使用的是图像v1.3.2,因为我需要Elasticsearch 2.4.0(以便与该项目的Spring Boot版本保持一致)。

I'm running Elasticsearch instance from Docker. The image is from jHipster docker hub repo: jhipster/jhipster-elasticsearch/ - I'm using image v1.3.2 because I need Elasticsearch 2.4.0 (to be in line with Spring Boot version of the project).

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

I'm starting ES container along with Logstash and Kibana images, with docker-compose. This are the settings for starting ES container:

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进行传输

So I'm using 9400 for REST and 9500 for transport communication.

这是 elasticsearch_custom.yml 内的配置,已安装到ES配置:

This is configuration inside elasticsearch_custom.yml that is mounted to 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

When I start container, this is what I get from 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节点:

Now I'm trying to connect to this ES node like this:

    @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;
    }

我遇到的错误是最著名的错误:

The error I'm getting is the most famous one:

原因:org.elasticsearch.client.transport.NoNodeAvailableException:没有配置的节点可用:[{#transport#-1} { 127.0.0.1} {127.0.0.1:9500}]

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 ,但没有一个起作用。现在,我花了很多时间来尝试配置它,但我仍然缺少一些东西。

I googled and tried different config approaches, also with client.transport.sniff set to false, but non of those worked. Now I spent lots of time trying to configure this one and I'm still missing something.

在此先感谢您的帮助。

Thanks in advance for help.

更新:

当我启动应用程式。因此主机端口配置是这样的:

There is also embedded ES instance running when I start the app. So host ports config is like this:


  • 嵌入式ES:9200(http),9300(tcp)

  • Docker的ES:9400(http),9500(tcp)

这里是完整的 docker-compose .yml

    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:())


推荐答案

我设法通过将 transport.host 定义为 0.0.0.0来使其正常工作位于 elasticsearch_custom.yml 内,因此该实例绑定到容器的ip。

I managed to get this working by defining the transport.host as 0.0.0.0 inside elasticsearch_custom.yml, so the instance binds to the container's ip.

也许这也应该是项目github elasticsearch.yml 的默认设置。 jhipster-elasticsearch rel = nofollow noreferrer>回购。

Maybe this should be also default setup for elasticsearch.yml on the project's github repo.

这篇关于通过Java / Spring Boot连接到Docker Elasticsearch实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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