目前有一个需求,就是Flume能够做为一个相似于tomcat的服务器,能够经过post请求进行访问,而且路径须要:ip:port/contextPath格式。java
通过一些资料获悉,httpSource只是httpSource的一个玩具工具,能够说毛坯版,目前仅仅支持的是按照ip:port访问,并不具有servlet这种功能。tomcat
那么打开源码看一下:服务器
这上面即是httpsource源码了,能够看到主要是5个类:HTTPBadRequestException,HTTPSource,HTTPSourceConfigurationConstants,HTTPSourceHandler,JSONHandler。工具
分别的做用是:post
HTTPBadRequestException:定义一些http异常,经常使用的好比404。blog
HTTPSourceConfigurationConstants:主要定义一些source的常量,来自于配置文件。好比:port,host等等接口
HTTPSourceHandler:一个接口,做为handler模板。ip
JSONHandler:提供的默认实现Handler,选择是[“header”:,"body":]这种格式,笔者对此很不习惯,其实里面提供了好几种event模式,不知道为嘛要选择这种。ssl
HTTPSource:这个就是主类了,里面有相似于main方法的start方法。get
其实本质上httpSource就是一个嵌入了jetty的服务器,经过接受post请求(目前写死了只处理post请求)。将数据转换为event往下发。因此,修改很简单。
你只须要在HTTPSourceConfigurationConstants添加一个contextPath参数:
public static final String CONTEXT_PATH = "contextPath";
而后跳到HTTPSource类,
在前面的声明中加上:
private volatile String contextPath;
configure方法里面加上获取这个path:
contextPath = context.getString(HTTPSourceConfigurationConstants.CONTEXT_PATH);
接下来就是在jetty服务器加上路径啦,能够看到这部分代码:
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加上配置的路径,咱们就能够根据路径来访问:
root.addServlet(new ServletHolder(new FlumeHTTPServlet()), "/"+contextPath);
好了,大功告成。