微服务日志之Spring Boot Kafka实现日志收集
前言
承接上文( 微服务日志之.NET Core使用NLog通过Kafka实现日志收集 https://www.cnblogs.com/maxzhang1985/p/9522017.html ).NET/Core的实现,我们的目地是为了让微服务环境中dotnet和java的服务都统一的进行日志收集。
Java体系下Spring Boot + Logback很容易就接入了Kafka实现了日志收集。


Spring Boot集成
Maven 包管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
包依赖引用:
<dependency>
<groupId>com.github.danielwegener</groupId>
<artifactId>logback-kafka-appender</artifactId>
<version>0.2.0-RC1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.0</version>
</dependency>
logback-spring.xml
在Spring Boot项目resources目录下添加logback-spring.xml配置文件,注意:一定要修改 {"appname":"webdemo"},这个值也可以在配置中设置为变量。添加如下配置,STDOUT是在连接失败时,使用的日志输出配置。所以这每个项目要根据自己的情况添加配置。在普通日志输出中使用异步策略提高性能,内容如下:
<appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" >
<customFields>{"appname":"webdemo"}</customFields>
<includeMdc>true</includeMdc>
<includeContext>true</includeContext>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</encoder>
<topic>loges</topic>
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.HostNameKeyingStrategy" />
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
<producerConfig>bootstrap.servers=127.0.0.1:9092</producerConfig>
<!-- don't wait for a broker to ack the reception of a batch. -->
<producerConfig>acks=0</producerConfig>
<!-- wait up to 1000ms and collect log messages before sending them as a batch -->
<producerConfig>linger.ms=1000</producerConfig>
<!-- even if the producer buffer runs full, do not block the application but start to drop messages -->
<!--<producerConfig>max.block.ms=0</producerConfig>-->
<producerConfig>block.on.buffer.full=false</producerConfig>
<!-- kafka连接失败后,使用下面配置进行日志输出 -->
<appender-ref ref="STDOUT" />
</appender>
注意:一定要修改 {"appname":"webdemo"} , 这个值也可以在配置中设置为变量 。对于第三方框架或库的错误和异常信息如需要写入日志,错误配置如下:
<appender name="kafkaAppenderERROR" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" >
<customFields>{"appname":"webdemo"}</customFields>
<includeMdc>true</includeMdc>
<includeContext>true</includeContext>
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<rootCauseFirst>true</rootCauseFirst>
</throwableConverter>
</encoder>
<topic>ep_component_log</topic>
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.HostNameKeyingStrategy" />
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.BlockingDeliveryStrategy">
<!-- wait indefinitely until the kafka producer was able to send the message -->
<timeout>0</timeout>
</deliveryStrategy>
<producerConfig>bootstrap.servers=127.0.0.1:9020</producerConfig>
<!-- don't wait for a broker to ack the reception of a batch. -->
<producerConfig>acks=0</producerConfig>
<!-- wait up to 1000ms and collect log messages before sending them as a batch -->
<producerConfig>linger.ms=1000</producerConfig>
<!-- even if the producer buffer runs full, do not block the application but start to drop messages -->
<producerConfig>max.block.ms=0</producerConfig>
<appender-ref ref="STDOUT" />
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
在异常日志用使用了同步策略保证,错误日志的有效收集,当然可以根据实际项目情况进行配置。
LOG配置建议:
日志root指定错误即可输出第三方框架异常日志:
<root level="INFO">
<appender-ref ref="kafkaAppenderERROR" />
</root>
建议只输出自己程序里的级别日志配置如下(只供参考):
<logger name="项目所在包" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="kafkaAppender" />
</logger>
最后
GitHub:https://github.com/maxzhang1985/YOYOFx 如果觉还可以请Star下, 欢迎一起交流。
.NET Core 开源学习群:214741894
微服务日志之Spring Boot Kafka实现日志收集的更多相关文章
- 第二章 微服务构建:Spring Boot
此处介绍Spring Boot的目的除了它是Spring Cloud的基础外,也由于其自身的各项优点,如自动化配置.快速开发.轻松部署等,非常适合用作微服务架构中各项具体微服务的开发框架. 本章内容: ...
- 微服务构建: Spring Boot
在展开 Spring Cloud 的微服务架构部署之前, 我们先了解一下用于构建微服务的基础框架-Spring Boot. 由于 Spring Cloud 的构建基于 Spring Boot 实现, ...
- Spring Boot (九): 微服务应用监控 Spring Boot Actuator 详解
1. 引言 在当前的微服务架构方式下,我们会有很多的服务部署在不同的机器上,相互是通过服务调用的方式进行交互,一个完整的业务流程中间会经过很多个微服务的处理和传递,那么,如何能知道每个服务的健康状况就 ...
- 手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud
文章首发于今日头条:https://www.toutiao.com/i6712696637623370248/ 直接进入主题 Dubbo (答案领取方式见侧边栏) Dubbo 中 中 zookeepe ...
- 微服务架构之spring boot admin
Spring boot admin是可视化的监控组件,依赖spring boot actuator收集各个服务的运行信息,通过spring boot actuator可以非常方便的查看每个微服务的He ...
- SpringCloud 微服务一:spring boot 基础项目搭建
spring cloud是建立在spring boot的基础上的,而之前虽然听说过,也随便看了一下spring boot,却没有真正使用,因此还必须先花时间学一下spring boot. spring ...
- 微服务学习笔记——Spring Boot特性
1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标,健康检查和外部配置 6. 开 ...
- 微服务架构-选择Spring Cloud,放弃Dubbo
Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经走了一年多. 在使用 Spring Cloud 之前,我们对微服务实践是没有太多的体会和经验的.从 ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
随机推荐
- 【学习】DataFrame&Series类【pandas】
参考链接:http://blog.csdn.net/yhb315279058/article/details/50226027 DataFrame类: DataFrame有四个重要的属性: index ...
- 一个ipv4到ipv6的移植问题
之前在使用ipv4的时候,有一个模块是使用raw socket来发包,它使用的一个option是:IP_HDRINCL. 如果设置了IP_HDRINCL选项,则raw会绕过source validat ...
- 使用swiper插件,隐藏swiper后再显示,不会触发自动播放的解决办法
问题: 项目中有一个需求,当点击P1时,两个页面进行轮播.当点击P2时,页面不轮播. 设置好以后,点击P2,再点击P1,此时页面不能自动轮播,只能手动触发. 解决: 在轮播器配置里,配置observe ...
- 在IDEA中停止和关闭SonarLint自动检查,手动运行SonarLint检查代码
关闭SonarLint自动检查代码 有时敲一行代码SonarLint插件就会自动检查,让人感觉很不舒服,还会使电脑卡顿: 依次点击:File -> Settings 或直接Ctrl+Alt+S ...
- Codeforces Round #436 A. Fair Game
题意:给你n张卡片,上面写有数字,两个人选择两个数字,把相同数字的卡片都拿走,问能不能拿走所有的卡片并且两个人拿的卡片书相同. Examples Input 411272711 Output YES1 ...
- Setting up Scatter for Web Applications
[Setting up Scatter for Web Applications] If you are still using scatter-js please move over to scat ...
- 加NONCLUSTERED INDEX索引,在ON了之后还要INCLUDE
之前加了索引,但效果不大 SET STATISTICS TIME ON --执行时间 SET STATISTICS IO ON --IO读取 DBCC DROPCLEANBUFFERS --清除缓冲区 ...
- Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)
原文地址:https://wzfou.com/php-fpm/ 最近发现博客的内存老是隔三差五地被“吃掉”了,登录到后台后偶尔会出卡顿的情况,一开始怀疑是Swap不够导致的,于是给VPS主机增加了几个 ...
- Python中使用RabbitMQ
一 RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息 ...
- 以太坊虚拟机EVM 和EOS 虚拟机的劣势!
EVM: 01 智能合约设计层面 缺乏标准库支持:EVM缺少完善的标准库支持,甚至最基本的string类型支持,在EVM中都很鸡肋,例如字符串拼接.切割.查找等等都需要开发者自己实现.带来的后果就是 ...