修改flume源码,使其HTTPSource具备访问路径功能
目前有一个需求,就是Flume可以作为一个类似于tomcat的服务器,可以通过post请求进行访问,并且路径需要:ip:port/contextPath格式。
经过一些资料获悉,httpSource只是httpSource的一个玩具工具,可以说毛坯版,目前仅仅支持的是按照ip:port访问,并不具备servlet这种功能。
那么打开源码看一下:
这上面便是httpsource源码了,可以看到主要是5个类:HTTPBadRequestException,HTTPSource,HTTPSourceConfigurationConstants,HTTPSourceHandler,JSONHandler。
分别的作用是:
HTTPBadRequestException:定义一些http异常,常用的比如404。
HTTPSourceConfigurationConstants:主要定义一些source的常量,来自于配置文件。比如:port,host等等
HTTPSourceHandler:一个接口,作为handler模板。
JSONHandler:提供的默认实现Handler,选择是[“header”:,"body":]这种格式,笔者对此很不习惯,其实里面提供了好几种event模式,不知道为嘛要选择这种。
HTTPSource:这个就是主类了,里面有类似于main方法的start方法。
其实本质上httpSource就是一个嵌入了jetty的服务器,通过接受post请求(目前写死了只处理post请求)。将数据转换为event往下发。所以,修改很简单。
你只需要在HTTPSourceConfigurationConstants添加一个contextPath参数:
1 | public static final String CONTEXT_PATH = "contextPath" ; |
然后跳到HTTPSource类,
在前面的声明中加上:
1 | private volatile String contextPath; |
configure方法里面加上获取这个path:
1 | contextPath = context.getString(HTTPSourceConfigurationConstants.CONTEXT_PATH); |
接下来就是在jetty服务器加上路径啦,可以看到这部分代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public void start() { Preconditions.checkState(srv == null , "Running HTTP Server found in source: " + getName() + " before I started one." + "Will not attempt to start." ); srv = new Server(); // Connector Array Connector[] connectors = new Connector[ 1 ]; if (sslEnabled) { SslSocketConnector sslSocketConnector = new HTTPSourceSocketConnector(excludedProtocols); sslSocketConnector.setKeystore(keyStorePath); sslSocketConnector.setKeyPassword(keyStorePassword); sslSocketConnector.setReuseAddress( true ); connectors[ 0 ] = sslSocketConnector; } else { SelectChannelConnector connector = new SelectChannelConnector(); connector.setReuseAddress( true ); connectors[ 0 ] = connector; } connectors[ 0 ].setHost(host); connectors[ 0 ].setPort(port); srv.setConnectors(connectors); try { org.mortbay.jetty.servlet.Context root = new org.mortbay.jetty.servlet.Context( srv, "/" , org.mortbay.jetty.servlet.Context.SESSIONS); root.addServlet( new ServletHolder( new FlumeHTTPServlet()), "/" ); HTTPServerConstraintUtil.enforceConstraints(root); srv.start(); Preconditions.checkArgument(srv.getHandler().equals(root)); } catch (Exception ex) { LOG.error( "Error while starting HTTPSource. Exception follows." , ex); Throwables.propagate(ex); } Preconditions.checkArgument(srv.isRunning()); sourceCounter.start(); super .start(); } |
只需要在root.addServlet加上配置的路径,我们就可以根据路径来访问:
1 | root.addServlet( new ServletHolder( new FlumeHTTPServlet()), "/" +contextPath); |
好了,大功告成。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!