packageio.github.baijifeilong.rmi;importjava.rmi.Remote;importjava.rmi.RemoteException;publicinterfaceISayHelloServiceextendsRemote{Stringhello(String name)throwsRemoteException;}
服务端
packageio.github.baijifeilong.rmi;importjava.rmi.RemoteException;importjava.rmi.registry.LocateRegistry;importjava.rmi.server.UnicastRemoteObject;publicclassRmiServer{publicstaticvoidmain(String[] args)throwsRemoteException{ ISayHelloService sayHelloService =newSayHelloService(); UnicastRemoteObject.exportObject(sayHelloService,0); LocateRegistry.createRegistry(1099).rebind("SayHello", sayHelloService); }staticclassSayHelloServiceimplementsISayHelloService{@OverridepublicStringhello(String name){return"hello, "+ name; } }}
客户端
package io.github.baijifeilong.rmi;importjava.rmi.NotBoundException;importjava.rmi.RemoteException;importjava.rmi.registry.LocateRegistry;importjava.rmi.registry.Registry;publicclassRmiClient{publicstaticvoid main(String[] args)throwsRemoteException,NotBoundException{// RMI默认端口1099,不需要显式声明Registryregistry =LocateRegistry.getRegistry();ISayHelloServicesayHelloService = (ISayHelloService) registry.lookup("SayHello");System.out.println(sayHelloService.hello("word")); }}
进群:697699179可以获取Java各类入门学习资料!
这是我的微信公众号【编程study】各位大佬有空可以关注下,每天更新Java学习方法,感谢!
学习中遇到问题有不明白的地方,推荐加小编Java学习群:697699179内有视频教程 ,直播课程 ,等学习资料,期待你的加入
网友评论