previously distributed system calls were made using older EJBS, which served as service callers to invoke services from other systems. Recently found in the new company, using the RMI, check the bottom of the discovery of EJB implementation is RMI, is also familiar with ...
one, using RMI in the JDK for service publishing and referencing
Service-side interface:
/** * Created by Liuhuichao on 2016/11/18. */public interface Userinfoservice extends remote{ /** * Defines a remote interface, must inherit from the remotes interface, * All methods that require a remote call must throw a RemoteException exception * /String getusername (int id) throws remoteexception;}
Service-side interface implementation:
/** * Created by Liuhuichao on 2016/11/18. */public class Userinfoserviceimpl extends UnicastRemoteObject implements userinfoservice{ /** * Interface implementation class to implement the Serializable interface at the same time, here inherit UnicastRemoteObject is also indirectly implemented serializable interface, * At the same time, because the construction method needs to throw remoteexception, Therefore, the implicit parameterless construction method cannot be used by default, and the constructor method should be explicitly defined by itself. * @throws remoteexception * /protected Userinfoserviceimpl () throws remoteexception { } @ Override public String getusername (int id) throws RemoteException { return "Misty Paddy Fly Egret"; }}
The unit Test service starts with a client call:
* * Created by Liuhuichao on 2016/11/18. */public class Jdkrmiserviceclienttest {/** * uses Java native JDK for RMI invocation: Server * @throws remoteexception * @throws Alreadyboundexception * @throws malformedurlexception */@Test public void StartServer () throws Remoteexcept Ion, Alreadyboundexception, Malformedurlexception, interruptedexception {Object lock = new Object (); Synchronized (lock) {Userinfoservice userinfoservice=new userinfoserviceimpl ();//Create a remote object//Generate a remote object note An instance of the registry and specifies that the port is 8888 (the default port is 1099) locateregistry.createregistry (8888); To register the remote object on the RMI server, the URL with the name Userinfoservice////binding is the standard format: Rmi://host:port/name (the protocol name can be omitted) naming.bind ("RMI: 127.0.0.1:8888/userinfoservice ", Userinfoservice); Lock.wait (); }} @Test public void Startclient () throws RemoteException, Notboundexception, malformedurlexception {// Look for an object with the name Rhello in the RMI service registry and call the method on it UserinfoservIce userinfoservice= (Userinfoservice) naming.lookup ("Rmi://127.0.0.1:8888/userinfoservice"); System.out.println (Userinfoservice.getusername (1)); }}
in use, the server is generally packaged into a jar, then the client in the Pom configuration depends on the jar, you can use the server interface.
Two, spring integrated RMI
in order to facilitate the service release and invocation, we can use spring to integrate, we only need to write the ordinary Pojo class can be implemented;
Service-side interface:
/** * Created by Liuhuichao on 2016/11/18. */public interface Accountservice { int querybalance (String mobileno); String Shoopingpayment (String Mobileno, byte protocol);
Service-Side implementation class:
/** * Created by Liuhuichao on 2016/11/18. */public class Mobileaccountserviceimpl implements Accountservice {public int querybalance (String mobileno) { if (Mobileno! = null) return; return 0; } public string Shoopingpayment (string Mobileno, byte protocol) { return mobileno; }}
Spring configuration on the server side:
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/HTTP Www.springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.2.xsd "><bean id=" Serviceexporter "class=" Org.springframework.remoting.rmi.RmiServiceExporter "><property name="ServiceName" value= "Mobileaccountservice"/><!--service name--><property name= "service" ref= "Accountservice"/ > <!--configuration Injection implementation class--><property Name= "Serviceinterface" value= "test. Rmi.accountservice "/><property name=" Registryport "value=" 8089 "/> <!--called Port--><property name=" Serviceport "value=" 8088 "/></bean><bean id=" Accountservice "class=" test. Rmi.mobileaccountserviceimpl "/></beans>
To start the service side in the test code:
/** * Created by Liuhuichao on 2016/11/18. */@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"classpath:/ Applicationcontext-test-service-rmi.xml "}) public class Startservertest { @Test public void Startservertest ( ) throws Interruptedexception { object lock = new Object (); Synchronized (lock) { lock.wait ();}} }
Configuration of Client Spring:
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/HTTP Www.springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.2.xsd "><bean id=" Mobileaccountservice "class=" Org.springframework.remoting.rmi.RmiProxyFactoryBean "><property Name= "serviceurl" value= "Rmi://127.0.0.1:8089/mobileaccountservice"/> <!--the address of the RMI service called--><property Name= "Serviceinterface" value= "test. Rmi.accountservice "/> <!--the interface of the invoked service--><property name=" Refreshstubonconnectfailure "value=" true "/> <!--resolve an error that denies the connection or cannot find the service object after restarting the RMI server another problem with the--><!--stub query is that the target RMI server and RMI registry entries are available at query time. If the client attempts to query and cache the service stub before the server starts, the client's startup will fail (even if the service is not yet required). To be able to lazily query the service stub, set the Rmiproxyfactorybean Lookupstubonstarup flag to False. The stub is then queried on the first visit, that is, when the first method on the agent is called, it actively queries the stub and is cached. This also has the disadvantage that it is not possible to confirm whether the target service actually exists until the first call. --><property name= "Lookupstubonstartup" value= "false"/></bean></beans>
Client calling Code:
/** * Created by Liuhuichao on 2016/11/18. */@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"classpath:/ Applicationcontext-test-client-rmi.xml "}) public class Startclienttest { @Resource accountservice Accountservice; @Test public void Testrmiatclient () { String result = Accountservice.shoopingpayment ("123333456357654", (byte ) 5); SYSTEM.OUT.PRINTLN (result);} }
Three, RMI principle
RMI applications typically consist of two separate programs: Server programs and client programs. A typical server application creates multiple remote objects that enable these remote objects to be referenced, and then waits for the client to invoke the methods of those remote objects. A typical client program obtains a reference to one or more remote objects from the server and then invokes the method of the remote object. RMI provides a mechanism for communicating and transmitting information to servers and clients.
In communication with remote objects, RMI uses the standard mechanism: stub and skeleton. The stub of the remote object serves as the client local representative or agent role for the remote object. The calling program will invoke the method of the local stub, and the local stub will be responsible for executing the method call to the remote object. In RMI, the stub of a remote object is the same as the remote interface set implemented by the remote object. When you call the stub's method, the following actions are performed:
(1) Initialize the connection to the remote virtual machine containing the remote object;
(2) The parameters of the remote virtual machine are grouped (written and transmitted);
(3) Wait for the result of the method call;
(4) decoding (reading) The return value or the returned exception;
(5) Returns the value to the calling program.
To demonstrate a simpler invocation mechanism to the caller, the stub hides details such as serialization of parameters and network-level communication. In a remote virtual machine, each remote object can have a corresponding skeleton (no need to use skeleton in the JDK1.2 environment). The skeleton is responsible for assigning the call to the actual remote object implementation. It does the following when it receives a method call: (1) The parameters of the remote method are unpacked (read), (2) The methods on the actual remote object implementation are invoked, and (3) The result (return value or exception) is grouped (written and transmitted) to the calling program. Stubs and skeleton are generated by the Rmic compiler.
The method call is passed down from the client object through the placeholder program (Stub), the remote reference layer (remotereference layer), and the Transport layer (Transport layer) to the host, and then again through the transport layer, up through the remote call layer and the backbone (Skeleton). The server object is reached. The placeholder program acts as the proxy for the remote server object, enabling the object to be activated by the client. The remote reference layer handles semantics, manages the communication of single or multiple objects, and determines whether the call should be sent to a server or multiple. The transport layer manages the actual connection and traces the remote object that can accept the method call. The server-side backbone completes the actual method call to the server object and gets the return value. The return value is passed down through the remote reference layer, the server-side transport layer back to the client, and then back up through the transport layer and the remote call layer. Finally, the placeholder program obtains the return value.
Four, RMI and RPC
1, language differences:RMI only supports calls between Java systems, while RPC supports heterogeneous system calls;
2, how to be called:
in RMI, the remote interface enables 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.
3. Direct differences in the delivery of information
In RMI, the remote interface enables 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.
Java--rmi