让你的 wowza server提供 RESTful web 服务

        有时我们 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 例如以下:
新建 maven 项目
        新建项目的 debug 调试效果:
新建项目的 debug 调试效果图
        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 已成功部署:
defonds-server-module 已成功部署
        5. 訪问接口
        debug 启动 defonds-server-module,然后在浏览器訪问 http://localhost:1935/monitor?id=9527&name=defonds
        发现返回的是
Wowza Streaming Engine 4 Trial Edition
monitor返回版本号信息
        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:
        这次是返回的我们想要的信息:
monitor返回正确信息
        Eclipse 控制台有 log 输出了:
DEBUG server comment - ServerMonitorHttpInterface--http--request--id=9527;name=defonds
        成功了。

嗯,就这些。就是这么简单:)本文演示样例源代码已上传 CSDN,有兴趣的朋友能够去这里下载:http://download.csdn.net/detail/defonds/7493981


        參考资料

posted @ 2017-05-01 09:49  yxysuanfa  阅读(594)  评论(0编辑  收藏  举报