有时我们 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 如下:
新建项目的 debug 调试效果:
2. 编写 HTTProvider2Base 子类
3. 编辑 VHost.xml
编辑 %wowza%/conf/VHost.xml,添加一个 HTTPProvider:
4. 项目重新打包部署
命令行切换到你的 defonds-server-module 项目目录下,执行
mvn package
检查 %wowza%/lib 目录,发现 defonds-server-module 已成功部署:
5. 访问接口
debug 启动 defonds-server-module,然后在浏览器访问 http://localhost:1935/monitor?id=9527&name=defonds
发现返回的是
Wowza Streaming Engine 4 Trial Edition
Eclipse 控制台也没有 ServerMonitorHttpInterface 本应该输出的 log 日志。
这是因为 com.wowza.wms.http.HTTPServerVersion 这个 HTTPProvider 把请求拦截了:
因为它的配置是 *。可以改为其他,或者将其注释掉。就可以了。
注释掉 HTTPServerVersion 之后,重新启动 defonds-server-module,然后访问 http://localhost:1935/monitor?id=9527&name=defonds:
这次是返回的我们想要的信息:
Eclipse 控制台有 log 输出了:
DEBUG server comment - ServerMonitorHttpInterface--http--request--id=9527;name=defonds
参考资料
- http://stackoverflow.com/questions/10410854/call-a-wowza-server-from-android-app
- http://stackoverflow.com/questions/2010990/how-do-you-return-a-json-object-from-a-java-servlet
- http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/