ELK技术栈系列- Logstash(五) 搭建日志系统

本文详细指导如何在Spring Boot项目中搭建ELK(Elasticsearch, Logstash, Kibana)日志系统,包括配置pom.xml、设置Elasticsearch连接、定义Log实体、创建Service和Controller,并实现查询接口。
摘要由CSDN通过智能技术生成

搭建日志系统

绝大多数项目在后台管理中都有日志管理。以前的日志信息是存储在 MySQL 中,日志
随着项目运行时间会越来越多,一直存储在 MySQL 会导致查询降低。现在的日志信息通过
ELK 技术栈进行操作。存储在 Elasticsearch 中,可以更好的分析日志内容及更快查询效率。

给定简单需求:

搭建日志系统,提供查询 Elasticsearch 中日志信息的接口。

1 新建项目

名称为 ELK_Demo

2 修改 pom.xml

搭建最基本的环境,实现需求,没有考虑 Spring Cloud 相关环境,如果考虑 Spring Cloud还需要配置 Eureka 等信息。

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.6.RELEASE</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>1.18.12</version>
	</dependency>
</dependencies>

3 创建配置文件

在 resources 下新建 application.yml 配置文件。

配置 Elasticsearch 相关配置信息。

spring:
    data:
        elasticsearch:
            cluster-name: elasticsearch
            cluster-nodes: 192.168.8.140:9300

4 新建实体

根据 kibana 中查看到日志信息可以得出看出,除了 message 是类类型,里面包含一些其他属性外,其他的属性都是简单类型属性。

新建 com.bjsxt.pojo.Log。

注意@version 和@timestamp 要使用@JsonProperty 进行接收。

@Data
@Document(indexName = "test_log",type = "doc")
public class Log {
	@Id
	private String id;
	@Field(type= FieldType.Text)
	private String host;
	@Field(type= FieldType.Text)
	private String message;
	@Field(type= FieldType.Long)
	private Long port;
	@Field(type = FieldType.Date)
	@JsonProperty("@timestamp")
	private Date timestamp;
	@Field(type = FieldType.Text)
	@JsonProperty("@version")
	private String version;
}

5 新建 service 及实现类

新建 com.bjsxt.service.LogService 及实现类


public interface LogService {
    List<Log> selectByPage(int page,int size);
}
@Service
public class LogServiceImpl implements LogService {
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;
	@Override
	public List<Log> selectByPage(int page, int size) {
		SearchQuery sq = new NativeSearchQuery(QueryBuilders.matchAllQuery());
		sq.setPageable(PageRequest.of(page-1,size));
		return elasticsearchTemplate.queryForList(sq,Log.class);
	}
}

6 新建控制器

新建 com.bjsxt.controller.LogController

@Controller
public class LogController {
	@Autowired
	private LogService logService;
	@RequestMapping("/page")
	@ResponseBody
	public List<Log> showPage(int page,int size){
		return logService.selectByPage(page,size);
	}
}

7 测试结果

在浏览器输入: http://localhost:8080/page?page=1&size=2

会看见下面的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plenilune-望月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值