springboot配置ELK日志分析系统

tech2023-02-03  101

springboot配置ELK日志分析系统

环境配置下载组件配置Elasticsearch配置Kibana配置Logstash springboot服务测试

环境配置

下载组件

下载组件官网https://www.elastic.co/cn/downloads

配置Elasticsearch

修改配置文件 vim elasticsearch/config/elasticsearch.yml //配置外网连接,如果不能用请检查防火墙配置 node.name: node-1 network.host: 0.0.0.0 http.port: 9200 cluster.initial_master_nodes: ["node-1"]

出现的报错:

ERROR: [1] bootstrap checks failed [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

解决:

#修改config/elasticsearch.yml下,放开node.name注释,可更改名称 node.name: node-1 cluster.initial_master_nodes: [“node-1”]

无法通过ip访问:

#修改config/elasticsearch.yml下约第55行,修改为当前es地址或0.0.0.0 network.host: 0.0.0.0

//后台启动elasticsearch服务 nohup ./elasticsearch/bin/elasticsearch &

配置Kibana

修改配置

vim /kibana/config/kibana.yml server.port: 5601 server.host: "0.0.0.0" elasticsearch.hosts: ["http://192.168.0.164:9200"] 后台启动: nohup ./kibana/bin/kibana &

配置Logstash

创建并编辑一个新配置文件

mkdir /logstash/webconfig touch logstash/webconfig/log_elk.conf vim /logstash/webconfig/log_elk.conf # Sample Logstash configuration for creating a simple # Beats -> Logstash -> Elasticsearch pipeline. # 两个不同的输入,将两个服务的日志分别输入到不同的索引中 input { tcp { type => "elk1" # 设定type以区分每个输入源 host => "0.0.0.0" # 允许任意主机发送日志 port => 9250 codec => json_lines mode => "server" } tcp { type => "elk2" host => "0.0.0.0" port => 9251 codec => json_lines mode => "server" } } output { if [type] == "elk1" { elasticsearch { hosts => ["http://192.168.0.164:9200"] # ElasticSearch 的地址和端口 index => "elk1" # 指定索引名 } } if [type] == "elk2" { elasticsearch { hosts => ["http://192.168.0.164:9200"] index => "elk2" } } }

无法通过ip访问9600 只能使用localhost:9600 解决:

修改config/logstash.yml下约第118行,修改为当前ip地址或0.0.0.0 http.host: "0.0.0.0" 并且要注释掉 pipeline.ordered: auto

启动logstash

//-f后可以接指定的配置文件,也可以是一个目录,它会自动将目录中的所有配置文件一起导入合成一个配置文件,实现多配置文件启动 //单文件: nohup /logstash/bin/logstash -f /elk/logstash/wegconfig/log_elk.conf & //目录: nohup /logstash/bin/logstash -f /elk/logstash/wegconfig &

springboot服务测试

maven引用jar包 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.3</version> </dependency>

在resource目录下创建logback.xml

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <charset>UTF-8</charset> <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level [%t] [%logger - %line]: %m%n</Pattern> </encoder> </appender> <appender class="net.logstash.logback.appender.LogstashTcpSocketAppender" name="LOGSTASH"> <!--配置logstash的ip和端口,在logstash配置文件中 不同端口--> <destination>172.31.0.10:9251</destination> <encoder class="net.logstash.logback.encoder.LogstashEncoder" charset="UTF-8"/> </appender> <root level="INFO"> <appender-ref ref="LOGSTASH"/> <appender-ref ref="CONSOLE"/> </root> </configuration>
最新回复(0)