本篇文章大概4833字,阅读时间大约13分钟
产线环境上的Flink应用是长时运行的应用,日志量较大,通过flink web页面查看任务日志会导致浏览器卡死,通过日志排查问题非常不便。因此,需要将flink应用的日志发送到外部系统,方便进行日志检索
ELK全家桶是比较成熟的开源日志检索方案,flink日志搜集要做的就是将日志打到kafka,剩余的工作交由ELK完成即可。整个数据流向如下:
#========================================================================
# Job Conf
#========================================================================
env.java.opts: -Djob.name=FLINK日志DEMO
Flink-1.10.1中提供了log4j和logback的配置文件,默认情况下使用log4j,这里采用logback进行日志打点。
/**
* 获取flink应用的java环境变量传递的应用名称并添加到日志中
*
* @author Eights
*/
public class AppNameConvert extends ClassicConverter {
private static final String JOB_NAME = "job.name";
private static String appName = "应用默认名称";
//应用名称,这里就可以获取yarn application id, 运行机器之类的指标打到日志上
static {
String jobName = System.getProperty(JOB_NAME);
if (!StringUtils.isNullOrWhitespaceOnly(jobName)) {
appName = jobName;
}
}
@Override
public String convert(ILoggingEvent iLoggingEvent) {
return appName;
}
}
/**
* 自定义应用名称layout
* @author Eights
*/
public class AppNameLayOut extends PatternLayout {
static {
defaultConverterMap.put("app", AppNameConvert.class.getName());
}
}
https://github.com/danielwegener/logback-kafka-appender
<configuration>
<property name="LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %app [%thread] %-5level %logger{60} %X{sourceThread} - %msg%n"/>
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log.file}</file>
<append>false</append>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.eights.carbond.common.logconver.AppNameLayOut">
<pattern>${LOG_PATTERN}</pattern>
</layout>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.eights.carbond.common.logconver.AppNameLayOut">
<pattern>${LOG_PATTERN}</pattern>
</layout>
<charset>UTF-8</charset>
</encoder>
<topic>flink-app-logs</topic>
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/>
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>
<producerConfig>bootstrap.servers=dn2.eights.com:9092,dn3.eights.com:9092,dn4.eights.com:9092</producerConfig>
<producerConfig>retries=3</producerConfig>
<producerConfig>acks=1</producerConfig>
<producerConfig>batch-size=16384</producerConfig>
<producerConfig>buffer-memory=33554432</producerConfig>
<producerConfig>properties.max.request.size==2097152</producerConfig>
<producerConfig>linger.ms=1000</producerConfig>
<producerConfig>max.block.ms=0</producerConfig>
</appender>
<!-- This affects logging for both user code and Flink -->
<root level="INFO">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</root>
<!-- Uncomment this if you want to only change Flink's logging -->
<!--<logger name="org.apache.flink" level="INFO">-->
<!--<appender-ref ref="file"/>-->
<!--</logger>-->
<!-- The following lines keep the log level of common libraries/connectors on
log level INFO. The root logger does not override this. You have to manually
change the log levels here. -->
<logger name="akka" level="INFO">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</logger>
<logger name="org.apache.kafka" level="INFO">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</logger>
<logger name="org.apache.hadoop" level="INFO">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</logger>
<logger name="org.apache.zookeeper" level="INFO">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</logger>
<!-- Suppress the irrelevant (wrong) warnings from the Netty channel handler -->
<logger name="org.apache.flink.shaded.akka.org.jboss.netty.channel.DefaultChannelPipeline" level="ERROR">
<appender-ref ref="file"/>
<appender-ref ref="kafkaAppender"/>
</logger>
</configuration>
编写一个简单的flink-demo应用,在窗口的apply方法中打一波日志
编译打包,运行一下命令提交到yarn集群,采用yarn per job模式
flink run -m yarn-cluster -yd -ytm 2g -ys 4 -yjm 2g -ynm flink-demo测试 \
-c com.eights.carbond.streaming.sensor.AverageSensorReadings -j \
./flink-demo-1.0-SNAPSHOT.jar
在flink的web ui上可以查看自定义的业务名称
消费kafka的topic命令如下:
kafka-console-consumer --bootstrap-server dn2.eights.com:9092 \
--topic flink-app-logs --from-beginning --group eights \
--consumer-property enable.auto.commit=false
可以发现自定义的Flink业务应用名称已经打到了日志上,kafka中的日志显示正常,flink应用日志发送到kafka测试完成。