springboot结合elk实现案例
时间: 2023-10-15 07:27:06 浏览: 51
下面是一个简单的Spring Boot与ELK集成的实现案例:
1. 添加依赖
在Spring Boot应用程序的pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-socketappender</artifactId>
<version>6.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
```
2. 配置logback.xml
在src/main/resources目录下创建logback.xml文件,并添加以下配置:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="logstash"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOGSTASH_HOST}:${LOGSTASH_PORT}</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<logger name="com.example" level="DEBUG" additivity="false">
<appender-ref ref="logstash" />
</logger>
<root level="INFO">
<appender-ref ref="logstash" />
</root>
</configuration>
```
3. 配置logstash.conf
在logstash的安装目录下创建logstash.conf文件,并添加以下配置:
```conf
input {
tcp {
port => 5000
codec => json_lines
}
}
output {
elasticsearch {
hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
index => "logstash-%{+YYYY.MM.dd}"
document_type => "log"
}
}
```
4. 配置application.properties
在Spring Boot应用程序的src/main/resources目录下创建application.properties文件,并添加以下配置:
```properties
server.port=8080
spring.application.name=example-app
logging.file=./logs/example-app.log
# Logstash
LOGSTASH_HOST=localhost
LOGSTASH_PORT=5000
# Elasticsearch
ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
```
5. 启动ELK
启动Elasticsearch和Kibana。在logstash的安装目录下执行以下命令启动logstash:
```sh
bin/logstash -f logstash.conf
```
6. 测试应用程序
在浏览器中访问http://localhost:8080/hello,应用程序会记录一条日志。打开Kibana,创建索引模式,然后在Discover页面中可以看到刚刚记录的日志。创建一个仪表板,可以监控应用程序的性能和健康状况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)