有时我们 nginx 需要和 wowza 服务器交互以进行一些 LB 事宜;有时我们的管理员需要实时了解 wowza 服务器的一些其他状态信息(比如一些自定义对象的状态等等),而用 rtmp 不是太方便。这些情况下,如果我们的 wowza 服务器能提供一个 http api 就好了,就像 web 容器 tomcat 那样。本文以一个简单的 http 发送参数到 wowza,然后 wowza 返回一个 json 为例(经典的 REST 案例),介绍如何让你的 wowza 服务器提供 RESTful web 调用接口。


        本文是在《​​取代 Ant:使用 Maven 管理 Wowza 插件开发​​》例子的基础上进一步进行研发,没有使用 wowza 官方的 Eclipse 插件(官方对 wowza 项目的管理使用的是 ant)。


       

1. 新建 maven 项目

        参考《​​取代 Ant:使用 Maven 管理 Wowza 插件开发​​》步骤。新建的 maven 项目 defonds-server-module 如下:


让你的 wowza 服务器提供 RESTful web 服务_json


        新建项目的 debug 调试效果:


让你的 wowza 服务器提供 RESTful web 服务_ide_02


       

2. 编写 HTTProvider2Base 子类

package com.defonds.wms.module.server;

import java.io.IOException;
import java.io.OutputStream;

import com.wowza.wms.http.HTTProvider2Base;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.IVHost;

public class ServerMonitorHttpInterface extends HTTProvider2Base {
private static final WMSLogger logger = WMSLoggerFactory.getInstance().getLoggerObj(ServerMonitorHttpInterface.class.getName());

@Override
public void onHTTPRequest(IVHost ivHost, IHTTPRequest request, IHTTPResponse response) {
String id = request.getParameter("id");
String name = request.getParameter("name");
logger.debug("ServerMonitorHttpInterface--http--request--id=" + id + ";name=" + name);

// TODO do your own business logic here

String jsonObject = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
response.setHeader("Content-Type", "application/json");
// Get the printwriter object from response to write the required json object to the output stream
OutputStream out = response.getOutputStream();
try {
out.write(jsonObject.getBytes());
out.flush();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}

}

       

3. 编辑 VHost.xml

        编辑 %wowza%/conf/VHost.xml,添加一个 HTTPProvider:


<HTTPProvider>
<BaseClass>com.defonds.wms.module.server.ServerMonitorHttpInterface</BaseClass>
<RequestFilters>monitor*</RequestFilters>
<AuthenticationMethod>none</AuthenticationMethod>
</HTTPProvider>


       

4. 项目重新打包部署

        命令行切换到你的 defonds-server-module 项目目录下,执行


mvn package

        检查 %wowza%/lib 目录,发现 defonds-server-module 已成功部署:


让你的 wowza 服务器提供 RESTful web 服务_java_03


       

5. 访问接口

        debug 启动 defonds-server-module,然后在浏览器访问 http://localhost:1935/monitor?id=9527&name=defonds


        发现返回的是


Wowza Streaming Engine 4 Trial Edition


让你的 wowza 服务器提供 RESTful web 服务_ide_04


        Eclipse 控制台也没有 ServerMonitorHttpInterface 本应该输出的 log 日志。


        这是因为 com.wowza.wms.http.HTTPServerVersion 这个 HTTPProvider 把请求拦截了:


<HTTPProvider>
<BaseClass>com.wowza.wms.http.HTTPServerVersion</BaseClass>
<RequestFilters>*</RequestFilters>
<AuthenticationMethod>none</AuthenticationMethod>
</HTTPProvider>


        因为它的配置是 *。可以改为其他,或者将其注释掉。就可以了。


        注释掉 HTTPServerVersion 之后,重新启动 defonds-server-module,然后访问 http://localhost:1935/monitor?id=9527&name=defonds:


        这次是返回的我们想要的信息:


让你的 wowza 服务器提供 RESTful web 服务_json_05


        Eclipse 控制台有 log 输出了:


DEBUG server comment - ServerMonitorHttpInterface--http--request--id=9527;name=defonds


     

       

参考资料