Flume-ng spoolDir目录监控踩过的坑

来源:互联网 发布:3ds max模型优化 编辑:程序博客网 时间:2024/05/22 09:40

参考此Blog,转载请申明出处。

使用Flume-ng1.6 spoolDir收集日志遇到的问题

  • 文件解码
    不能正确的重命名文件,抛出bug后,之后所有文件都不可以被flume收集,是一个比较严重的错,引起原因是flume使用NIO方式读取文件,将读取的文件以UTF-8的编码读取,在Linux状态下,默认是按照GBK编码方式存储文件,所以读取时就会遇到字符长度不够问题,解决办法:在flume配置文件中设置监控目录中读取文件的编码方式。

  • copy大文件抛出异常因为copy到spoolDir下的文件不可以被修改所致,解决方案:

    • 使用scp或者cp备份文件,然后将文件mv进spoolDir下。还可以使用后缀名正则配合,先拷贝进去.tmp的文件,等待copy完成后,rename文件名。

    • 修改源代码
      增加文件拷贝校验
      这里写图片描述

/**  *  * @Title: checkFileCpIsOver  * @Description: 用来检查文件拷贝是否完成  * @param @param currentFile    设定文件  * @return void    返回类型  * @throws  */private void checkFileCpIsOver(File file) {    logger.info("CheckFileCpIsOVer start..........");    long modified = file.lastModified();//目前文件的修改时间    long length = file.length();//目前文件的大小    try {        Thread.sleep(1000);//等待1秒钟    } catch (InterruptedException e) {        e.printStackTrace();    }    File currentFile = new File(file.getAbsolutePath());    int count = 0;//记录循环次数,超过20次,也就是10秒后抛出异常    while(currentFile.lastModified() != modified || currentFile.length() != length) {        if(count > 20) {            String message = "File Copy time too long. please check copy whether exception!" + "\n"                    + "File at :" + file.getAbsolutePath() + "\n"                    + "File current length is:" + currentFile.lastModified();            new IllegalStateException(message);        }        count++;        modified = currentFile.lastModified();        length = currentFile.length();        try {            Thread.sleep(500);//等待500毫秒        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        currentFile = new File(file.getAbsolutePath());    }    //一直到文件传输完成就可以退出}
0 0
原创粉丝点击