elk 聚合日志

1.简介

随着微服务的使用,创建稳定的分布式应用程序和摆脱许多遗留问题变得很容易。

但是微服务的使用也带来了一些挑战, 分布式日志管理就是其中之一。

由于微服务是隔离的,因此它们不共享数据库和日志文件,因此实时搜索,分析和查看日志数据变得充满挑战。

这就是ELK堆栈的救援之处。

2. ELK

它是三个开源产品的集合:

  • 弹性搜索是基于JSON的NoSQL数据库
  • Logstash一个日志管道工具,可从各种来源获取输入,执行不同的转换并将数据导出到各种目标(此处为弹性搜索)
  • Kibana是可视化层,可在弹性搜索的基础上工作

请参考下面给出的架构:

ELK堆栈

日志存储从微服务中获取日志。

提取的日志将转换为JSON并提供给弹性搜索。

开发人员可以使用Kibana查看弹性搜索中存在的日志。

3.安装ELK

ELK是基于Java的。

在安装ELK之前,必须确保已JAVA_HOMEPATH ,并且已使用JDK 1.8完成安装。

3.1 Elasticsearch

  • 可以从下载页面下载最新版本的Elasticsearch,并将其解压缩到任何文件夹中
  • 可以使用bin\elasticsearch.bat从命令提示符处执行它
  • 默认情况下,它将从http:// localhost:9200开始

3.2基巴纳

  • 可以从下载页面下载最新版本的Kibana,并且可以将其提取到任何文件夹中
  • 可以使用bin\kibana.bat在命令提示符下执行它
  • 成功启动后,Kibana将在默认端口5601上启动,并且Kibana UI将位于http:// localhost:5601

3.3 Logstash

  • 可以从下载页面下载最新版本的Logstash,并将其解压缩到任何文件夹中
  • 根据配置说明创建一个文件cst_logstash.conf
  • 可以使用bin/logstash -f cst_logstash.conf在命令提示符下执行以启动logstash

4.创建一个示例微服务组件

创建微服务是必需的,以便logstash可以指向API日志。

