In-depth spring integration integration remote call RMI principle

background

1. RMI remote method call

  • RMI (Remote Method Invocation) remote method invocation. The object on the client Java virtual machine can call the method on the object in the server Java virtual machine like a local object. Use representative: EJB
  • RMI application architecture:
    In-depth spring integration integration remote call RMI principle
  • Transport Layer − At this layer, the client and the server are connected. It is used to manage the existing connection, and it can also create a new connection.
  • Stub − The stub is the proxy proxy of the remote object on the client. It resides in the client system; it is the gateway of the client program.
  • Skeleton − The object that resides on the server. The stub communicates with the skeleton and sends the request to the remote object.
  • RRL (Remote Reference Layer) − A layer that manages references from clients to servers.

java RMI example

Server

The pojo java Bean
In-depth spring integration integration remote call RMI principle
service inherits the Remote interface
In-depth spring integration integration remote call RMI principle
service and implements the UnicastRemoteObject, which can realize the service publishing function.
In-depth spring integration integration remote call RMI principle
server
In-depth spring integration integration remote call RMI principle

Client

The pojo java Bean
In-depth spring integration integration remote call RMI principle
service inherits the Remote interface.
In-depth spring integration integration remote call RMI principle
Client test.
In-depth spring integration integration remote call RMI principle
At this time, the client prints

The information returned by the server is: succes

Server-side printing

Order{item='davidwang', qty=456, orderDate=2020-07-17T13:58:45.464}

summary

The java rmi service has the function of providing rmi by implementing the Remote interface. The real service implements the UnicastRemoteObject interface to enable it to publish the service. Finally, the LocateRegistry registration and Naming service are used to bind the service.

Spring RMI example

Server

The pojo javaBean
In-depth spring integration integration remote call RMI principle
service
In-depth spring integration integration remote call RMI principle
implementation
In-depth spring integration integration remote call RMI principle
starts the server
In-depth spring integration integration remote call RMI principle
RmiServiceExporter (the server is released using UnicastRemoteObject.exportObject())

Client

The pojo javaBean
In-depth spring integration integration remote call RMI principle
service
In-depth spring integration integration remote call RMI principle
test class
In-depth spring integration integration remote call RMI principle
RmiProxyFactoryBean (the client, automatically calls the getObject() method to obtain the proxy class), and the
server prints:

create order: Order{item='davidwang', qty=456, orderDate=2020-07-17T14:20:59.274}

Client printing:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
rmiClientTest
exporter
spring rmi返回结果:Order{item='davidwang', qty=456, orderDate=2020-07-17T14:20:59.274}

summary

Spring encapsulates the implementation of java rmi. The server uses RmiServiceExporter to publish services, and the client uses RmiProxyFactoryBean to obtain services.

In-depth Spring RMI principles

Analyze from the server RmiServiceExporter and the client RmiProxyFactoryBean respectively

Server RmiServiceExporter

RmiServiceExporter implements InitializingBean and implements the afterPropertiesSet method during bean initialization. The specific implementation directly refers to the prepare() method.
In-depth spring integration integration remote call RMI principle
1. RmiInvocationWrapper inherits from Remote, wrapping the interface service is
In-depth spring integration integration remote call RMI principle
equivalent to the interface implementing the Remote interface.
2. The
In-depth spring integration integration remote call RMI principle
Registry uses the LocateRegistry.getRegistry method to obtain the Registry
. 3. The exportObject() method of UnicastRemoteObject of jdk is used. There are six ways to use it:

  • UnicastRemoteObject()
  • UnicastRemoteObject(int)
    • UnicastRemoteObject(int, RMIClientSocketFactory, RMIServerSocketFactory)
  • exportObject(Remote)
  • exportObject(Remote, int)

  • The fourth of exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory) has expired.

    Client RmiProxyFactoryBean

    The client RmiProxyFactoryBean inherits from RmiClientInterceptor, by implementing the afterPropertiesSet() method of InitializingBean, and the specific implementation is in the prepare method.
    In-depth spring integration integration remote call RMI principle
    Support Naming mode and stub mode
    In-depth spring integration integration remote call RMI principle

to sum up

1. RMI is easy to confuse with RPC.

Two, RPC remote procedure call

  • RPC (Remote Procedure Call Protocol) remote procedure call protocol, request to call a certain service from a remote computer through the network. It is a protocol for requesting services from remote computer programs through the network without needing to understand the underlying network technology. Use representative: Dubbo
    In-depth spring integration integration remote call RMI principle

    Three, the difference between RMI and RPC

  • 1. The method call method is different: RMI calls the method. In RMI, the remote method is called through the Stub object on the client as the remote interface. Every remote method has a method signature. If a method is executed on the server, but no matching signature is added to the remote interface (stub), then the new method cannot be called by the RMI client. RPC calls functions. In RPC, a request is sent to a remote host through a network service protocol. The request contains a parameter set and a text value, usually in the form of "classname.methodname (parameter set)". This indicates to the RPC server that the requested method is in the "classname" class, named "methodname". Then the RPC server searches for the matching class and method, and uses it as the input of that method parameter type. The parameter type here matches the type in the RPC request. Once the match is successful, this method is called, and the result is encoded and sent back through the network protocol.
  • 2. The applicable language range is different: RMI is only used for Java and supports the transfer of objects. RPC is based on the C language, does not support transmission objects, is a network service protocol, and has nothing to do with the operating system and language.
    In-depth spring integration integration remote call RMI principle
  • 3. The return form of the call result is different: RMI is object-oriented, and Java is object-oriented, so the call result of RMI can be an object type or a basic data type. The result of RPC is uniformly expressed by External Data Representation (XDR) language, which abstracts the difference between endian classes and data type structures. Only the data types defined by XDR can be transferred. It can be said that RMI is an object-oriented Java RPC.

Guess you like

Origin blog.51cto.com/15015181/2556236