测试工具 - 日志收集与记录(Maven + Java + ELK)
环境部署
整体镜像运行情况
ElasticSearch部署
镜像下载
docker pull elasticsearch:7.6.2
启动镜像
docker run -itd --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
新建目录
D:\docker\elasticsearch\config
D:\docker\elasticsearch\data
D:\docker\elasticsearch\plugins
D:\docker\elasticsearch\logs
拷贝文件
docker cp es:/usr/share/elasticsearch/config/elasticsearch.keystore D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/jvm.options D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/role_mapping.yml D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/users D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/elasticsearch.yml D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/log4j2.properties D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/roles.yml D:\docker\elasticsearch\config\
docker cp es:/usr/share/elasticsearch/config/users_roles D:\docker\elasticsearch\config\
删除运行镜像
docker stop es
docker rm es
插件安装
# 可以下载的插件地址,请查询如下网址:
https://www.elastic.co/guide/en/elasticsearch/plugins/7.6/index.html
# 安装插件的两种方式
# 1)下载插件,放到D:\docker\elasticsearch\plugins
# 2)进入docker进行,通过命令 bin/elasticsearch-plugin install [插件名称]
启动镜像
docker run -itd --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v D:\docker\elasticsearch\config:/usr/share/elasticsearch/config -v D:\docker\elasticsearch\data:/usr/share/elasticsearch/data -v D:\docker\elasticsearch\logs:/usr/share/elasticsearch/logs -v D:\docker\elasticsearch\plugins:/usr/share/elasticsearch/plugins elasticsearch:7.6.2
环境验证
# 查看基本信息
http://localhost:9200
# 查看已经安装的插件
curl -X GET localhost:9200/_cat/plugins
# 查询健康状态
curl -X GET localhost:9200/_cat/health?v
# 查看集群节点
curl -X GET localhost:9200/_cat/nodes?v
# 查询有哪些索引
curl -X GET localhost:9200/_cat/indices?v
logstash部署
镜像下载
docker pull logstash:7.6.2
新建目录
D:\docker\logstash\pipeline
创建配置文件
# 文件名: logstash.sh
# 文件完整路径: D:\docker\logstash\pipeline\logstash.sh
# 文件内容
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "elasticsearch:9200"
index => "logstash-%{+YYYY.MM.dd}"
}
}
启动镜像
docker run -itd --name logstash --link es:elasticsearch -v D:\docker\logstash\pipeline\logstash.conf:/usr/share/logstash/pipeline/logstash.conf -p 9600:9600 -p 4560:4560 logstash:7.6.2
环境验证
# 查看基本信息
http://localhost:9600/
kibana部署
镜像下载
docker pull kibana:7.6.2
启动镜像
docker run -itd --name kibana --link es:elasticsearch -p 5601:5601 kibana:7.6.2
环境验证
http://localhost:5601/app/kibana
JAVA Demo发送测试案例日志
修改pom文件
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
增加logback.xml文件(在resource目录下)
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<appender name="socket" class="net.logstash.logback.appender.LogstashSocketAppender">
<host>localhost</host>
<port>4560</port>
</appender>
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!-- 此处的地址(IP:Port)请根据实际地址调整 --!>
<destination>localhost:4560</destination>
<!-- encoder is required -->
<encoder chareset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
<queueSize>1048576</queueSize>
</appender>
<root level="INFO">
<appender-ref ref="socket"/>
<appender-ref ref="stash"/>
</root>
</configuration>
添加测试代码
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
public class elkdemo {
// 日志采集
private static final Logger log = LoggerFactory.getLogger(elkdemo.class);
@Test
public void sendMsg(){
String msg = "This is test message";
log.info(msg);
}
}
在Kibana查看结果
JAVA Demo(Spring Boot)发送测试案例日志
修改pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<!--集成logstash-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
</dependencies>
增加logback-spring.xml(resource目录下)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<!--输出到logstash的appender-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--可以访问的logstash日志收集端口-->
<destination>localhost:4560</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
增加测试代码
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class elkdemo {
@Test
public void sendMsg() {
log.info("This is spring boot test message");
}
}
在Kibana中查看结果