从 Spring Boot 应用程序连接到 Logstash 时 Logstash-logback-encoder 连接超时

问题描述 投票:0回答:1

我在启动 Spring Boot 3 应用程序时遇到以下错误:“net.logstash.logback:logstash-logback-encoder:7.4”

01:53:14,051 |-WARN in net.logstash.logback.appender.LogstashTcpSocketAppender[LOGSTASH] - Log destination localhost/<unresolved>:5000: connection failed. java.net.ConnectException: Connection refused at java.net.ConnectException: Connection refused at at java.base/sun.nio.ch.Net.pollConnect(Native Method) at at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) at at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:539) at at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594) at at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) at at java.base/java.net.Socket.connect(Socket.java:633) at at net.logstash.logback.appender.AbstractLogstashTcpSocketAppender$TcpSendingEventHandler.openSocket(AbstractLogstashTcpSocketAppender.java:765) at at net.logstash.logback.appender.AbstractLogstashTcpSocketAppender$TcpSendingEventHandler.onStart(AbstractLogstashTcpSocketAppender.java:691) at at net.logstash.logback.appender.AsyncDisruptorAppender$EventClearingEventHandler.onStart(AsyncDisruptorAppender.java:382) at at net.logstash.logback.encoder.com.lmax.disruptor.BatchEventProcessor.notifyStart(BatchEventProcessor.java:224) at at net.logstash.logback.encoder.com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:120) at at java.base/java.lang.Thread.run(Thread.java:833)
我的 spring 应用程序正在使用 

'org.springframework.boot:spring-boot-docker-compose' 依赖于管理器 docker-compose 容器。在 docker-compose 中,我的 ELK 设置如下

services: elasticsearch: image: elasticsearch:8.13.0 environment: - node.name=elasticsearch - cluster.name=es-docker-cluster - discovery.type=single-node - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - xpack.security.enabled=false volumes: - elasticsearch-data:/usr/share/elasticsearch/data ports: - "9200:9200" - "9300:9300" logstash: image: logstash:8.13.0 command: logstash -f /etc/logstash/conf.d/logstash.conf ports: - "5000:5000" # Expose the default Logstash TCP input port - "9600:9600" # Expose the Logstash monitoring port volumes: - ./logstash/config:/etc/logstash/conf.d depends_on: - elasticsearch kibana: image: kibana:8.13.0 environment: - SERVER_NAME=kibana - ELASTICSEARCH_HOSTS=http://elasticsearch:9200 # - ELASTICSEARCH_USERNAME=kibana_system # - ELASTICSEARCH_PASSWORD=kibana_password # ${KIBANA_PASSWORD} ports: - "5601:5601" depends_on: - elasticsearch volumes: elasticsearch-data:
问题仅出现在 Spring 应用程序启动之初。 

spring-boot-docker-compose运行logstash容器后,appender尝试连接logstash容器,但第一次失败(因为logstash需要等待elasticsearch启动然后启动);然后每 30 秒重试一次连接,并能够连接到logstash 服务器。基于 logstash-logback-encoder - https://github.com/logfellow/logstash-logback-encoder?tab=readme-ov-file#connection-timeout 的文档,我尝试将此 connectionTimeout 添加到我的 logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n</pattern> </encoder> </appender> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>localhost:5000</destination> <connectionTimeout>60 seconds</connectionTimeout> <encoder class="net.logstash.logback.encoder.LogstashEncoder"/> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="LOGSTASH"/> </root> </configuration>
Spring 能够从 logback-spring.xml 加载更改,但单独的 connectionTimeout 不起作用。有谁知道我错过了什么?

spring-boot docker docker-compose logstash logstash-logback-encoder
1个回答
0
投票
我认为你应该简单地为 Elasticsearch 和依赖服务添加健康检查,如官方文档

中所述。 然后您可以添加一个条件作为

condition: service_healthy

这确保了logstash在elasticsearch的健康状况之后启动。

© www.soinside.com 2019 - 2024. All rights reserved.