1.docker-compose.yaml部署
version: '3'
services:
elasticsearch:
image: elasticsearch:7.13.2
container_name: elasticsearch
environment:
- "cluster.name=elasticsearch" #设置集群名称为elasticsearch
- "discovery.type=single-node" #以单一节点模式启动
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
volumes:
- /mydata/elk/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
- /mydata/elk/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
ports:
- 9200:9200
- 9300:9300
kibana:
image: kibana:7.13.2
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:7.13.2
container_name: logstash
volumes:
- /mydata/elk/logstash/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
depends_on:
- elasticsearch #kibana在elasticsearch启动之后再启动
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
ports:
- 4560:4560
创建目录 mkdir /mydata/elk/logstash
进入 cd /mydata/elk/logstash
创造文件,可以创建多个索引。
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "es:9200"
index => "demo-spring-book-elk-%{+YYYY.MM.dd}"
}
elasticsearch {
hosts => "es:9200"
index => "demo-gateway-%{+YYYY.MM.dd}"
}
}
为es设置权限
chmod 777 /mydata/elk/elasticsearch
启动
docker-compose up -d
# 进入logstash容器
docker exec -it logstash /bin/bash
# 进入bin目录
cd /bin/
# 安装插件
logstash-plugin install logstash-codec-json_lines
# 退出容器
exit
# 重启logstash服务
docker restart logstash
访问地址:http://192.168.3.101:5601
<dependency>
<groupId>net.logstash.logbackgroupId>
<artifactId>logstash-logback-encoderartifactId>
<version>5.3version>
dependency>
并且设置拦截器,定义唯一得请求id
package com.hjt.filter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
@Component
@Slf4j
public class TraceFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
// "traceId" 正常来说这个应该是前端传给后端得 这个只是作为后端demo得演示
String requestNo = httpServletRequest.getHeader("request_no");
log.info("-----输出:requesetNo,{}",requestNo);
System.out.println("输出:"+requestNo);
MDC.put("traceid", getTraceId());
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
private String getTraceId() {
long timestamp = System.currentTimeMillis();
UUID uuid = UUID.randomUUID();
String uniqueId = timestamp + uuid.toString().replace("-", "");
return uniqueId;
}
}
注意appender节点下的destination需要改成你自己的logstash服务地址,比如我的是:192.168.3.101:4560 。
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="demo-spring-boot-elk"/>
<contextName>${APP_NAME}contextName>
<property name="maxHistory" value="30" />
<property name="maxFileSize" value="100MB" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss} | %X{traceid} | %highlight(%5p) | %green(%thread) | %boldMagenta(%logger) | %msg%n
pattern>
encoder>
appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>
log/app.%d{yyyy-MM-dd}.%i.log
fileNamePattern>
<maxHistory>${maxHistory}maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}maxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
appender>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>hjt-code:4560destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
appender>
<logger name="java.sql.PreparedStatement" value="DEBUG" />
<logger name="java.sql.Connection" value="DEBUG" />
<logger name="java.sql.Statement" value="DEBUG" />
<logger name="com.ibatis" value="DEBUG" />
<logger name="com.ibatis.common.jdbc.SimpleDataSource" value="DEBUG" />
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" />
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate"
value="DEBUG" />
<logger name="com.apache.ibatis" level="TRACE" />
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="LOGSTASH"/>
<appender-ref ref="FILE"/>
root>
configuration>
常见的日志级别
kibana中配置
回头看看我们索引的配置
注意,APP_NAME要和logstash.conf配置的索引名称一致。
创建成功
进入容器
docker exec -it kibana /bin/bash
进入config目录,修改kibana.yml配置
增加配置文件
i18n.locale: "zh-CN"
保存退出,并重启容器。
如果存在多个服务同时往同一个logstash中存放日志信息,可以采用两种方式进行实现
在同一个logstash-logs.conf文件中,配置多个input及output,比如:
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "es:9200"
index => "demo-spring-book-elk-%{+YYYY.MM.dd}"
}
}
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4561
codec => json_lines
}
}
output {
elasticsearch {
hosts => "es:9200"
index => "demo-gateway-%{+YYYY.MM.dd}"
}
}
代码地址
https://github.com/hongjiatao/spring-boot-anyDemo