springboot整合elk7.17.3

Posted on 2022-08-20

elk环境搭建
idea打包镜像参考

logback.xml添加下面内容

<!--LogStash访问host-->
<springProperty name="LOG_STASH_HOST" scope="context" source="logstash.host" defaultValue="localhost"/>


<!--接口访问记录日志输出到LogStash-->
<appender name="LOG_STASH_RECORD" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>${LOG_STASH_HOST}:4560</destination>
    <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <timestamp>
                <timeZone>Asia/Shanghai</timeZone>
            </timestamp>
            <!--自定义日志输出格式-->
            <pattern>
                <pattern>
                    {
                    "project": "spring-cloud-learning",
                    "level": "%level",
                    "service": "${APP_NAME:-}",
                    "class": "%logger",
                    "traceId": "%X{traceId}",
                    "spanId": "%X{spanId}",
                    "message": "%message"
                    }
                </pattern>
            </pattern>
        </providers>
    </encoder>
    <!--当有多个LogStash服务时,设置访问策略为轮询-->
    <connectionStrategy>
        <roundRobin>
            <connectionTTL>5 minutes</connectionTTL>
        </roundRobin>
    </connectionStrategy>
</appender>


<logger name="com.fandf.log.aspect.WebLogAspect" level="DEBUG">
    <appender-ref ref="LOG_STASH_RECORD"/>
</logger>


springboot项目配置文件添加

#logstash ip,mac本地连接方式
logstash:
  host: host.docker.internal

打包成镜像后执行

docker run -p 9005:9005 --name user-service \
--link logstash:logstash \
-v /etc/localtime:/etc/localtime \
-d fandf/user-service:1.0.0

我的controller

image.png

调用接口
image.png

登录kibana创建索引 image.png

description 为swagger注解描述, traceId为全局链路id image.png

源码地址

  • 只测试elk,可以在源码 application-docker.yml里面删除seata和nacos相关配置即可