发表于: 2017-12-04 23:01:15
1 821
今天做的事情:
开始了解Rmi,看了一些概念。例如RMI是Java的一组拥护开发分布式应用程序的API。 RMI(Remote Method Invocation,远程方法调用)是用Java在JDK1.1中实现的,它大大增强了Java开发分布式应用的能力。
先来跑通一个例子 http://blog.csdn.net/zhongweijian/article/details/8005124
spring rmi实际上是扩展了下java rmi的实现,可以使用bean的xml配置方式使用rmi。
然而RMI在使用时必须一连串繁复的手续,像是服务介面在定义时必须继承java.rmi.Remote介面、服务Server在实作时必须继承java.rmi.UnicastRemoteObject类别、必须使用rmic指令产生stub与skeleton等,设定上手续繁杂。 您可以在Spring中透过org.springframework.remoting.rmi.RmiServiceExporter来简化使用RMI的手续,来实际看看例子,了解Spring在RMI上的使用与简化,
这个博客做的是简化的例子,并没有去继承remote。
一:定义远程接口,这里要注意下:
1. 远程接口必须为public属性。如果不这样,除非客户端与远程接口在同一个包内,否则 当试图装入实现该远程接口的远程对象时,调用会得到错误结果。
public interface RmiService {
public String doWork();
public int add(int a, int b);
}
二:实现远程接口:
public class RmiServiceImpl implements RmiService {
public String doWork() {
return "this messages return from server";
}
public int add(int a, int b) {
return a+b;
}
}
三:编写服务类
让Spring管理,生成bean实例,注册和启动rmi服务。
<bean id="rmiService"
class="com.jnshu.dao.RmiServiceImpl"/>
<bean id="serviceExporter"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="rmiService"/>
<property name="serviceName" value="rmiService" />
<property name="serviceInterface" value="com.jnshu.dao.RmiService" />
</bean>
编写一个RmiServr类来启动服务
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("rmi/rmi-server.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (reader.readLine().equals("exit")) {
System.exit(0);
}
}
}
四:编写客户端类信息:
继续让spring管理
<bean id="rmiServiceProxy"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost/rmiService" />
<property name="serviceInterface" value="com.jnshu.dao.RmiService" />
</bean>
编写一个RmiClient作为客户调用远程接口
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"rmi/rmi-client.xml");
RmiService service =
(RmiService) context.getBean("rmiServiceProxy");
String result1 = service.doWork();
System.out.println(result1);
int result2 = service.add(1, 2);
System.out.println(result2);
}
结果:
总结:使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性。
注意:代码写好之后,先启动服务,之后再客户端访问。
还有一种方式是继承Remote,链接:http://blog.csdn.net/zmx729618/article/details/52130722
接口
/**
* Created by 有何不可 on 2017/12/4.
*
* 定义远程接口 集成remote接口
* 其中所有需要远程调用的方法都必须抛出RemoteException异常
*/
public interface IHello extends Remote{
public String sayHello(String name) throws RemoteException;
public int sum(int a, int b)throws RemoteException;
}
实现类:
public class HelloImpl extends UnicastRemoteObject implements IHello{
private static final long serialVersionUID = 1961558474342609777L;
public HelloImpl ()throws RemoteException {
super();
}
public String sayHello(String name) throws RemoteException {
return "hello "+name;
}
public int sum(int a, int b) throws RemoteException {
return a+b;
}
}
以下的就不多说了。
深度思考:
什么是rmi?为什么要使用rmi框架?
1 什么是RMI ?
远程方法调用是一种计算机之间对象互相调用对方函数,启动对方进程的一种机制,使用这种机制,某一台计算机上的对象在调用另外一台计算机上的方法时,使用的程序语法规则和在本地机上对象间的方法调用的语法规则一样。
2.RMI的优势
这种机制给分布计算的系统设计、编程都带来了极大的方便。只要按照RMI规则设计程序,可以不必再过问在RMI之下的网络细节了,如:TCP和Socket等等。任意两台计算机之间的通讯完全由RMI负责。调用远程计算机上的对象就像本地对象一样方便。
遇到的问题:
刚开始看rmi挺蒙的,加上下午睡的蒙蒙的,用了不少时间。对于web和service分离,还是不懂怎么去做,知道是耦合很低,可以重用之类的好处,对于怎么拆开,还是需要继续探索
收获:
跑通两个rmi的demo,集成remote的demo和简化的rmi的demo,初步了解。
禅道链接:http://task.ptteng.com/zentao/project-task-393.html
评论