ELK服务部署参考:https://preparedata.blog.csdn.net/article/details/113386835
当ELK服务安装成功后,进行下面spring cloud 配置ELK
一、引入logstash依赖
在build.gradle文件中的dependencies的对象中引入依赖
compile 'net.logstash.logback:logstash-logback-encoder:5.2'
二、application.xml配置logstash
spring:
# logStashHost服务配置 9200是logstash默认的端口
logStashHost:
ip: localhost
port: 9200
三、配置logback-spring
新建logback-spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--配置logstash的ip和端口,logstash.conf的input需要对应}-->
<destination>localhost:2223</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<!-- 展示info级别日志 -->
<root level="INFO">
<!-- LOGSTASH展示 -->
<appender-ref ref="LOGSTASH" />
<!-- 控制台展示 -->
<appender-ref ref="CONSOLE" />
</root>
</configuration>
配置logstash的ip和端口,需要和logstash.conf的input对应
logstash.conf配置参考:https://preparedata.blog.csdn.net/article/details/113386835
四、在logstash服务中配置
在logstash.conf文件中配置
部署参考:https://preparedata.blog.csdn.net/article/details/113386835
input {
# 日志采集输入配置,可配置多个输入
# 参考 https://www.elastic.co/guide/en/logstash/current/plugins-inputs-elasticsearch.html
tcp {
mode => "server"
host => "0.0.0.0" # 0.0.0.0 允许任意主机发送日志, 主机可以是IP,HOST,IP:port或HOST:port。端口默认为9200。
type => "pd.shop" # 区分每个输入源-订单服务
port => 2222 # 输入源的端口号
codec => json_lines # 数据格式
}
}
filter {
}
output {
# 日志输出配置,可配置多个输出
# 参考: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
if [type] == "pd.shop" {
elasticsearch {
action => "index" # 输出时创建映射
hosts => "http://localhost:9200" # ElasticSearch 的地址和端口
index => "pd.shop" # 指定索引名
codec => "json"
}
}
}
四、打印日志
首先需要添加注解@Slf4j
package com.pd.shop.service.impl;
import com.pd.shop.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserInfoImpl implements UserInfoService {
@Override
public void queryList(){
log.info("aaaa");
log.debug("bbbb");
log.error("cccc");
}
}
五、Kibana创建索引
在Kibana页面中创建索引pd.shop(logstash.conf配置中定义的索引名)
1.管理菜单
2.索引模式
3.创建索引
4.新建索引名(索引名需要和ELK配置中对用)
5.选择时间戳
6.查询日志
转载:https://blog.csdn.net/sinat_16998945/article/details/113842014
查看评论