设为首页 收藏本站
查看: 646|回复: 0

[经验分享] red5 整合到tomcat中(一)

[复制链接]

尚未签到

发表于 2017-1-31 08:59:30 | 显示全部楼层 |阅读模式
  今天忙了一天,目的很简单,就是把red5整合到tomcat中,通过简单访问可以请求到数据。
  下面简单记述一下过程:
  1,下载需要的red5.red5官网http://www.red5.org/,下载1.0.rec里的两个zip包,如图。
  
DSC0000.bmp
 2,将下载的包解压后,将red5-war-1.0-RC1包里的ROOT.war放到tomcat的webapps下。将原来的ROOT文件夹改名为ROOT_t,便于区别,启动tomcat。当启动完毕后就出现一个新的ROOt文件夹。该文件夹备用。
  3,在Eclipse中新建项目。我这里命名为chapter2,然后到ROOT文件夹下 WEB-INF下面拷贝lib目录到相同位置,并拷贝文件适当修改。还要拷贝red5.jar.下面先修改web.xml.具体如下

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>chapter2</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/chapter2</param-value>
</context-param>
<context-param>
<param-name>globalScope</param-name>
<param-value>default</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>default.context</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/*-web.xml</param-value>
</context-param>
<listener>
<listener-class>org.red5.server.war.WarLoaderServlet</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>gateway</servlet-name>
<servlet-class>org.red5.server.net.servlet.AMFGatewayServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>rtmpt</servlet-name>
<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gateway</servlet-name>
<url-pattern>/gateway</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/fcs/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/open/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/idle/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/send/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/close/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/WEB-INF/*</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/persistence/*</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/streams/*</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
</web-app>

  4,进入Root/WEB-INF/classes文件夹里,拷贝所有配置文件,如下.
  
DSC0001.png
 修改root-web.xml。具体内容如下,
  
 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<!-- ROOT web context -->
<bean id="web.context.chapter2" class="org.red5.server.Context">
<property name="scopeResolver" ref="red5.scopeResolver" />
<property name="clientRegistry" ref="global.clientRegistry" />
<property name="serviceInvoker" ref="global.serviceInvoker" />
<property name="mappingStrategy" ref="global.mappingStrategy" />
</bean>
<bean id="web.scope.chapter2" class="org.red5.server.WebScope" init-method="register">
<property name="server" ref="red5.server" />
<property name="parent" ref="global.scope" />
<property name="context" ref="web.context.chapter2" />
<property name="handler" ref="web.handler.chapter2" />
<property name="contextPath" value="${webapp.contextPath}" />
<property name="virtualHosts" value="${webapp.virtualHosts}" />
</bean>
<bean id="web.handler.chapter2"     
class="first.Application" singleton="true" />
<bean id="flv.service"
class="chapter7.StreamService" singleton="true"> </bean>
</beans>

  注意此处的chapter2,是我们的项目名称。red5-web.properties放置在web-info下,内容是

webapp.contextPath=/chapter2
webapp.virtualHosts=localhost,localhost:8088,127.0.0.1:8088
  5,修改red5-core.xml 。
  具体如下:

<!-- RTMP Mina Transport -->
<bean id="rtmpTransport" class="org.red5.server.net.rtmp.RTMPMinaTransport" init-method="start" destroy-method="stop">
<property name="ioHandler" ref="rtmpMinaIoHandler" />
<property name="connectors">
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg index="0" type="java.lang.String" value="127.0.0.1" />  
<constructor-arg index="1" type="int" value="1935" />  
</bean>
<!-- You can now add additional ports and ip addresses
<bean class="java.net.InetSocketAddress">
<constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  
<constructor-arg index="1" type="int" value="1936" />  
</bean>
-->
</list>
</property>
<property name="receiveBufferSize" value="65536" />
<property name="sendBufferSize" value="271360" />
<property name="connectionThreads" value="4" />
<property name="ioThreads" value="16" />
<!-- This is the interval at which the sessions are polled for stats. If mina monitoring is not
enabled, polling will not occur. -->
<property name="jmxPollInterval" value="1000" />
<property name="tcpNoDelay" value="true" />
</bean>
  注意: tomcat里面添加太多的red5服务,会造成tomcat内存泄露,这么时候就要修改tomcat的启动内存bin/catalina.bat
  Win: 
  set JAVA_OPTS=%JAVA_OPTS% -Xms1024m -Xmx1024m
  linux:
  JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxNewSize=512m -XX:MaxPermSize=512m"
  如果要修改webapps下的项目启动的优先级,就在  conf\Catalina\localhost  加上一个 项目名.xml 的文件
  文件内容:

<?xml version="1.0" encoding="UTF-8"?>  
<!--   
Licensed to the Apache Software Foundation (ASF) under one or more   
contributor license agreements.  See the NOTICE file distributed with   
this work for additional information regarding copyright ownership.   
The ASF licenses this file to You under the Apache License, Version 2.0   
(the "License"); you may not use this file except in compliance with   
the License.  You may obtain a copy of the License at   
http://www.apache.org/licenses/LICENSE-2.0   
Unless required by applicable law or agreed to in writing, software   
distributed under the License is distributed on an "AS IS" BASIS,   
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
See the License for the specific language governing permissions and   
limitations under the License.   
-->  
<Context antiResourceLocking="false" privileged="true" />  
  6 然后我们做相应的响应代码。

package first;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;

public class Application extends ApplicationAdapter{
public boolean appConnect(IConnection conn,Object[] args)
{
System.out.println("链接");
return true;
}
public String change(String str){
System.out.println("客户端调用服务器端方法");
return str.toUpperCase();
}
}


package chapter7;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.springframework.core.io.Resource;
public class StreamService {
/**
* Getter for property 'listOfAvailableFLVs'.
*
* @return Value for property 'listOfAvailableFLVs'.
*/
public Map getListOfAvailableFLVs() {
IScope scope = Red5.getConnectionLocal().getScope();
Map<String, Map> filesMap = new HashMap<String, Map>();
Map<String, Object> fileInfo;
try {
Resource[] flvs = scope.getResources("streams/*.flv");
if (flvs != null) {
for (Resource flv : flvs) {
File file = flv.getFile();
Date lastModifiedDate = new Date(file.lastModified());
String lastModified = formatDate(lastModifiedDate);
String flvName = flv.getFile().getName();
String flvBytes = Long.toString(file.length());
fileInfo = new HashMap<String, Object>();
fileInfo.put("name", flvName);
fileInfo.put("lastModified", lastModified);
fileInfo.put("size", flvBytes);
filesMap.put(flvName, fileInfo);
}
}
Resource[] mp3s = scope.getResources("streams/*.mp3");
if (mp3s != null) {
for (Resource mp3 : mp3s) {
File file = mp3.getFile();
Date lastModifiedDate = new Date(file.lastModified());
String lastModified = formatDate(lastModifiedDate);
String flvName = mp3.getFile().getName();
String flvBytes = Long.toString(file.length());
fileInfo = new HashMap<String, Object>();
fileInfo.put("name", flvName);
fileInfo.put("lastModified", lastModified);
fileInfo.put("size", flvBytes);
filesMap.put(flvName, fileInfo);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return filesMap;
}
private String formatDate(Date date) {
SimpleDateFormat formatter;
String pattern = "dd/MM/yy H:mm:ss";
Locale locale = new Locale("en", "US");
formatter = new SimpleDateFormat(pattern, locale);
return formatter.format(date);
}
}
  red5 整合到tomcat中(二)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-335565-1-1.html 上篇帖子: tomcat项目移植到weblogic问题 下篇帖子: 查看TOMCAT内存使用情况(总结)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表