???????
??????????????????????
?????????????????? JAVA ????????????A??????B?????????A ??????÷???B?е?????????????????????
??????????????????????B???????????????????A??? RPC ?????????????????????? RPC ????????????????????????????? REST ???????????
?????????????????????к????? JAVA ?????????????????????????? RMI ???? JVM ????????????á?????????????????? RPC ????????????????????????????????????????????????????????????
????RMI ???
????RMI??Remote Method Invocation??????????????????????????ó?????????????????????е???????????????????????????????????????Java??????????????繃???з????????
???????
????· ?? JAVA ????????????輯???κε????????
????· ??????????????????
????· ???????????????
????· JAVA ??????
???????Э??
???????????????? TCP ????Э?????? rmi://??JAVA???????????
???????
????· RMI Registry??????洢????????????????
????· Server????????????????????????????????? RMI Registry
????· Client??????????????????????????÷??????
???????л???

????????????????????? RMI ????????е????????????????????????????????????????????????????????????
????· ??????????
????· ?????????????
????· ?????????????
????· ????????????????????????
????· ???????????÷???
??????÷???
????· ??? RMI Registry
?????????????????????????????????????????
????import java.rmi.registry.LocateRegistry;
????Registry registry = LocateRegistry.createRegistry(REGISTRY_PORT);
?????????????????????
????/usr/bin/rmiregistry REGISTRY_PORT
????· ??? RMI Registry
??????????????? java.rmi.server.hostname ?????ò????
????import java.rmi.registry.LocateRegistry;
????Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT)
????· ????????????
????????? Remote
????????????????? RemoteException
????import java.rmi.Remote;
????public interface RemoteService extends Remote {
????//define your function
????Object run() throws RemoteException;
????}
????· UnicastRemoteObject.exportObject(Remote obj?? int port)
???????? Remote ??????????????? Serializable ???
?????? TCP ??????????
????port ? 0 ???????????????? ?? ???1~1023????????????????????? ??
????import java.rmi.server.UnicastRemoteObject;
????Remote remoteProxy = UnicastRemoteObject.exportObject(your_remote_service?? 0);
????· ????????? RMI Registry?? ?? Registry ?е????????????????????????????? ??
??????? Registry ???????????????????? rebind ?????????????????? ?? Naming.rebind(String name?? Remote obj) ???????????? name ?е?????????????????????????????????? ??
????// ?????????????? Registry
????Registry registry = ...
????registry.rebind(String name?? Remote obj);
????· ?????????????
????Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT);
????Remote obj = registry.lookup(REMOTE_NAME);
???????
????###????????? ????????????
????package com.test.remote;
????import java.rmi.Remote;
????import java.rmi.RemoteException;
????public interface RemoteService extends Remote {
????Object run() throws RemoteException;
????Object run(Object obj) throws RemoteException;
????}
????###????B???????????
????· ????????????
package com.test.serviceB.publishService;
import com.test.remote.RemoteService;
import java.rmi.RemoteException;
public class pService1 implements RemoteService {
public Object run() {
System.out.println("invoke pService1.");
return "success";
}
public Object run(Object obj) throws RemoteException {
System.out.println("invoke pService1?? params is " + obj.toString());
return "success";
}
}
????· ???????
???????? RMI Registry?????????????? rmiregistry ???????????
???????????????
????????????????????????????
??????????????????????????
??????????????
????public class Boot {
????private static final String REMOTE_P1 = "serviceB:p1";
????private static final int REGISTRY_PORT = 9999;
????public static void main(String[] args) throws RemoteException {
????// ?????????????????????????
????RemoteService p1 = new pService1();
????Remote stub1 = UnicastRemoteObject.exportObject(p1?? 0);
????// ??????? Registry???????????????
????Registry registry = LocateRegistry.createRegistry(REGISTRY_PORT);
????registry.rebind(REMOTE_P1?? stub1);
????System.out.println("service b bound");
????}
????}
????###????A????????????
????· ???????
??????????
?????? Registry ?в????????÷????????????
???????????????
????public class Boot {
????private static final String REMOTE_P1 = "serviceB:p1";
????private static final int REGISTRY_PORT = 9999;
????public static void main(String[] args) throws RemoteException {
????try {
????Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT);
????// ?????л??????????
????RemoteService p1 = (RemoteService) registry.lookup(REMOTE_P1);
????// ?????????
????String res1 = (String)p1.run();
????System.out.printf("The remote call for %s %s "?? REMOTE_P1?? res1);
????} catch (NotBoundException e){
????e.printStackTrace();
????} catch (RemoteException e){
????e.printStackTrace();
????}
????}
????}
??????????
???????????B
????service b bound
???????????A
????The remote call for serviceB:p1 success
????Process finished with exit code 0
??????????B ???????
????service b bound
????invoke pService1.
????????÷?
???????????????漰?????????????????????????Σ?????ε??????????????????????????????????????????
???????????????????????????????????
????java.rmi.server.codebase????????????·??????????·????????????file://?? ftp://?? http:// ????????
????java.rmi.server.useCodebaseOnly?????? true?? ?????????????? codebase?? ?????????? codebase?? ????B ?????? ????A ??????????? ?????軔???????????false??
???????????????????RMI ????????????????SecurityManager?????????????????????????
????java.security.policy?????????????????
?????????????
????java.rmi.server.hostname?????ò????????????
????sun.rmi.transport.tcp.handshakeTimeout??????????????????
????????????
???????????????????????????????仰???????“???????????????????????????????????????????”
????????????????????????????????????????????????????????????
??????????? Registry ????????????????
????????δ???????????
????Registry registry = LocateRegistry.getRegistry(REGISTRY_PORT);

???????????????????????????????????
????????δ???????????
????Remote obj = UnicastRemoteObject.exportObject(Remote obj?? int port);

????????????д??????????? ???????????????????????????????????
??????? JDK ???????????????????? invoke ????????????????????????? invoke() ????????? UnicastRef ?С?

????????
??????????п????????????????ζ??? ?????? connection???о??????????????????????
???????
??????? RMI ??????????Щ?????????????????????????????????????????????????????? RPC ????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????仯?????????????????С??????У??????Щ??????????????????????????????????????????