一 、服務端編寫
(1)RPCServer類
public class RPCServer implements Bizable{ public String sayHi(String name){ return "Hi~"+name; } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); RPC.Server server = new RPC.Builder(conf) .setProtocol(Bizable.class) .setInstance(new RPCServer()) .setBindAddress("192.168.19.10") .setPort(9527) .build(); server.start(); } }
(2)Bizable接口
public interface Bizable { public static final long versionID=10010L; public String sayHi(String name); }
二 、客戶端編寫
public class RPCClient { public static void main(String[] args) throws Exception { Bizable proxy = RPC.getProxy(Bizable.class, 10010L, new InetSocketAddress("192.168.19.10", 9527), new Configuration()); String result = proxy.sayHi("小虹塵"); System.out.println("=============================================================="); System.out.println(result); System.out.println("=============================================================="); RPC.stopProxy(proxy); } }
三 、 測試
(1)本地客戶端與服務端通信
運行RPCServer類------>運行RPCClient類 ,運行結果如圖:
(2)虛擬機與本機通信
使用idea軟件打可運行的Jar包
File------>Project Structure------>Artifacts------> + ------>Jar------>From Module And Dependences
------>Main
Class------>RPCClient------>Copy to the output directory and link via manifest
------>Apply------>OK
【2】將打好的文件../out/artifacts/hadoop_jar_jar上傳到虛擬機下
【3】在主機,運行本地的RPCServer類
【4】進入到hadoop_jar_jar目錄下
cd hadoop_jar_jar
運行jar包 java -jar 包名
例如:java -jar hadopp_jar.jar