springcloud集成elk

2022-03-29 14:41:54 浏览数 (1)

springcloud是一个应用很广的微服务全家桶,阿里开发了自己的springcloud alibaba分支,elk是一款不错的监控工具,尤其是日志分析,可以将springcloud微服务项目的日志通过logstash-logback-encoder传送到elk的logstash上,好了废话不多少了,直接进入环境搭建流程:

1.使用docker-compose搭建elk环境,docker-compose.yml文件内容如下:

代码语言:javascript复制
version: "3"
services:
  elasticsearch:
    image: docker.io/elasticsearch:6.8.0
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - ./es/data:/usr/share/elasticsearch/data
    container_name: elasticsearch680
    hostname: elasticsearch
    privileged: true
    restart: always
    ports:
      - "9200:9200"
      - "9300:9300"
  logstash:
   image: docker.io/logstash:6.8.0
   volumes:
    - ./logstash.conf:/etc/logstash.conf
   privileged: true
   environment:
    LOGSPOUT: ignore
   ports:
    - "4560:4560/tcp"
   links:
    - elasticsearch
   command: -f /etc/logstash.conf
  kibana:
    image: docker.io/kibana:6.8.0
    environment:
      - ELASTICSEARCH_URL=http://elasticsearch:9200
    container_name: kibana680
    hostname: kibana
    depends_on:
      - elasticsearch
    restart: always
    ports:
      - "5601:5601"

2.logstash.conf文件内容如下:

代码语言:javascript复制
input {
   tcp {
        port => 4560
        mode => "server"
        tags => ["tags"]

    ##格式json
        codec => json_lines
    }

}

output {
        elasticsearch {
        #ES地址
                hosts => "192.168.111.137:9200"
        #指定索引名字
                index => "applog"
        }
        stdout { codec => rubydebug}
}

3.通过docker-compose up -d启动elk环境,前提是已经安装好docker-compose环境,具体怎么安装这里不再赘述。

4.上面是elk环境的搭建,也就是服务端,客户端也就是springcloud项目具体工作有两个:1.添加依赖,在logback-xxxx.xml文件加入对应appender

代码语言:javascript复制
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.3</version>
        </dependency>
代码语言:javascript复制
    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>192.168.111.137:4560</destination>
        <!-- 日志输出编码 -->
        
        
        <encoder charset="UTF-8"
                class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "logLevel": "%level",
                        "serviceName": "${springAppName:-SpringApp}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "line":"%L",
                        "message": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>   

     <root level="INFO">
        <appender-ref ref="console" />
        <appender-ref ref="LOGSTASH"></appender-ref>
    </root>

5.上述过程不出意外的话整个环境就搭建好了:

0 人点赞