下面的清单显示了示例微服务的代码。

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.xyz.app</groupId><artifactId>ArtDemo1001_Rest_Controller_Full_Deployment_Logging</artifactId><version>0.0.1-SNAPSHOT</version><!-- Add Spring repositories --><!-- (you don't need this if you are using a .RELEASE version) --><repositories><repository><id>spring-snapshots</id><url>http://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><url>http://repo.spring.io/milestone</url></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><url>http://repo.spring.io/snapshot</url></pluginRepository><pluginRepository><id>spring-milestones</id><url>http://repo.spring.io/milestone</url></pluginRepository></pluginRepositories><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR3</spring-cloud.version></properties><!-- Add typical dependencies for a web application --><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><!-- Package as an executable jar --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

上面的pom.xml代码已配置了基于Spring Boot的项目所需的依赖项。

EmployeeDAO.java

package com.xyz.app.dao;import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;import org.springframework.stereotype.Repository;import com.xyz.app.model.Employee;@Repository
public class EmployeeDAO {/*** Map is used to Replace the Database * */static public Map<Integer,Employee> mapOfEmloyees = new LinkedHashMap<Integer,Employee>();static int count=10004;static{mapOfEmloyees.put(10001, new Employee("Jack",10001,12345.6,1001));mapOfEmloyees.put(10002, new Employee("Justin",10002,12355.6,1002));mapOfEmloyees.put(10003, new Employee("Eric",10003,12445.6,1003));}/*** Returns all the Existing Employees* */public Collection getAllEmployee(){return mapOfEmloyees.values();            }/**Get Employee details using EmployeeId .* Returns an Employee object response with Data if Employee is Found* Else returns a null* */public Employee getEmployeeDetailsById(int id){return mapOfEmloyees.get(id);}/**Create Employee details.* Returns auto-generated Id* */public Integer addEmployee(Employee employee){count++;employee.setEmployeeId(count);mapOfEmloyees.put(count, employee);return count;}/**Update the Employee details,* Receives the Employee Object and returns the updated Details  * */public Employee updateEmployee (Employee employee){mapOfEmloyees.put(employee.getEmployeeId(), employee);return employee;}/**Delete the Employee details,* Receives the EmployeeID and returns the deleted employee's Details  * */public Employee removeEmployee (int id){Employee emp= mapOfEmloyees.remove(id);return emp;}}

上面的代码表示应用程序的DAO层。

CRUD操作在包含Employee对象的Map集合上执行,以避免数据库依赖性并保持应用程序轻巧。

EmployeeController.java

package com.xyz.app.controller;import java.util.Collection;import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import com.xyz.app.dao.EmployeeDAO;
import com.xyz.app.model.Employee;@RestController
public class EmployeeController {@Autowired private EmployeeDAO employeeDAO;public static Logger logger = Logger.getLogger(EmployeeController.class);/** Method is used to get all the employee details and return the same */ @RequestMapping(value="emp/controller/getDetails",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)public ResponseEntity<Collection> getEmployeeDetails(){logger.info("From Producer method[getEmployeeDetails] start");logger.debug("From Producer method[getEmployeeDetails] start");Collection  listEmployee =employeeDAO.getAllEmployee();logger.debug("From Producer method[getEmployeeDetails] start");logger.info("From Producer method[getEmployeeDetails] end");return new ResponseEntity<Collection>(listEmployee, HttpStatus.OK);}/** Method finds an employee using employeeId and returns the found Employee If no employee is not existing corresponding to the employeeId, then null is returned with HttpStatus.INTERNAL_SERVER_ERROR as status*/ @RequestMapping(value="emp/controller/getDetailsById/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)public ResponseEntity getEmployeeDetailByEmployeeId(@PathVariable("id") int myId){logger.info("From Producer method[getEmployeeDetailByEmployeeId] start");Employee employee = employeeDAO.getEmployeeDetailsById(myId);if(employee!=null){logger.info("From Producer method[getEmployeeDetailByEmployeeId] end");return new ResponseEntity(employee,HttpStatus.OK);}else{logger.info("From Producer method[getEmployeeDetailByEmployeeId] end");return new ResponseEntity(HttpStatus.NOT_FOUND);}}/** Method creates an employee and returns the auto-generated employeeId */ @RequestMapping(value="/emp/controller/addEmp",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.TEXT_HTML_VALUE)public ResponseEntity addEmployee(@RequestBody Employee employee){logger.info("From Producer method[addEmployee] start");logger.debug("From Producer method[addEmployee] start");int empId= employeeDAO.addEmployee(employee);logger.debug("From Producer method[addEmployee] start");logger.info("From Producer method[addEmployee] end");return new ResponseEntity("Employee added successfully with id:"+empId,HttpStatus.CREATED);}/** Method updates an employee and returns the updated Employee If Employee to be updated is not existing, then null is returned with HttpStatus.INTERNAL_SERVER_ERROR as status*/ @RequestMapping(value="/emp/controller/updateEmp",method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)public ResponseEntity updateEmployee(@RequestBody Employee employee){logger.info("From Producer method[updateEmployee] start");if(employeeDAO.getEmployeeDetailsById(employee.getEmployeeId())==null){Employee employee2=null;return new ResponseEntity(employee2,HttpStatus.INTERNAL_SERVER_ERROR);}System.out.println(employee);employeeDAO.updateEmployee(employee);logger.info("From Producer method[updateEmployee] end");return new ResponseEntity(employee,HttpStatus.OK);}/** Method deletes an employee using employeeId and returns the deleted Employee If Employee to be deleted is not existing, then null is returned with HttpStatus.INTERNAL_SERVER_ERROR as status*/ @RequestMapping(value="/emp/controller/deleteEmp/{id}",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_VALUE)public ResponseEntity deleteEmployee(@PathVariable("id") int myId){logger.info("From Producer method[deleteEmployee] start");if(employeeDAO.getEmployeeDetailsById(myId)==null){Employee employee2=null;return new ResponseEntity(employee2,HttpStatus.INTERNAL_SERVER_ERROR);}Employee employee = employeeDAO.removeEmployee(myId);System.out.println("Removed: "+employee);logger.info("From Producer method[deleteEmployee] end");return new ResponseEntity(employee,HttpStatus.OK);}
}

上面的代码代表具有请求处理程序的应用程序的控制器层。

请求处理程序调用DAO层函数并执行CRUD操作。

application.properties

server.port = 8090logging.level.com.xyz.app.controller.EmployeeController=DEBUG#name of the log file to be created#same file will be given as input to logstashlogging.file=app.logspring.application.name = producer

上面的代码代表为基于Spring Boot的应用程序配置的属性。

5. Logstash配置

如3.3节所述,需要为logstash创建配置文件。

logstash将使用此配置文件从微服务日志中获取输入。

日志被转换为JSON并馈入elasticsearch。

cst_logstash.conf

input {file {# If more than one log files from different microservices have to be tracked then a comma-separated list of log files can # be providedpath => ["PATH-TO-UPDATE/app.log"]codec => multiline {pattern => "^%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{TIME}.*"negate => "true"what => "previous"}}
}
output {stdout {codec => rubydebug}# Sending properly parsed log events to elasticsearchelasticsearch {hosts => ["localhost:9200"]}
}

上面的logstash配置文件侦听日志文件,并将日志消息推送到弹性搜索。

注意 :根据您的设置更改日志路径。

6.执行与输出

6.1为日志执行微服务

可以使用clean install spring-boot:run部署Spring Boot应用程序,并可以从浏览器或邮递员客户端访问以下URL: http:// localhost:8090 / emp / controller / getDetails 。

这将击中微服务并在微服务方面生成日志。

这些日志将由logstash读取,并推送到弹性搜索中,此外,可以使用Kibana进行后续步骤来查看这些日志。

6.2在Kibana上查看输出的步骤

  • 在管理控制台中配置索引。 使用索引值作为logstash-*作为默认配置。 打开链接: http:// localhost:5601 / app / kibana#/ management / kibana / index?_g =() ,它将显示如下屏幕:

Kibana Index Creation- 1
  • 单击下一步,将显示以下屏幕

Kibana Index Creation- 2

选择上面突出显示的选项,然后单击“创建索引模式”

  • 从左侧菜单中选择“发现”选项后,页面显示如下:

在Kibana-1上查看日志
  • 可以根据上面突出显示的属性来可视化和过滤日志。 将鼠标悬停在任何属性上后,将显示该属性的“添加”按钮。 在这里,选择消息属性视图如下所示:

在Kibana- 2上查看日志

7.参考

  • https://logz.io/learn/complete-guide-elk-stack/
  • https://howtodoinjava.com/microservices/elk-stack-tutorial-example/
  • https://dzone.com/articles/logging-with-elastic-stack

8.下载Eclipse项目

下载您可以在此处下载此示例的完整源代码: microservice

翻译自: https://www.javacodegeeks.com/2018/12/log-aggregation-using-elk-stack.html

elk 聚合日志

elk 聚合日志_使用ELK堆栈进行日志聚合相关推荐

  1. 服务器日志文件中包含堆栈跟踪,日志框架 Logback 官方手册(第三章:Configuration)...

    以下内容翻译整理自logback官方手册,地址:logback官方手册 logback 配置 将日志请求插入应用程序代码需要相当多的计划和工作.观察表明,大约有4%的代码用于日志记录.因此,即使是一个 ...

  2. 怎样用java编写日志_用JAVA写一个日志类程序以供大家学习

    中华网络安全联盟    作者:jacoo    来源:本站原创    时间:2006-4-18 说明: 尽管JAVA类库和其他工具提供了不少的纪录程序运行状态的日志类,我发觉也 不是万能的,有时需要根 ...

  3. 操作 mysql 不生成日志_详解MySQL的日志

    MySQL日志 MySQL的日志记录了MySQL的日常操作和错误信息,通过这些日志我们可以知道MySQL内部发生的事情,可以为MySQL的优化和管理提供必要的信息. MySQL的日志主要有:二进制日志 ...

  4. windows 防火墙日志_如何使用Windows防火墙日志跟踪防火墙活动

    windows 防火墙日志 In the process of filtering Internet traffic, all firewalls have some type of logging ...

  5. 思科关闭日志_关于思科交换机的日志配置总结

    (以下总结参考了思科 Release 12.2(46)SG版本说明书) 一.日志等级及格式 注:Level 数字越小越严重 格式列举,例如: *Nov 29 02:18:03.503 GMT: %DH ...

  6. 华为如何在开发者选项观察错误日志_爬虫scrapy框架--log日志输出配置及使用

    1.在配置文件中设置日志输出文件名和日志等级 1.为什么以日期为文件名? 因为这样可以方便开发者查看每天的日志信息,同时也可以防止单文件log日志信息堆积的越来越多,所以将当天日志信息保存到当天的日志 ...

  7. mysql下日志_浅谈mysql下日志记录

    1,如何查看mysql执行命令历史 #.mysql_history     此文件记录了在mysql中执行命令的历史 2,事务日志: transaction log:事务型存储引擎自行的管理和使用. ...

  8. navicat mysql 日志_使用Navicat查看MySQL日志的具体方法

    使用Navicat查看MySQL日志的具体方法 发布时间:2020-05-18 14:04:52 来源:51CTO 阅读:699 作者:三月 下文主要给大家带来使用Navicat查看MySQL日志的具 ...

  9. mysql重做日志_详解MySQL 重做日志(redo log)与回滚日志(undo logo)

    前言: 前面文章讲述了 MySQL 系统中常见的几种日志,其实还有事务相关日志 redo log 和 undo log 没有介绍.相对于其他几种日志而言, redo log 和 undo log 是更 ...

最新文章

  1. 特斯拉“纯视觉路线”能去掉ISP吗?
  2. linux搭建--DISPLAY环境变量问题(一)
  3. 北大邮箱收件服务器,北京大学微电子学研究院邮件系统
  4. 12、MySQL逻辑运算符
  5. html元素移动时颜色逐渐变深,css实现随鼠标移动div渐变色效果
  6. linux select读取节点数据失败_MySQL中覆盖索引查询和select*查询执行结果案例分析...
  7. 【Java心得总结六】Java容器中——Collection
  8. 11 Python之初识函数
  9. 前后端分离 常用工具汇总
  10. html块状元素和内联元素_HTML中的内联元素和块元素-解释
  11. php xml 实例教程,php解析xml方法实例详解,解析xml实例详解_PHP教程
  12. WCF分布式开发常见错误解决(1):添加服务引用出错
  13. 第二百一十六节,jQuery EasyUI,Spinner(微调)组件
  14. Linux内核空间与用户空间信息交互方法
  15. JSP指令:page指令,errorPage和isErrorPage
  16. php strictbool,PHP 7 Bool类型提示不起作用
  17. 半导体随机存储器(random access memory)
  18. openbravo erp介绍(一)
  19. 基于 HTML5 WebGL 的 3D 网络拓扑图
  20. 软件测试AI语音智能音响,什么是智能音箱_ai音箱都有什么功能 - 全文

热门文章

  1. 牛客题霸 [最长公共子串]C++题解/答案
  2. CF765F Souvenirs(势能线段树)
  3. P6672-[清华集训2016]你的生命已如风中残烛【结论】
  4. CF235C-Cyclical Quest【SAM】
  5. P4149-[IOI2011]Race【点分治】
  6. P6835-[Cnoi2020]线形生物【期望dp】
  7. P3338-[ZJOI2014]力【FFT】
  8. P4092-[HEOI2016/TJOI2016]树【线段树,倍增】
  9. 纪中A组模拟赛总结(2021.7.12)
  10. 【树链剖分】Disruption P(luogu 4374)