Flume-ng spoolDir分布式日志收集目录监控

来源:互联网 发布:js不区分大小写比较 编辑:程序博客网 时间:2024/05/29 07:14

ok,上来直接来干货  

在使用flume-ng时,踩了很多坑,现在来说一下,希望大家绕过坑,到达熟练使用flume的目的


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


第二坑:cp大文件抛出异常,这个是因为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());        }        //一直到文件传输完成就可以退出    }

修改之后打成jar包上传到flume的lib下替换原有jar即可

1 0
原创粉丝点击