设为首页 加入收藏

TOP

flume 注册 windows 服务
2018-12-13 01:50:03 】 浏览:80
Tags:flume 注册 windows 服务

虽然生产环境下 flume 一般都在linux中运行,但是总会有些特殊情况要在 windows server 中使用 flume。但是楼主在 google、bing、baidu找了一通也没有比较好的方案,于是只能自已造轮子了。

思路:

既然 flume 是 java 项目,那么最终还是通过 java.exe 运行,那么只要找到这条命令,就有办法注册成服务。

分析:

先看 bin\flume-ng.cmd 文件,就一条命令:

powershell.exe -NoProfile -InputFormat none -ExecutionPolicy unrestricted -File %~dp0flume-ng.ps1 %*

可以看到 flume 是使用 window 的powershell.exe 命令行运行的,但是经过测试,注册成服务后不能启动powershell.exe 进程,个人感觉是权限问题,从事件查询器中可以看到 403、600 等错误代码,没有深究,回到使用 java.exe 运行的思路。

先写好配置文件与运行脚本,保证可以在命令行中正常启动flume进程。

flume 的启动脚本主要是在 bin\flume-ng.ps1 文件中,可以在文件中找到下面的函数:

function runFlume($javaClassPath, $javaLibraryPath, $javaOptions, $class, $javaProcessArgumentList)  {
  [string]$javaPath = GetJavaPath
  [string]$fullJavaCommand = "-classpath $javaClassPath -Djava.library.path=$javaLibraryPath $javaOptions $class $javaProcessArgumentList"
  if ($dryrun) {
    Write-Host 'Dry run mode enabled (will not actually initiate startup)'
    Write-Host "$javaPath $fullJavaCommand"
  } else {
    Write-Host "
  Running FLUME $command :
    class: $class
    arguments: $javaProcessArgumentList
    "

    $ErrorActionPreference = "Continue"
    $x = Start-Process $javaPath -ArgumentList "$fullJavaCommand" -Wait -NoNewWindow
  }
}

这部分就是运行 flume 进程的脚本,其中$javaPath $fullJavaCommand 就是我们要找的启动命令。

注意函数中有个 if...else,对照命令行提示,楼主运行agent时是进入到了 else 块,现在对这个函数修改下:

function runFlume($javaClassPath, $javaLibraryPath, $javaOptions, $class, $javaProcessArgumentList)  {
  [string]$javaPath = GetJavaPath
  [string]$fullJavaCommand = "-classpath $javaClassPath -Djava.library.path=$javaLibraryPath $javaOptions $class $javaProcessArgumentList"
  if ($dryrun) {
    Write-Host 'Dry run mode enabled (will not actually initiate startup)'
    Write-Host "$javaPath $fullJavaCommand"
  } else {
    Write-Host "
  Running FLUME $command :
    class: $class
    arguments: $javaProcessArgumentList
    fullJavaCommand: $fullJavaCommand       #增加这一行
    "

    $ErrorActionPreference = "Continue"
    $x = Start-Process $javaPath -ArgumentList "$fullJavaCommand" -Wait -NoNewWindow
  }
}

此时再运行flume,就可以看到命令行中输出了$fullJavaCommand 变量的内容,楼主的提示如下:

Running FLUME agent :
    class: org.apache.flume.node.Application
    arguments: -n udplog -f "D:\lianzt\flume\udp-log\udp-log.conf"
    fullJavaCommand: -classpath "D:\software\apache-flume-1.8.0-bin\conf";"D:\so
ftware\apache-flume-1.8.0-bin\lib\*";"D:\software\apache-flume-1.8.0-bin\conf" -
Djava.library.path=  org.apache.flume.node.Application -n udplog -f "D:\lianzt\f
lume\udp-log\udp-log.conf"

然后使用 java$fullJavaCommand 就可以看到 flume 正常启动了。

注册服务:

通常 java 项目都是用 wapper 注册服务,不过个人比较喜欢 nssm 工具,小巧、轻量、简单,可以把任意 exe 程序注册为系统服务。楼主经常使用这个工具把 node.js python java redis等程序注册成服务。

需注意,当exe程序启动多个进程,而且相互不为守护进程时,在服务窗口停止服务只会停止注册时配置的exe,比如windows下nginx 会启动一个主进程和多个 worker,如果使用nssm注册为服务,在服务窗口只能启动/停止 nginx 主进程,worker不会被杀死。

nssm下载地址:http://www.nssm.cc/download

只能在命令行中使用,输入nssm 会输出使用说明,注册 flume 的过程如下:

1.命令行中输入 nssm install,弹出配置窗口。

2.path中填写 java.exe 路径,startup directory 是项目运行的根目录,可权限需要填写,options中填写$fullJavaCommand 的内容。

3.输入 service name 点击确定即可。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇flume读取日志数据写入kafka &nbs.. 下一篇windows部署log4j输出日志给flume

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目