From: http://blog.sina.com.cn/s/blog_56fd58ab0100mrl6.html
In the distributed service framework, the most basic problem is how remote services communicate. In the Java field, there are many technologies that can implement remote communication, such: RMI, Mina, ESB, burlap, Hessian, soap, EJB, and JMS. What is the relationship between these terms? What is the principle behind them, understanding these is the basic knowledge for implementing the distributed service framework. If there are high performance requirements, it is necessary to have a deep understanding of the mechanisms behind these technologies.
In the blog, we will explore the future. We welcome you to introduce more technologies and principles for implementing remote communication.
Basic Principles
To achieve communication between network machines, first look at the basic principles of network communication in computer systems, at the underlying level, what network communication needs to do is to transmit the stream from one computer to another, which is implemented based on the transmission protocol and network I/O. Among them, the transmission protocol is famous for HTTP, TCP, UDP, and so on, HTTP, TCP, and UDP are the transmission protocols and network I/O extended for certain application scenarios based on the socket concept, mainly including bio, NiO, and AIO, all Distributed Application Communication is implemented based on this principle, just to make the application easy to use. Various languages usually provide application layer protocols that are closer to the application.
Application-level protocol
The target of remote service communication is to initiate a request on one computer. After receiving the request, the other machine processes the request and returns the result to the requester, in this case, one way request, synchronous request, asynchronous request, and other request methods are available. According to the network communication principle, what needs to be done is to convert the request into a stream, the transmission protocol is used to transmit data to the remote end. After receiving the request stream, the remote computer processes the data, converts the result to a stream, and returns the data to the caller through the transmission protocol.
This is the principle, but for the convenience of application, the industry has launched many application-level protocols based on this principle, so that you do not have to directly operate such underlying things, generally, application-level remote communication protocols provide:
1. To avoid the hassle of directly performing stream operations, a standard transmission format that is easier to use or fit with languages is provided;
2. The implementation of the network communication mechanism is to convert the transmission format into a stream for you and transmit it to the remote computer through a certain transmission protocol, the remote computer converts the received stream to the transmission format and stores the data or notifies the remote computer in some way.
When learning application-level remote communication protocols, we can learn with these questions:
1. What is the standard transmission format?
2. How can I convert a request to a transmitted stream?
3. How to receive and process a stream?
4. What is the transmission protocol?
However, application-level remote communication protocols do not greatly improve the transmission protocols, mainly in stream operations, this process makes the application layer generate a stream and process the stream more suitable for the language or standard used. As for the transmission protocol, it is generally optional. Well-known in the Java field include: RMI, XML-RPC, binary-RPC, soap, CORBA, and JMS:
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
RMI
RMI is a typical remote communication protocol customized for Java. We all know that in single Vm, we can call Java object instance directly to implement communication. In remote communication, if this method can also be followed, it is certainly the best, and this remote communication mechanism becomes RPC (Remote Procedure Call), RMI is born towards this goal.
Let's take a look at the principle of a complete remote communication process based on RMI:
1. The client initiates a request and transfers the request to the stub class of the RMI client;
2. Stub class serializes the requested interfaces, methods, parameters, and other information;
3. Transmit serialized streams to the server based on socket;
4. The server receives the stream and forwards it to the corresponding Skelton class;
5. The Skelton class deserializes request information and calls the actual processing class;
6. After processing the class, return the result to the Skelton class;
7. The Skelton class serializes the result and sends it to the stub of the client through socket;
8. Stub deserializes the received stream and returns the deserialized Java object to the caller.
Let's take a look at a better illustration of JBoss-remoting for this process:
Answer the following questions about the application-level protocol:
1. What is the standard transmission format?
Is Java objectstream.
2. How can I convert a request to a transmitted stream?
Converts the requested Java object information to a stream based on the Java serialization mechanism.
3. How to receive and process a stream?
Start the corresponding listening port according to the protocol used. When a stream enters, the stream is deserialized Based on the Java serialization mechanism, and the corresponding processing object information is obtained according to the RMI protocol, after the processing is completed, the results are also returned Based on the Java serialization mechanism.
4. What is the transmission protocol?
Socket.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
XML-RPC
XML-RPC is also a Remote Call Protocol similar to RMI, what is different from RMI is that it defines the request information (request object, method, parameter, etc.) in standard XML format. What are the advantages of this, it can also be used for cross-language communication.
Let's look at a remote communication process of the XML-RPC protocol:
1. The client initiates a request and fills in the request information according to the XML-RPC agreement;
2. After filling, the XML is converted into a stream and transmitted through the transmission protocol;
3. After receiving the received stream, convert it to XML and obtain and process the request information according to the XML-RPC protocol;
4. After processing, write the result into XML according to the XML-RPC protocol and return it.
The above process is illustrated:
To answer the same question:
1. What is the standard transmission format?
XML in standard format.
2. How can I convert a request to a transmitted stream?
Converts XML into a stream.
3. How to receive and process a stream?
Get the request stream through the listening port, convert it to XML, obtain the request information according to the protocol, process the request, and write the result into XML to return.
4. What is the transmission protocol?
HTTP.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Binary-RPC
Binary-RPC looks at the name to know that it is similar to the XML-RPC, the difference is only that the standard format of transmission from XML to binary format.
To answer the same question:
1. What is the standard transmission format?
Binary files in standard format.
2. How can I convert a request to a transmitted stream?
Converts a binary file to a stream.
3. How to receive and process a stream?
Obtain the request stream through the listening port, convert it to a binary file, obtain the request information based on the protocol, process the request, and write the result into XML to return.
4. What is the transmission protocol?
HTTP.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Soap
Soap originally meant Simple Object Access Protocol. It is a lightweight communication protocol for information exchange based on XML in a distributed environment. It can be considered as an advanced version of xml rpc, the principles of the two are exactly the same. They are both HTTP + XML. The difference is that the XML norms defined by the two are different. Soap is also the service call protocol standard adopted by WebService. Therefore, we will not elaborate on it here.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
CORBA
Common Object Request Broker Architecture (Public Object Request proxy [scheduling] program architecture) is a set of standards used to define the "Distributed Object System) as the initiator and standard setting unit. The purpose of CORBA is to define a set of protocols. objects that comply with this protocol can interact with each other, no matter what language they are written, no matter what machines and operating systems they run on.
In my opinion, CORBA is an architecture similar to SOA, covering optional remote communication protocols, but it cannot be included in the communication protocols here, and it is basically obsolete, in addition, we do not know much about CORBA, so we will not discuss it here.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
JMS
JMS is a means and method for implementing Remote Communication in the Java field. The Implementation of Remote Communication Based on JMS is different from that of rpc. Although RPC can be achieved, but because it is not defined at the protocol level, we do not think that JMS is an RPC protocol, but it is indeed a remote communication protocol, in other language systems, something similar to JMS exists. You can call such mechanisms as message mechanisms in a unified manner. What about message mechanisms, it is usually a communication mechanism recommended in the high concurrency and distributed fields. The main problem here is fault tolerance (see Erlang for details ).
Let's take a look at a remote communication process in JMS:
1. The client converts the request to a message that complies with the JMS regulations;
2. Put the message into the JMS queue or topic through the jms api;
3. If it is a JMS queue, it will be sent to the JMS queue subscribed to this topic in the target queue in the sending process. If it is a topic, it will be sent to the JMS queue subscribed to this topic.
4. The processing end obtains the message by training the JMS queue. After receiving the message, it parses the message and processes it according to the JMS protocol.
Q:
1. What is the standard transmission format?
Message specified by JMS.
2. How can I convert a request to a transmitted stream?
Put the parameter information in the message.
3. How to receive and process a stream?
The JMS queue is trained to receive the message and process it after receiving it. After processing, the message is still sent to the queue or multicast in the form of message.
4. What is the transfer protocol?
Not limited.
JMS-based is also one of the common methods for implementing Remote Asynchronous calls.
Optional Implementation Technology
Of course, the above principles have not introduced any remote communication protocols available in the Java field, such as ormi used by EJB and a simple HTTP invoker defined by spring.
After reading the principles, let's take a look at the Java framework or library that can be used to implement remote communication. Well-known frameworks include JBoss-remoting, spring-remoting, Hessian, burlap, and xfire (axis) activemq, Mina, mule, ejb3, etc., to give a brief introduction and evaluation of each type. In fact, to build a distributed service framework, these things should be well understood, this is because the distributed service framework covers two aspects: the distributed domain and the application domain.
Of course, you can also implement your own communication framework or library based on the remote network communication principle (Transport Protocol + net Io.
So what problems will I learn about these remote communication frameworks or libraries?
1. What protocol is implemented based on?
2. How to initiate a request?
3. How can I convert a request to a protocol-compliant format?
4. What transmission protocol is used for transmission?
5. What mechanism does the response end receive requests?
6. How can I restore a stream to a transmission format?
7. How can I respond after processing?
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
JBoss-remoting
JBoss-remoting is a Java remote communication framework written by JBoss. Based on this framework, RPC for Java objects based on multiple transmission protocols can be easily implemented.
Directly answer the question:
1. What protocol is implemented based on?
JBoss-remoting is a communication framework. Therefore, JBoss-remoting supports communication in Multiple Protocol modes, such as pure socket + IO mode, RMI mode, and HTTP + IO mode.
2. How to initiate a request?
In JBoss-remoting, you only need to pass the request parameter object to the invocationrequest object of JBoss-remoting. You can also encapsulate the invocationrequest object based on the invocationrequest protocol.
3. How can I convert a request to a protocol-compliant format?
JBoss-remoting converts requests to object byte streams based on the Java serialization mechanism or the serialization Implementation of JBoss itself.
4. What transmission protocol is used for transmission?
Supports multiple transmission protocols, such as socket and HTTP.
5. What mechanism does the response end receive requests?
The response end only needs to register its own processing object to the connection object of the server provided by JBoss-remoting.
6. How can I restore a stream to a transmission format?
JBoss-remoting restores request information to Java objects based on the Java serialization mechanism or the serialization Implementation of JBoss itself.
7. How can I respond after processing?
After the processing, the result object can be directly returned. JBoss-remoting will serialize the object according to the protocol and return it to the caller.
In addition, JBoss-remoting supports multiple communication modes, such as synchronous/asynchronous/unidirectional communication.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Spring-remoting
Spring-remoting is a remote communication framework provided by spring in the Java field. Based on this framework, you can also easily publish common spring beans in a remote protocol mode, you can also configure spring bean as a remotely called Bean.
1. What protocol is implemented based on?
Like JBoss-remoting, spring, as a remote communication framework, integrates Multiple Remote Communication libraries to support multiple protocols, such as RMI, HTTP + IO, XML-RPC, and binary-RPC.
2. How to initiate a request?
In spring, because the remote called Bean is implemented by proxy, the request is initiated completely through the service interface.
3. How can I convert a request to a protocol-compliant format?
Spring converts the requested object information to a stream in a protocol manner. For example, spring HTTP invoker is implemented based on a protocol defined by spring, and HTTP is used in the transmission protocol, the request information is converted to a stream for Transmission Based on the Java serialization mechanism.
4. What transmission protocol is used for transmission?
Supports multiple transmission protocols, such as RMI and HTTP.
5. What mechanism does the response end receive requests?
The response end follows the Protocol to receive requests. for users, you only need to configure the common spring bean as the response end or provide the server through spring configuration.
6. How can I restore a stream to a transmission format?
Restore by protocol.
7. How can I respond after processing?
After the processing is complete, return directly. Spring-remoting will serialize the data based on the protocol.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Hessian
Hessian is a remote communication library based on binary-RPC provided by Caucho.
1. What protocol is implemented based on?
Based on Binary-RPC.
2. How to initiate a request?
Requests must be initiated through the APIS provided by Hessian.
3. How can I convert a request to a protocol-compliant format?
Hessian serializes request information through its custom serialization mechanism to generate binary streams.
4. What transmission protocol is used for transmission?
Hessian is transmitted over HTTP.
5. What mechanism does the response end receive requests?
The response side receives the request based on the API provided by Hessian.
6. How can I restore a stream to a transmission format?
Hessian deserializes request information based on its proprietary serialization mechanism. It is already a request information object when it is passed to the user.
7. How can I respond after processing?
Return directly after processing. Hessian serializes the result object and transmits it to the caller.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Burlap
Burlap is also provided by Caucho, which, unlike Hessian, is based on XML-RPC protocols.
1. What protocol is implemented based on?
Based on XML-RPC protocol.
2. How to initiate a request?
Based on the APIS provided by burlap.
3. How can I convert a request to a protocol-compliant format?
Convert the request information to an XML format that complies with the Protocol into a stream for transmission.
4. What transmission protocol is used for transmission?
HTTP protocol.
5. What mechanism does the response end receive requests?
Listen for HTTP requests.
6. How can I restore a stream to a transmission format?
Restores Based on the XML-RPC protocol.
7. How can I respond after processing?
The returned results are written into XML and returned to the caller by burlap.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Xfire, Axis
Xfire and axis are the implementation frameworks of WebService. WebService can be regarded as a complete SOA architecture Implementation Standard. Therefore, using xfire and axis means using WebService.
1. What protocol is implemented based on?
Based on the SOAP protocol.
2. How to initiate a request?
Obtain the proxy of the remote service and call it directly.
3. How can I convert a request to a protocol-compliant format?
The request information is converted to an XML format following the SOAP protocol, and the framework is converted to a stream for transmission.
4. What transmission protocol is used for transmission?
HTTP protocol.
5. What mechanism does the response end receive requests?
Listen for HTTP requests.
6. How can I restore a stream to a transmission format?
Restore Based on the SOAP protocol.
7. How can I respond after processing?
The returned results are written into XML and returned to the caller by the framework.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Activemq
Activemq is the implementation of JMS. It is a good choice to implement Remote Communication Based on Message mechanisms such as JMS, after all, the functions of the message mechanism make it easy to implement synchronous, asynchronous, and unidirectional calls based on it. In addition, the message mechanism is also a good choice from the perspective of easy error, this is an important foundation for Erlang to achieve fault tolerance.
1. What protocol is implemented based on?
Based on the JMS protocol.
2. How to initiate a request?
Follow the jms api to initiate a request.
3. How can I convert a request to a protocol-compliant format?
It is not clear. The conjecture should be a binary stream.
4. What transmission protocol is used for transmission?
Supports multiple transmission protocols, such as socket and HTTP.
5. What mechanism does the response end receive requests?
Listen to the port that complies with the protocol.
6. How can I restore a stream to a transmission format?
Same as issue 3.
7. How can I respond after processing?
Follow the jms api to generate a message and write it to the JMS queue.
Examples of implementing Remote Communication Based on JMS include spring-intergration, mule, and lingo.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Mina
Mina is a communication framework provided by Apache. It has never mentioned network I/O before. previously mentioned frameworks or libraries are basically bio-based, while Mina uses NiO, NIO has a significant performance improvement compared with bio in terms of concurrency growth, while the improvement of Java performance has a lot to do with the close combination of NiO and OS.
1. What protocols are implemented based on?
Based on pure socket + NiO.
2. How to initiate a request?
Use the client API provided by Mina.
3. How can I convert a request to a protocol-compliant format?
Mina serializes request objects according to the Java serialization mechanism.
4. What transmission protocol is used for transmission?
Supports multiple transmission protocols, such as socket and HTTP.
5. What mechanism does the response end receive requests?
Listen to the protocol port in NIO mode.
6. How can I restore a stream to a transmission format?
Follow the Java serialization mechanism to deserialize the request object.
7. How can I respond after processing?
Follow the Mina API to return data.
Mina is based on NiO, so it is no suspense to support asynchronous calls.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
EJB
The most prominent aspect of EJB is its distribution. EJB adopts the ormi protocol, which is similar to the RMI protocol, however, the outstanding features of EJB in Distributed communication security control, transport pool, and smart proxy make it an indispensable force in the distributed field.
1. What protocol is implemented based on?
Based on the ormi protocol.
2. How can I initiate a request?
EJB call.
3. How can I convert a request to a protocol-compliant format?
Serialize the request object according to the Java serialization mechanism.
4. What transmission protocol is used for transmission?
Socket.
5. What mechanism does the response end receive requests?
Listening protocol port.
6. How can I restore a stream to a transmission format?
Follow the Java serialization mechanism to deserialize the request object.
7. How can I respond after processing?
Directly return the processing object.
In the previous articles on the distributed service framework series, we suspected that the JNDI was misleading. In this blog, we also introduced the mechanism of JNDI, because the JNDI depends on the specific implementation, here we can only explain the JBoss's JNDI implementation.
After the object instance is bound to the JBoss jnp server, the remote end uses context. when you get a remote object instance in lookup () mode and start calling, the JBoss JNDI implementation method is to get the object instance from the jnp server, serialize it back to the local, and then perform local deserialization, then, call the class locally.
Through this mechanism, we can know that the local must actually have a class bound to the object instance on JBoss. Otherwise, the deserialization will certainly fail, what needs to be done in remote communication is to remotely execute an action and obtain the corresponding results. It can be seen that remote communication cannot be achieved purely based on JNDI.
However, JNDI is also a key technical point for implementing the distributed service framework, because it can be used to implement transparent remote and local calls, just like EJB, in addition, it is also a good solution to hide the actual deployment mechanism (such as datasource.
Summary
From a series of analyses, we can see that there are a lot of knowledge points involved in the field of remote communication, such: communication Protocol (socket/tcp/HTTP/udp/RMI/XML-RPC etc .), message mechanism, network io (bio/NiO/AIO), multithread, local call and remote call transparent solution (involving Java classloader, dynamic proxy, unit test etc .) asynchronous and synchronous calls, network communication processing mechanisms (automatic reconnection, broadcast, exception, pool processing, etc.), Java serialization (Private serialization mechanisms of various protocols, etc) implementation Principles of various frameworks (transmission formats, how to convert transmission formats into streams, how to convert request information into transmission formats, how to receive streams, and how to restore a stream to transmission) format ), to be proficient in what needs to be determined based on actual needs, you can easily make choices only when you understand the principles, you can even implement private remote communication protocols as needed. In terms of distributed applications, I think at least the knowledge points mentioned above need to be better understood.
References:
1. 《
Java Remote Communication optional technology and principles
Http://java.chinaitlab.com/base/740383.html
2.
Hessian-3.2.0 source code
3.
A Preliminary Study on Hessian serialization implementation http://www.javaeye.com/topic/245238