(To) http://www.blogjava.net/BlueDavy/archive/2008/03/04/182077.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 thoroughly understand the mechanisms behind these technologies, in this blog, we will explore the future and 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 transmission protocols and network I/O defined for certain application scenarios. They mainly include 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.
Therefore, 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. It is generally optional for the transmission protocol, which is well-known in the Java field: 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 TCP/IP;
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 TCP/IP;
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?
TCP/IP.
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 to fill in the request information according to the XML-RPC protocol;
2. After filling, the XML is converted into a stream and transmitted through the transmission protocol;
3. After receiving the stream, it is converted to XML, and the request information is obtained and processed 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, both of which are HTTP + XML. The difference is that the XML norms defined by the two are different. Soap is also the service call consensus standard adopted by WebService. Therefore, we will not elaborate on it here.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
CORBA
Common Object Request Broker Architecture (Public
Use Object Request proxy [scheduling]ProgramSystem Structure) is a set of standards used to define the "Distributed Object System", initiated by OMG (Object Menagement group ).
And standard units. 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 are running on.
System.
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, however, 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. It can be called a message mechanism in a unified manner. What about a message mechanism, 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 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.
Answer:
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 transmission 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, and so on are used to describe and evaluate each type of service. In fact, to build a distributed service framework, we must have a very deep understanding of these things, 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 when I understand 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 TCP/IP + IO, RMI, and HTTP + IO.
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 TCP/IP 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.
parallel -----------------
Hessian
Hessian is a remote communication library provided by Caucho based on binary-RPC.
1. What protocol is implemented based on?
implemented based on binary-RPC protocol.
2. How to initiate a request?
requests must be initiated through the API 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 use to receive requests?
the response side receives requests based on the API provided by Hessian.
6. How to restore the stream to the transmission format?
Hessian deserializes request information based on its proprietary serialization mechanism. When it is passed to the user, it is already the corresponding request information object.
7. How can I respond after processing?
after the processing is completed, the result object is directly returned. Hessian serializes the result object and transmits it to the caller.
burlap ---------------
burlap
burlap is also provided by Caucho, which is different from Hessian in that it is based on the XML-RPC protocol.
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 use to receive requests?
listen to HTTP requests.
6. How to restore the stream to the transmission format?
restore Based on the XML-RPC protocol.
7. How can I respond after processing?
the returned results are written into XML and returned by burlap to the caller.
release -----------------
xfire, axis
xfire and axis are the implementation frameworks of WebService, webService is 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 use to receive requests?
listen to HTTP requests.
6. How to restore the stream to the 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.
strong -----------------
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?
I guess it is a binary stream.
4. What transmission protocol is used for transmission?
supports multiple transmission protocols, such as TCP/IP, UDP, and HTTP.
5. What mechanism does the response end use to receive requests?
listen to the Protocol-compliant ports.
6. How to restore the stream to the 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 Remote Communication Based on JMS include spring-intergration, mule, and lingo.
notice -----------------
Mina
Mina is a communication framework provided by Apache. I/O has never been mentioned before, the frameworks or libraries mentioned earlier are basically bio-based, while Mina uses NiO. Nio significantly improves performance compared with bio in terms of concurrency growth, while Java improves performance, close integration with NiO and OS is not small.
1. What protocol is implemented based on?
optional transfer protocol + 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 adopts the Java serialization mechanism to serialize request objects.
4. What transmission protocol is used for transmission?
supports multiple transmission protocols, such as TCP/IP and HTTP.
5. What mechanism does the response end use to receive requests?
listen to the protocol port in NIO mode.
6. How to restore the stream to the 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 in NIO mode, so it is no suspense to support asynchronous calls.
------------------------------------------------------------------------------------------------------------------------------- -----------------
EJB
the most prominent aspect of EJB is its distribution. EJB adopts the ormi protocol, it is similar to the RMI protocol, but the outstanding Security Control, Transport Pool, smart proxy, and other aspects of distributed communication of EJB make it an important force in the distributed field.
1. What protocol is implemented based on?
Based on the ormi protocol.
2. How to initiate a request?
EJB call.
3. How can I convert a request to a protocol-compliant format?
follow the Java serialization mechanism to serialize the request object.
4. What transmission protocol is used for transmission?
TCP/IP.
5. What mechanism does the response end use to receive requests?
listen to the protocol port.
6. How to restore the stream to the transmission format?
follow the Java serialization mechanism to deserialize the request object.
7. How can I respond after processing?
return the processing object directly.
suspected that JNDI is misleading in the Article of the previous distributed service framework series, in this blog, we also mentioned the mechanism of JNDI. Since JNDI depends on the specific implementation, we can only explain the implementation of JBoss's JNDI here.
after the object instance is bound to the JBoss jnp server, the remote client 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.
with this mechanism, you can understand that the local database 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.
but 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 or remote call protocol (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 the original stream for 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 develop private remote communication protocols based on your needs. For large distributed applications, I think the knowledge points mentioned above at least need to be understood.
References (thanks to these articles)
RMI Principle and Implementation: http://www.yesky.com/274/1625274.shtml
Java NiO principle and use: http://www.jdon.com/concurrent/nio%D4%AD%C0%ED%D3%A6%D3%C3.htm
Xml rpc protocol: http://hedong.3322.org/archives/000470.html
Http://www.mengyan.org/blog/archives/2005/07/12/30.html
Spring technology application of remote service details: http://www.builder.com.cn/2007/1027/583384.shtml
Java RPC communication Mechanism-soap: Http://www.java114.com/content16/content3826.html
Java remoting: Protocol benchmarks: http://q.sohu.com/forum/5/topic/1148909
Evalution of RMI alternative: https://www.jfire.org/modules/phpwiki/index.php/Evaluation%20of%20RMI%20Alternative
Metaprotocol taxonomy: http://hessian.caucho.com/doc/metaprotocol-taxonomy.xtp
What is WebService: http://www.vchome.net/dotnet/webservice/webservice15.htm