SpringBoot服务集成ELK

文章目录

    • 前置环境
    • 部署步骤
      • pom.xml
      • defaults-logstash.xml
      • logback-spring.xml
      • bootstrap.yml
    • 服务部署
      • SpringBoot启动服务

前置环境

EleasticSearch7.2.0

Kibana7.2.0

Logstash7.2.0

部署步骤

pom.xml

<dependency>
	<groupId>net.logstash.logbackgroupId>
	<artifactId>logstash-logback-encoderartifactId>
	<version>6.5version>
dependency>

确认自己项目中是否存在 jackson依赖,一般情况下,如果有 redission、nacos的依赖就不需要引用。
注意logback的版本和jackson得版本存在关联差异性。我这边的对照版本是 jackson 2.11.4 => logback 6.5

defaults-logstash.xml


<included>
    
    
    
    <springProperty scope="context" name="logstash.ips" source="jamel.logstash.ips" defaultValue="127.0.0.1:5044"/>
    <springProperty scope="context" name="logstash.connectionTTL" source="jamel.logstash.connectionTTL"
                    defaultValue="5 minutes"/>

    
    <appender name="LOGSTASH_APPENDER" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <includeCallerData>trueincludeCallerData>
        <destination>${logstash.ips}destination>
        
        
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <pattern>
                    <omitEmptyFields>trueomitEmptyFields>
                    
                    <pattern>
                        {
                        "severName": "${spring.application.name}",
                        "severIp": "%ip",
                        "userId": "%userId",
                        "port": "${server.port}",
                        "level": "%p",
                        "marker": "%xMarker",
                        "method": "%xLogger.%xMethod:%xLine",
                        "message": "%m",
                        "exception": "${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}",
                        "createTime": "%d{yyyy-MM-dd HH:mm:ss.SSS}"
                        }
                    pattern>
                pattern>
            providers>
        encoder>
        
        <connectionStrategy>
            <roundRobin>
                <connectionTTL>${logstash.connectionTTL}connectionTTL>
            roundRobin>
        connectionStrategy>
    appender>
included>

logback-spring.xml


<configuration>
    <include resource="com/jamel/log/xml/defaults.xml"/>
    
    <include resource="com/jamel/log/xml/defaults-logstash.xml"/>

    <springProfile name="dev,prod">
        <root level="${log.level.console}">
            <appender-ref ref="LOGSTASH_APPENDER"/>
        root>
    springProfile>
configuration>

bootstrap.yml

jamel:
  logstash:
    ips: 47.107.76.32:5044

服务部署

SpringBoot启动服务

查看kibana情况,至此表示日志已经从springboot推送到es。可以通过kibana查看
SpringBoot服务集成ELK_第1张图片

SpringBoot服务集成ELK_第2张图片

你可能感兴趣的:(分布式架构,服务部署,spring,boot,elk,elasticsearch)