Home > Software design >  Spring boot and logstash tcp link dosent work with docker compose "localhost/<unresolved>
Spring boot and logstash tcp link dosent work with docker compose "localhost/<unresolved>

Time:05-28

It works for me in local but i had an error when using docker :

error is : localhost/<unresolved>:5000: connection, how can i set this unresolved value for logstash destination id

docker-compose

 version: '3.2'
    services:
      elasticsearch:
        image: elasticsearch:$ELK_VERSION
        volumes:
          - elasticsearch:/usr/share/elasticsearch/data
        environment:
          ES_JAVA_OPTS: "-Xmx256m -Xms256m"
          # Note: currently there doesn't seem to be a way to change the default user for Elasticsearch
          ELASTIC_PASSWORD: $ELASTIC_PASSWORD
          # Use single node discovery in order to disable production mode and avoid bootstrap checks
          # see https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
          discovery.type: single-node
          # X-Pack security needs to be enabled for Elasticsearch to actually authenticate requests
          xpack.security.enabled: "true"
        ports:
          - "9200:9200"
          - "9300:9300"
        healthcheck:
          test: "wget -q -O - http://$ELASTIC_USER:$ELASTIC_PASSWORD@localhost:9200/_cat/health"
          interval: 1s
          timeout: 30s
          retries: 300
        networks:
          - internal
        restart: unless-stopped
      # https://www.elastic.co/guide/en/logstash/current/docker-config.html
      logstash:
        image: logstash:$ELK_VERSION
        ports:
          - "5000:5000"
          - "9600:9600"
        environment:
          LS_JAVA_OPTS: "-Xmx256m -Xms256m"
          ELASTIC_USER: $ELASTIC_USER
          ELASTIC_PASSWORD: $ELASTIC_PASSWORD
          XPACK_MONITORING_ELASTICSEARCH_USERNAME: $ELASTIC_USER
          XPACK_MONITORING_ELASTICSEARCH_PASSWORD: $ELASTIC_PASSWORD
          XPACK_MONITORING_ELASTICSEARCH_HOSTS: "elasticsearch:9200"
          XPACK_MONITORING_ENABLED: "true"
        volumes:
          - ./logstash/pipeline:/usr/share/logstash/pipeline:ro
        networks:
          - internal
        restart: unless-stopped
        depends_on:
          - elasticsearch
    
      # https://www.elastic.co/guide/en/kibana/current/docker.html
      kibana:
        image: kibana:${ELK_VERSION}
        environment:
          ELASTICSEARCH_USERNAME: $ELASTIC_USER
          ELASTICSEARCH_PASSWORD: $ELASTIC_PASSWORD
          # Because Elasticsearch is running in a containerized environment
          # (setting this to false will result in CPU stats not being correct in the Monitoring UI):
          XPACK_MONITORING_UI_CONTAINER_ELASTICSEARCH_ENABLED: "true"
        ports:
          - "5601:5601"
        networks:
          - internal
        restart: unless-stopped
        depends_on:
          - elasticsearch
          - logstash
      mysqldb:
        image: mysql:5.7
        restart: unless-stopped
        env_file: ./.env
        environment:
          - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
          - MYSQL_DATABASE=$MYSQLDB_DATABASE
        ports:
          - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
        volumes:
          - db:/var/lib/mysql
      app:
        depends_on:
          - mysqldb
        build: ./../
        restart: on-failure
        env_file: ./.env
        ports:
          - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
        environment:
          SPRING_APPLICATION_JSON: '{
            "spring.datasource.url"  : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
            "spring.datasource.username" : "$MYSQLDB_USER",
            "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
            "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
            "spring.jpa.hibernate.ddl-auto" : "update",
            "spring.application.name" : "ebnelhaythem"
          }'
        volumes:
          - .m2:/root/.m2
    
    networks:
      internal:
    
    volumes:
      elasticsearch:
      db:

AND LOGS ARE :

elastic_log_docker-app-1            | 14:15:45,959 |-WARN in net.logstash.logback.appender.LogstashTcpSocketAppender[logstash] - Log destination localhost/<unresolved>:5000: connection
         failed. java.net.ConnectException: Connection refused
        elastic_log_docker-app-1            |   at java.net.ConnectException: Connection refused
        elastic_log_docker-app-1            |   at      at java.base/sun.nio.ch.Net.pollConnect(Native Method)
        elastic_log_docker-app-1            |   at      at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)

CodePudding user response:

Your Spring Boot application expect logstash to be available at localhost:

logstash:
  host: localhost

However, when you run everything inside docker-compose, logstash will not be present at localhost since this now refers to the app container.

To resolve this, override the logstash host property when you run the application with docker-compose with the value logstash (so the name of the docker-compose service). Similar as you do for the MySQL url.

  • Related