发表于: 2017-11-27 20:48:13
1 646
今天完成的内容:
(1)RMI小例子。
Spring 封装RMI实现:
服务端Web工程中添加接口,普通接口:
package rmi.test;
public interface HelloRMIService {
int getAdd(int a, int b);
}
实现类就不贴了。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="helloRMIServiceImpl" class="rmi.test.HelloRMIServiceImpl"> </bean>
<!-- 将一个类发布为一个RMI服务 -->
<bean id="myRMIServer" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="helloRMI"></property>
<property name="service" ref="helloRMIServiceImpl"></property>
<property name="serviceInterface" value="rmi.test.HelloRMIService"></property>
<property name="registryPort" value="9999"></property>
</bean>
</beans>
启动Web工程的服务器,该配置文件应该被Spring的监听器监听,并加载,启动成功后,服务端就算建好了。
如果服务器是在localhost启动的,那么暴露的RMI的IP也是localhost,如果需要使用其他IP,需要让服务器在其他的IP启动。
这里没有web服务器,使用main模拟启动:
package rmi.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIServiceTest {
public static void main (String[] args){
new ClassPathXmlApplicationContext("rmiServer.xml");
}
}
客户端实现类:
package rmi.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIClient {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("rmiClient.xml");
HelloRMIService helloRMIService = applicationContext.getBean("myRMIClient", HelloRMIService.class);
System.out.println(helloRMIService.getAdd(3,4));
}
}
客户端配置文件(bean部分):
<bean id="myRMIClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface" value="rmi.test.HelloRMIService"></property>
<property name="serviceUrl" value="rmi://127.0.0.1:9999/helloRMI"></property>
</bean>
启动,客户端调用服务端的方法得出结果:
明天的计划:消化一下,结合任务。
遇到的问题:xml路径问题。
收获:更加了解了RMI。
评论