SpringBoot實戰電商項目mall地址:github.com/macrozheng/…php
ELK即Elasticsearch、Logstash、Kibana,組合起來能夠搭建線上日誌系統,本文主要講解使用ELK來收集SpringBoot應用產生的日誌。java
docker pull elasticsearch:6.4.0
docker pull logstash:6.4.0
docker pull kibana:6.4.0
複製代碼
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "es:9200"
index => "springboot-logstash-%{+YYYY.MM.dd}"
}
}
複製代碼
mkdir /mydata/logstash
複製代碼
version: '3'
services:
elasticsearch:
image: elasticsearch:6.4.0
container_name: elasticsearch
environment:
- "cluster.name=elasticsearch" #設置集羣名稱爲elasticsearch
- "discovery.type=single-node" #以單一節點模式啓動
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #設置使用jvm內存大小
volumes:
- /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件掛載
- /mydata/elasticsearch/data:/usr/share/elasticsearch/data #數據文件掛載
ports:
- 9200:9200
kibana:
image: kibana:6.4.0
container_name: kibana
links:
- elasticsearch:es #能夠用es這個域名訪問elasticsearch服務
depends_on:
- elasticsearch #kibana在elasticsearch啓動以後再啓動
environment:
- "elasticsearch.hosts=http://es:9200" #設置訪問elasticsearch的地址
ports:
- 5601:5601
logstash:
image: logstash:6.4.0
container_name: logstash
volumes:
- /mydata/logstash/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf #掛載logstash的配置文件
depends_on:
- elasticsearch #kibana在elasticsearch啓動以後再啓動
links:
- elasticsearch:es #能夠用es這個域名訪問elasticsearch服務
ports:
- 4560:4560
複製代碼
docker-compose up -d
複製代碼
注意:Elasticsearch啓動可能須要好幾分鐘,要耐心等待。 node
# 進入logstash容器
docker exec -it logstash /bin/bash
# 進入bin目錄
cd /bin/
# 安裝插件
logstash-plugin install logstash-codec-json_lines
# 退出容器
exit
# 重啓logstash服務
docker restart logstash
複製代碼
systemctl stop firewalld
複製代碼
訪問地址:http://192.168.3.101:5601linux
<!--集成logstash-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
複製代碼
注意appender節點下的destination須要改爲你本身的logstash服務地址,好比個人是:192.168.3.101:4560 。git
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<!--應用名稱-->
<property name="APP_NAME" value="mall-admin"/>
<!--日誌文件保存路徑-->
<property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/>
<contextName>${APP_NAME}</contextName>
<!--天天記錄日誌到文件appender-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!--輸出到logstash的appender-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--能夠訪問的logstash日誌收集端口-->
<destination>192.168.3.101:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
複製代碼
搭建了ELK日誌收集系統以後,咱們若是要查看SpringBoot應用的日誌信息,就不須要查看日誌文件了,直接在Kibana中查看便可。github
github.com/macrozheng/…spring
mall項目全套學習教程連載中,關注公衆號第一時間獲取。docker