我运行以下命令在本地机器上设置 ELK 堆栈:
docker run -e "discovery.type=single-node" -d -it --name es -p 9200:9200 -p 9300:9300 elasticsearch:7.2.0
docker run -d -it --name kibana --link es:elasticsearch -p 5601:5601 kibana:7.2.0
docker run -d -it --name logstash -p 5000:5000 logstash:7.2.0 --path.settings= -e 'input { tcp { port => 5000 codec => "json" } } output { elasticsearch { hosts => ["localhost"] index => "micro-%{serviceName}"} }'
然后我添加了 logback 依赖项并在我的微服务中更改了默认 logback.xml 以配置LogstashTcpSocketAppender
如下:
<configuration>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<property name="LOG_LEVEL_PATTERN" value="[${springAppName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]" />
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<!-- Appender to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:5000</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc />
<context />
<logLevel />
<loggerName />
<pattern>
<pattern>
{
"serviceName": "PchService"
}
</pattern>
</pattern>
<threadName />
<message />
<logstashMarkers />
<stackTrace />
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="STASH"/>
</root>
</configuration>
然后向微服务发送了一些请求。但是当我在http://localhost:5601打开 kibana 时,没有数据/日志被发送到弹性搜索。我该如何解决这个问题?