RMI:远程方法调用(Remote Method Invocation)。可以让在某个java虚拟机上的对象像调用本地对象方法同样调用另外一个java 虚拟机中的对象上的方法。html
对于客户对象来讲,步骤2-6是彻底透明的。
以上简明扼要的介绍了什么是RMI和调用步骤,接下来,说说在程序中是如何使用rmi的。java
1.建立一个服务端项目rmi-server
2.建立远程方法接口,该接口须要实现Remote接口git
public interface IHelloService extends Remote { public String sayHello(String name) throws RemoteException; public int sum(int a, int b) throws RemoteException; }
3.建立远程方法接口实现类,而后执行main方法就等于启动了rmi服务端了spring
public class HelloServiceImpl extends UnicastRemoteObject implements IHelloService { public HelloServiceImpl() throws RemoteException { super(); } @Override public String sayHello(String name) throws RemoteException { return "hello: " + name; } @Override public int sum(int a, int b) throws RemoteException { return a + b; } public static void main(String args[]) { try { //建立一个远程对象 IHelloService helloService = new HelloServiceImpl(); //生成远程对象注册表Registry的实例,并指定端口为8888(默认端口是1099) LocateRegistry.createRegistry(8888); //把远程对象注册到RMI注册服务器上,并命名为RHello //绑定的URL标准格式为:rmi://host:port/name(协议名能够省略,下面两种写法均可以) Naming.bind("rmi://127.0.0.1:8888/HelloService", helloService); System.out.println(">>INFO:远程IHello对象绑定成功!"); } catch (RemoteException e) { System.out.println("建立远程对象发生异常!"); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println("发生重复绑定对象异常!"); e.printStackTrace(); } catch (MalformedURLException e) { System.out.println("发生URL畸形异常!"); e.printStackTrace(); } } }
1.新建一个客户端项目rmi-client
2.复制服务端的远程方法接口IHelloService,注意,包名要一致
3.测试类调用RMI服务springboot
public class HelloServiceClientTest { @Test public void testSayHello() { try { // 在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法 IHelloService rhello = (IHelloService) Naming.lookup("rmi://127.0.0.1:8888/HelloService"); System.out.println(rhello.sayHello("world")); System.out.println(rhello.sum(454, 5457)); } catch (Exception e) { e.printStackTrace(); } } }
1.建立一个基于Sringboot的项目rmi-server
2.建立一个远程方法接口服务器
package com.wp.learn.spring.remoting; import java.rmi.Remote; import java.rmi.RemoteException; public interface IHelloService extends Remote { public String sayHello(String name) throws RemoteException; public int sum(int a, int b) throws RemoteException; }
3.实现远程方法接口网络
package com.wp.learn.spring.remoting.impl; import com.wp.learn.jdk.remoting.IHelloService; import org.springframework.stereotype.Service; import java.rmi.RemoteException; @Service public class HelloServiceImpl implements IHelloService { @Override public String sayHello(String name) throws RemoteException { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } return "hello: " + name; } @Override public int sum(int a, int b) throws RemoteException { return a + b; } }
4.rmi相关配置app
@Configuration public class RMIConfig { @Autowired @Qualifier("helloServiceImpl") private HelloServiceImpl helloService; @Bean public RmiServiceExporter initRmiServiceExporter(){ RmiServiceExporter exporter=new RmiServiceExporter(); exporter.setServiceInterface(IHelloService.class); exporter.setServiceName("HelloService"); exporter.setService(helloService); exporter.setRegistryPort(10086); return exporter; } }
1.建立一个基于springboot的项目rmi-client
2.建立和服务端同样的远程方法接口,注意包名一致
3.rmi配置ide
@Configuration public class RMIClientConfig { @Bean(name = "helloService") public RmiProxyFactoryBean initRmiProxyFactoryBean() { RmiProxyFactoryBean factoryBean = new RmiProxyFactoryBean(); factoryBean.setServiceUrl("rmi://127.0.0.1:10086/HelloService"); factoryBean.setServiceInterface(IHelloService.class); factoryBean.setLookupStubOnStartup(false); factoryBean.setRefreshStubOnConnectFailure(true); // lookupStubOnStartup : 这个属性是表示,不在容器启动的时候建立与Server端的链接; // refreshStubOnConnectFailure : 这个属性是表示是否链接出错时自动重连; // registryClientSocketFactory : 这个是客户端与服务端建立SOCKECT的一个工厂。 return factoryBean; } }
4.调用接口测试测试
@RestController @RequestMapping(value = "/v1/") public class RMIClientController { // @Autowired // @Qualifier(value = "helloService") // IHelloService helloService; @Autowired IHelloService helloService; @RequestMapping(value = "/test") public String test1() { // IHelloService helloService = (IHelloService) factoryBean.getObject(); try { System.out.println(helloService.sayHello("小青")); System.out.println("----------------------------------"); System.out.println(helloService.sum(132, 355)); } catch (RemoteException e) { e.printStackTrace(); } return "123"; } }
大功告成!源码地址: https://gitee.com/wangpinggs/learn/tree/master 相关博客: http://www.blogjava.net/zhenyu33154/articles/320245.html http://elf8848.iteye.com/blog/1961205