springboot整合elk(elasticsearch-6.8.1,logstash-6.3.0,kibana-6.8.1)
本案例demo及elk下载地址:资源下载链接
https://download.csdn.net/download/dayonglove2018/12881644
logstash感觉也应该用对应的版本,只是我这边下载速度太太太太慢了,几kb/s 甚至出现不下载。。。。
下载
https://www.elastic.co/cn/downloads/past-releases#elasticsearch
https://artifacts.elastic.co/downloads/logstash/logstash-6.3.0.zip
https://artifacts.elastic.co/downloads/kibana/kibana-6.8.1-windows-x86_64.zip
下载了好几个世纪:
配置与安装
配置elasticsearch
修改yml配置
找到elasticsearch.yml文件,在文件最下面添加如下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,Content-Type
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
双击启动 elasticsearch.bat
访问localhost:9200 会出现如下json说明启动成功!
设置ES密码
注意:得先启动es服务以后再开启窗口设置密码!
进入到D:\elk\elasticsearch-6.8.1\bin目录,启动cmd 执行:
elasticsearch-setup-passwords interactive
这个图片是之前安装过的,也是同样的步骤
es安装,配置好了。可以关了黑窗口,再重新启动一次
启动OK!
配置kibana
修改kibana.yml文件
添加如下内容:
刚才设置的es的用户名和密码,配置到这里
server.host: "0.0.0.0"
# ES的用户名和密码
elasticsearch.url: "http://localhost:9200"
elasticsearch.username: "elastic"
elasticsearch.password: "123456"
启动kibana
双击kibana.bat
访问:http://localhost:5601/
启动成功。用刚才配置的用户名(elastic)和密码(123456)登录即可!
配置logstash
新建logtash.conf文件,放到bin目录下,方便启动操作
注意:配置文件名写错了。所以这里得错下去
配置内容:
logstash.conf
读取控制台日志:
input {
stdin {
} }
input {
tcp {
host => "127.0.0.1"
port => 9250
mode => "server"
# tags => ["tags"]
codec => json_lines
}
}
# output {
stdout {
codec => rubydebug } }
output {
stdout{
codec =>rubydebug}
elasticsearch {
hosts => ["localhost:9200"]
index => "logback-%{+YYYY.MM.dd}"
user => "elastic"
password => "123456"
}
}
读取日志文件:
input {
file {
path => "c:/opt/logs/java-contract-info.log" # 日志文件
type => "elasticsearch"
discover_interval => 3 #心跳监听日志文件是否改变
start_position => "beginning" #从文件开始处读写
}
}
output {
stdout{
codec => rubydebug
}
elasticsearch {
hosts => "localhost:9200"
index => "logstash-log4j2-%{+YYYY.MM.dd}"
user => "elastic"
password => "123456"
}
}
启动logstash
进入到 D:\elk\logstash-6.3.0\bin 目录下,输入命令:
logstash -f logtash.conf
到此elk都启动了。下面开始springboot整合他们
spingboot整合
目录结构为:
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.zjy</groupId>
<artifactId>elk</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elk</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-core</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.data</groupId>