First, compare the differences between RPC and RMI:
First Java provides the RMI api,jdk1.5 after the virtual machine automatically generates two classes: stub class stub and Skeleton Class Skelton. The stub is to the client, and when the client invokes a method of the remote object, it is actually the appropriate method to invoke the local stub object. Skelton is responsible for accepting requests from clients on the server side for remote objects. RMI must have JVM support, so if C,s is using Java development, you might consider using RMI.
RPC can be a cross-language, client request parameter with classes, methods and parameters that need to be called
----------------------------------------above is a personal understanding----------------------------------------------------
The following 3 differences turn from: http://blog.csdn.net/shan9liang/article/details/8995023
(1) RPC is a cross-language, and RMI only supports Java.
(2) RMI invokes a remote object method that allows methods to return Java objects and basic data types, whereas RPC does not support the concept of objects, and the messages passed to the RPC service are represented by external data representations (External data representation, XDR) language, which is abstract The difference between the byte-order class and the data type structure. Only data types defined by XDR can be passed, and RMI is a Java RPC object-oriented approach.
(3) On a method call, in RMI, the remote interface causes each remote method to have a method signature. If a method executes on the server, but no matching signature is added to the remote interface, the new method cannot be called by the RMI client.
In RPC, when a request arrives at the RPC server, the request contains a set of parameters and a literal value, usually in the form of a "classname.methodname". This indicates to the RPC server that the requested method is in the class "ClassName", which is called "MethodName". The RPC server then searches for classes and methods that match it and takes it as input to the type of the method parameter. The parameter type here is matched to the type in the RPC request. Once the match is successful, the method is called, and the result is encoded and returned to the client side.
The remote object inherits the remotes, each of which requires throws RemoteException, which provides business functionality through this interface and then binds it to a port to provide remote services
Public Interface extends Remote { publicthrows remoteexception;}
Public class Implements Business { @Override publicthrows remoteexception { if ("Quit". Equalsignorecase (Message)) { System.out.println ("server would be shut"); System.exit (0); } return "Server Response" + message; }}
Server
Public classServer { Public Static voidMain (string[] args)throwsremoteexception{intPort = 1233;//This port is responsible for providing servicesString name = "Businessdemo"; Business and Business=NewBusinessimpl (); Unicastremoteobject.exportobject (Business,port); //If Businessimpl inherits the UnicastRemoteObject this line of code can not be writtenRegistry Registry = locateregistry.createregistry (1234);//This port is responsible for registering and querying services, and the client only needs to know the port number.Registry.rebind (name, business); }}
Client
Public class Client { publicstaticvoidthrows remoteexception, notboundexception{ // If CS is to be cloth on two machines, here IP server IP can be String name = " Businessdemo "; // access the server when you're done here. System.out.print (Business.echo ("Hello"));} }
Talking about Java RMI