由于RED5本身就基于spring的,所以在其上结合spring使用rmi是很简单的!
由于red5中已经存在spring的以下一些包,此文章是基本red5版本 : 1.0.0-rc
, 要使用rmi需要加入以下两个包:
加包时请根据red5版本选择正确的包(不要问我哪个版本对应哪些包,自己试吧)
ok ,准备工作完成
先写客户端和服务都需要的接口吧:
package org.freechat.chatserver.rmi; public interface TestRMI { public void test(); }
package org.freechat.chatserver.rmi; public class TestRMIImple implements TestRMI { @Override public void test() { System.out.println("rmi调用成功"); } }
添加以下spring配置:
spring-rmi-exporter.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="testBean" class="org.freechat.chatserver.TestBean"></bean> <bean id="testRMI" class="org.freechat.chatserver.rmi.TestRMIImple"> </bean> <bean id="balanceChessGameServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service"> <ref bean="testRMI" /> </property> <property name="serviceName"> <value>testRMI</value> </property> <property name="serviceInterface"> <value>org.freechat.chatserver.rmi.TestRMI </value> </property> <property name="registryPort"> <value>1199</value> </property> <property name="servicePort"> <value>2899</value> </property> </bean> </beans>
在red5配置文件red5-web.xml中引入以上配置,即red5-web.xml中添加
<import resource="classpath:/spring-bean.xml" />
将应用部署到red5下,启动red5将看到以下信息:
ok, rmi注册成功..
接下来写客户端测试下:
首先配置远程rmi:
rmi.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="testRed5RMI" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl"> <value>rmi://192.168.1.16:1199/testRMI</value> </property> <property name="serviceInterface"> <value>org.freechat.chatserver.rmi.TestRMI</value> </property> </bean> </beans>
import org.freechat.chatserver.rmi.TestRMI; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("rmi.xml"); TestRMI testRMI = (TestRMI) context.getBean("testRed5RMI"); testRMI.test(); } }
rmi调用成功
注意 事项:
rmi注册时默认注册到hostname : 0.0.0.0上, 本机调用肯定没问题, 要是客户端在不同机器上,或服务端多ip时,客户端调用可能会有以下错误
java.rmi.ConnectException: Connection refused to host: 0.0.0.0;
这时应该在rmi注册前设置 系统变量:
System.setProperty("java.rmi.server.hostname", "192.168.1.212");指明注册的hostname
在red5中, 可在application启动时设置 ,即 start方法中加入以上语句;
网上也有说 在org.springframework.remoting.rmi.RmiServiceExporter暴露服务时,指定registryHost,即设置属性:
<bean id="balanceChessGameServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service"> <ref bean="testRMI" /> </property> <property name="serviceName"> <value>testRMI</value> </property> <property name="serviceInterface"> <value>org.freechat.chatserver.rmi.TestRMI </value> </property> <property name="registryPort"> <value>1199</value> </property> <property name="servicePort"> <value>2899</value> </property> <property name="registerHost"> <value>192.168.1.212</value> </property> </bean>但是我试了n次, 连注册时都报错了,希望高手指点,错误如下:
er' defined in class path resource [spring-bean.xml]: Invocation of init method failed; nested exception is java.rmi.ConnectException: Connection refused to hos t: 192.168.1.212; nested exception is: java.net.ConnectException: Connection refused: connect at org.springframework.beans.factory.support.AbstractAutowireCapableBean Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBean Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBean Factory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb ject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr y.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe an(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory. preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finish BeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refres h(AbstractApplicationContext.java:425) at org.red5.server.tomcat.TomcatLoader$2.run(TomcatLoader.java:774) Caused by: java.rmi.ConnectException: Connection refused to host: 192.168.1.212; nested exception is: java.net.ConnectException: Connection refused: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198 ) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322) at sun.rmi.registry.RegistryImpl_Stub.list(Unknown Source) at org.springframework.remoting.rmi.RmiServiceExporter.testRegistry(RmiS erviceExporter.java:420) at org.springframework.remoting.rmi.RmiServiceExporter.getRegistry(RmiSe rviceExporter.java:331) at org.springframework.remoting.rmi.RmiServiceExporter.prepare(RmiServic eExporter.java:268) at org.springframework.remoting.rmi.RmiServiceExporter.afterPropertiesSe t(RmiServiceExporter.java:229) at org.springframework.beans.factory.support.AbstractAutowireCapableBean Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) at org.springframework.beans.factory.support.AbstractAutowireCapableBean Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) ... 10 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:519) at java.net.Socket.connect(Socket.java:469) at java.net.Socket.<init>(Socket.java:366) at java.net.Socket.<init>(Socket.java:180) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect SocketFactory.java:22) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster SocketFactory.java:128) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595) ... 20 more
好了 完了 注你成功!