Java Remote communication technology and principle analysis

Source: Internet
Author: User
Tags soap stub jboss msmq ibm mq wsdl

In a distributed service framework, 一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术 such as RMI, MINA, ESB, Burlap, Hessian, SOAP, EJB, and JMS, what is the relationship between these nouns, and what is the rationale behind them? Understanding these is the basic knowledge of implementing a distributed service framework, and if there is a high performance requirement, then it is necessary to understand the mechanics behind these technologies.

1 Fundamentals

In order to realize the communication between network machines, we should first look at the basic principle of network communication in computer system, 在底层层面去看,网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络IO来实现,其中传输协议比较出名的有tcp、udp等等,tcp、udp都是在基于Socket概念上为某类应用场景而扩展出的传输协议,网络IO,主要有bio、nio、aio三种方式,所有的分布式应用通讯都基于这个原理而实现 and just for the ease of application, the various languages usually provide some application layer protocols which are more easy to apply.

2 message Mode

In the final analysis, enterprise Application system is the processing of data, and for a multi-subsystem enterprise Application System, its basic support is undoubtedly the processing of messages. 与对象不同,消息本质上是一种数据结构(当然,对象也可以看做是一种特殊的消息),它包含消费者与服务双方都能识别的数据,这些数据需要在不同的进程(机器)之间进行传递,并可能会被多个完全不同的客户端消费 . Message delivery seems to be better than file delivery and remote procedure call (RPC) because it has better platform independence and is well-supported for concurrent and asynchronous calls.

对于Web Service与RESTful而言,则可以看做是消息传递技术的一种衍生或封装。

2.1 Message Channel Mode

The message pattern that we often use is the message channel mode.

消息通道作为在客户端(消费者,Consumer)与服务(生产者,Producer)之间引入的间接层,可以有效地解除二者之间的耦合。The consumer's "ignorance" of the producer can be achieved by implementing a message format that requires communication between the two parties, as well as the mechanism and timing for processing the message. 事实上,该模式可以支持多个生产者与消费者 . For example, we can have multiple producers send messages to the message channel because consumers are ignorant of the producers, and it does not have to consider which producer sent the message.

While the message channel relieves the coupling between producer and consumer, it allows us to arbitrarily expand the producer and consumer 但它又同时引入了各自对消息通道的依赖,因为它们必须知道通道资源的位置 . 要解除这种对通道的依赖,可以考虑引入Lookup服务来查找该通道资源. For example, in JMS, the message channel queue can be obtained through JNDI. For full flexibility, you can store channel-related information in a configuration file, and the lookup service first obtains the channel by reading the configuration file.

消息通道通常以队列的形式存在,这种先进先出的数据结构无疑最为适合这种处理消息的场景。 Microsoft's MSMQ, IBM MQ, JBoss MQ, and Open source RABBITMQ, Apache ACTIVEMQ all implemented the message channel pattern through the queue. Therefore, in choosing to use the Message channel mode, it is more necessary to analyze and weigh all kinds of products that implement the model from the aspect of quality attribute. For example, 消息通道对并发的支持以及在性能上的表现;消息通道是否充分地考虑了错误处理;对消息安全的支持;以及关于消息持久化、灾备(fail over)与集群等方面的支持 .

因为通道传递的消息往往是一些重要的业务数据,一旦通道成为故障点或安全性的突破点,对系统就会造成灾难性的影响。

The mechanism of Jndi is also mentioned here, because Jndi depends on the specific implementation, which can only be explained in the implementation of JBoss Jndi:

When the object instance is bound to JBoss JNP server, when the remote end obtains the remote object instance in Context.lookup () mode and begins the call, the implementation of JBoss Jndi is to get the object instance from JNP server, serialize it back to local, It is then deserialized locally, and then the class is called locally.

Through this mechanism, we can know that the local actually must have bound to the JBoss object instance class, otherwise the deserialization will certainly fail, and the remote communication needs to do is to perform a remote operation, and obtain the corresponding results, can be seen purely based on Jndi is unable to achieve remote communication.

But Jndi is also a key technical point for implementing a distributed service framework, because it allows for transparent remote and local calls, like EJBs, and it's a good way to hide actual deployment mechanisms (like DataSource).

2.2 Publisher-Subscriber (Publisher-subscriber) mode

Once the message channel needs to support multiple consumers, it is possible to face the choice of two models: Pull model and push model. 拉模型是由消息的消费者发起的,主动权把握在消费者手中,它会根据自己的情况对生产者发起调用. :

Another embodiment of the pull model is that the producer notifies the consumer when the state has changed. However, the notified consumer will get more details by calling the passed-in consumer object in a callback manner.

In a message-based distributed system, the consumers of the pull model usually listen to the channel periodically in the form of a batch job, based on a predetermined interval of time. Once a message is found to be delivered, it will instead pass the message to the real processor (or to the consumer) to process the message and execute the relevant business.

推模型的主动权常常掌握在生产者手中,消费者被动地等待生产者发出的通知,这就要求生产者必须了解消费者的相关信息。 :

For push models, consumers do not need to know the producers. When the producer notifies the consumer, it is often the message (or event) that is passed, not the producer itself. At the same time, producers can also register different consumers according to different circumstances, or in the packaging of the notification logic, according to different state changes, notify different consumers.

Both models have the advantage. 拉模型的好处在于可以进一步解除消费者对通道的依赖,通过后台任务去定期访问消息通道。坏处是需要引入一个单独的服务进程,以Schedule形式执行. And for 推模型而言,消息通道事实上会作为消费者观察的主体,一旦发现消息进入,就会通知消费者执行对消息的处理 . 无论推模型,拉模型,对于消息对象而言,都可能采用类似Observer模式的机制,实现消费者对生产者的订阅,因此这种机制通常又被称为Publisher-Subscriber模式 ,:

Typically, publishers and Subscribers are registered to the infrastructure that is used to propagate the changes (that is, the message channel). 发布者会主动地了解消息通道,使其能够将消息发送到通道中;消息通道一旦接收到消息,会主动地调用注册在通道中的订阅者,进而完成对消息内容的消费.

For subscribers, there are two ways to process messages. 一种方式是广播机制,这时消息通道中的消息在出列的同时,还需要复制消息对象,将消息传递给多个订阅者 . For example, there are several subsystems that need to obtain customer information from the CRM system, and according to the delivery of customer information, the corresponding processing. The message channel at this point is also known as the Propagation channel. 另一种方式则属于抢占机制,它遵循同步方式,在同一时间只能有一个订阅者能够处理该消息 . The message channel that implements the Publisher-subscriber mode selects the currently idle unique subscriber and then dequeue the message and passes it to the subscriber's message handling method.

Currently, there are many message middleware that can support the publisher-subscriber pattern, such as the Messagepublisher and Messagesubscriber interfaces provided in the JMS interface protocol for topic objects. RabbitMQ也提供了自己对该模式的实现 . Microsoft's MSMQ, while introducing an event mechanism, can trigger events to notify subscribers when a message is received by the queue. But it is not the publisher-subscriber pattern implementation in the strict sense. Nservicebus, a major contributor to the Microsoft MVP Udi Dahan, has done a layer of packaging for MSMQ and WCF, and is well-implemented in this model.

2.3 Message Routing (MSG Router) mode

Whether it is the message channel mode or the Publisher-subscriber mode, the queue plays a pivotal role in it. However, in the enterprise application system, the 当系统变得越来越复杂时,对性能的要求也会越来越高,此时对于系统而言,可能就需要支持同时部署多个队列,并可能要求分布式部署不同的队列 . These queues can receive different messages by definition, such as order Processing messages, log messages, query task messages, and so on. At this point, it is not appropriate for the producer and consumer of the message to assume responsibility for determining the message delivery path. 事实上,根据S单一职责原则,这种职责分配也是不合理的,它既不利于业务逻辑的重用,也会造成生产者、消费者与消息队列之间的耦合,从而影响系统的扩展.

既然这三种对象(组件)都不宜承担这样的职责,就有必要引入一个新的对象专门负责传递路径选择的功能,这就是所谓的Message Router模式,:

With message routing, we can configure routing rules to specify the path to the message delivery, as well as specify the specific consumer consumer for the corresponding producer. For example, specify the keyword for the route, and it binds the specific queue to the specified producer (or consumer). 路由的支持提供了消息传递与处理的灵活性,也有利于提高整个系统的消息处理能力. At the same time, the routing object effectively encapsulates the logic of finding and matching the message path, as if it were a mediator (meditator), responsible for coordinating the relationship between messages, queues, and path addressing.

3 Application-level protocols

Remote service communication, the goal that needs to be achieved is 在一台计算机发起请求,另外一台机器在接收到请求后进行相应的处理并将结果返回给请求端,这其中又会有诸如one way request、同步请求、异步请求等等请求方式,按照网络通信原理,需要实现这个需要做的就是将请求转换成流,通过传输协议传输至远端,远端计算机在接收到请求的流后进行处理,处理完毕后将结果转化为流,并通过传输协议返回给调用端 .

The principle is this, but for the convenience of application, the industry has introduced a lot of application-level protocols based on this principle, so that people can not go directly to operate such a low level of things, usually application-grade remote communication protocol will provide:

    1. In order to avoid the direct flow operation so troublesome 提供一种更加易用或贴合语言的标准传输格式 ;
    2. The realization of network communication mechanism is that 替你完成了将传输格式转化为流 , through some kind of transmission protocol to the remote computer, the remote computer is converted into a transmission format after receiving the stream and is stored or notified to the remote computer in some way.

So when learning about application-level remote communication protocols, we can learn with these questions:

    1. What is the standard format for transmission?
    2. How do I convert a request to a streamed stream?
    3. How to receive and process streams?
    4. What is the transport protocol?

However, the application-level remote communication protocol does not make much improvement on the transport protocol, mainly in the field of flow operations, 让应用层生成流和处理流的这个过程更加的贴合所使用的语言或标准,至于传输协议则通常都是可选的 in the Java domain is well-known: RMI、XML-RPC、Binary-RPC、SOAP、CORBA、JMS、HTTP , to specifically look at these remote communication application-level protocol.

3.1 RMI (remote method call)

RMI是个典型的为java定制的远程通信协议, as we all know, in a single VM, we can communicate by invoking the Java object instance directly, so if it is the best way to do this in remote communication, the mechanism of this kind of remote communication becomes RPC (remotes Procedure Call), RMI was born towards that goal.

RMI 采用stubs 和 skeletons 来进行远程对象(remote object)的通讯。stub 充当远程对象的客户端代理,有着和远程对象相同的远程接口,远程对象的调用实际是通过调用该对象的客户端代理对象stub来完成的,通过该机制RMI就好比它是本地工作,采用tcp/ip协议,客户端直接调用服务端上的一些方法。优点是强类型,编译期可检查错误,缺点是只能基于JAVA语言,客户机与服务器紧耦合。

Consider the principle of a complete remote communication process based on RMI:

  1. The client initiates the request and requests the stub class to be forwarded to the RMI client;

  2. The stub class serializes the requested interface, method, parameter, and other information;

  3. Based on the socket, the serialized stream is transmitted to the server side;

  4. The server side receives the stream and forwards it to the corresponding Skelton class;

  5. The Skelton class invokes the actual processing class after deserializing the requested information;

  6. After processing the class processing, the result is returned to the Skelton class;

  7. The Skelton class serializes the result and sends it to the client stub through the socket;

  8. The stub is deserialized after it receives the stream and returns the deserialized Java object to the caller.

Follow the principles to answer a few questions before learning the application-level protocol:

    1. What is the standard format for transmission?

      Is the Java Objectstream.

    2. How do I convert a request to a streamed stream?

      Converts the requested Java object information into a stream based on the Java serialization mechanism.

    3. How to receive and process streams?

      According to the protocol used to start the corresponding listening port, when there is a stream into the system based on the Java serialization mechanism to deserialize the stream, and according to the RMI protocol to obtain the corresponding processing object information, call and processing, the finished result is also based on the Java serialization mechanism to return.

    4. What is the transport protocol?

      Socket.

3.2 Xml-rpc

RPC uses the C/s method, 采用http协议 sends the request to the server, waits for the server to return the result. 这个请求包括一个参数集和一个文本集,通常形成“classname.methodname”形式. The advantage is cross-language cross-platform, c-side, s-side has greater independence, the disadvantage is that the object is not supported, the compiler can not check the error, only at run time check.

XML-RPC is also an RMI-like protocol for remote invocation, it and RMI 不同之处在于它以标准的xml格式来定义请求的信息(请求的对象、方法、参数等),这样的好处是什么呢,就是在跨语言通讯的时候也可以使用 .

Take a look at a remote communication process for the XML-RPC protocol:

  1. The client initiates the request and populates the request information according to the XML-RPC protocol;

  2. After filling, the XML is transformed into a stream and transmitted through the transport Protocol.

  3. Received after receiving the stream into XML, according to the XML-RPC protocol to obtain the requested information and processing;

  4. After processing, the results are written in XML and returned in accordance with the XML-RPC protocol.

To answer the question:

    1. What is the standard format for transmission?

      XML in standard format.

    2. How do I convert a request to a streamed stream?

      Converts XML into a stream.

    3. How to receive and process streams?

      Gets the requested stream through the listening port, translates to XML, and obtains the requested information according to the protocol, processes it and returns the result to the XML.

    4. What is the transport protocol?

      Http.

3.3 Binary-rpc

Binary-rpc look at the name to know and Xml-rpc is similar, 不同之处仅在于传输的标准格式由XML转为了二进制的格式 .

To answer the question:

    1. What is the standard format for transmission?

      A binary file in standard format.

    2. How do I convert a request to a streamed stream?

      Converts binary format files to streams.

    3. How to receive and process streams?

      Gets the requested stream through the listening port, translates into a binary file, obtains the requested information according to the protocol, processes it, and returns the result to the XML.

    4. What is the transport protocol?

      Http.

3.4 SOAP

Soap is originally simple Object Access Protocol, is a distributed environment, lightweight, XML-based information exchange protocol, you can think of soap is an advanced version of XML RPC, the principle of the two are identical, are http+xml, 不同的仅在于两者定义的XML规范不同,SOAP也是Webservice采用的服务调用协议标准, so there is no more elaboration.

WEB Service provides services that are 基于web容器的,底层使用http协议 similar to a remote service provider, such as a weather forecast service, that provides weather forecasts to clients around the world 是一种请求应答的机制,是跨系统跨平台的 . is to provide services out through a servlet.

首先客户端从服务器获得WebService的WSDL,同时在客户端生成一个代理类(Proxy Class),这个代理类负责与WebService服务器进行Request和Response。当一个数据(XML格式的)被封装成SOAP格式的数据流发送到服务器端的时候,就会生成一个进程对象并且把接收到这个Request的SOAP包进行解析,然后对事物进行处理,处理结束以后再对这个计算结果进行SOAP包装,然后把这个包作为一个Response发送给客户端的代理类(Proxy Class),同样地,这个代理类也对这个SOAP包进行解析处理,继而进行后续操作。这就是WebService的一个运行过程。

A WEB service is broadly divided into 5 levels:

  1. HTTP transmission channel;

  2. The data format of XML;

  3. SOAP encapsulation format;

  4. how WSDL is described;

  5. UDDI UDDI is a directory service that enterprises can use to register and search for webservices;

3.5 JMS

JMS is a means and method of implementing remote communication in the Java domain, 基于JMS实现远程通信时和RPC是不同的,虽然可以做到RPC的效果,但因为不是从协议级别定义的 so we don't think of JMS as an RPC protocol, and 但它确实是个远程通信协议 there is something like JMS in other language systems 可以统一的将这类机制称为消息机制,而消息机制呢,通常是高并发、分布式领域推荐的一种通信机制,这里的主要一个问题是容错 .

JMS is a Java messaging service that allows asynchronous message transmission between JMS clients through the JMS service. JMS支持两种消息模型:Point-to-Point(P2P)和Publish/Subscribe(Pub/Sub),即点对点和发布订阅模型 .

Consider the process of a remote communication in JMS:

  1. The client translates the request into a JMS-compliant message;

  2. Put a message into a JMS queue or topic through the JMS API;

  3. In the case of a JMS queue, the corresponding target Queue in the send, such as topic, is sent to the JMS queue subscribed to this topic.

  4. The processing end obtains the message through the rotation JMS Queue, and receives the message and resolves it and processes it according to the JMS protocol.

To answer the question:

    1. What is the standard format for transmission?

      The message specified by JMS.

    2. How do I convert a request to a streamed stream?

      Put the parameter information in the message.

    3. How to receive and process streams?

      Rotation a JMS queue to receive a message, receive it for processing, and still send or multicast in the queue as a message after processing is complete.

    4. What is the transport protocol?

      Unlimited.

基于JMS也是常用的实现远程异步调用的方法之一。

4 Differences between 4.1 RPC and RMI
  1. RPC跨语言,而RMI只支持Java

  2. RMI calls the remote object method, 允许方法返回Java对象以及基本数据类型 while RPC does not support the concept of an object, the message that is transmitted to the RPC service is represented by the external data representation (External data representation, XDR) language 这种语言抽象了字节序类和数据类型结构之间的差异 . 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. 如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新方法就不能被RMI客户方所调用.

    In RPC, the 当一个请求到达RPC服务器时,这个请求就包含了一个参数集和一个文本值,通常形成“classname.methodname”的形式 . This indicates to the RPC server that the requested method is in the class "ClassName", which is called "MethodName". 然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入. 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.

  4. RPC itself has no specifications, but the basic work mechanism is the same, namely: Serialization/deserialization+stub+skeleton, 宽泛的讲,只要能实现远程调用,都是RPC such as: RMI. net-remoting ws/soap/rest Hessian XMLRPC Thrift Potocolbuffer.

  5. The complete sockets communication interface is provided in Java, but the sockets requires that the client and the server must have an application-level protocol encoded to exchange data, which is cumbersome to use sockets. 一个代替Sockets的协议是RPC(Remote Procedure Call), 它抽象出了通讯接口用于过程调用,使得编程者调用一个远程过程和调用本地过程同样方便.

    The RPC system uses XDR to encode parameters and return values for remote calls. 但RPC并不支持对象,所以,面向对象的远程调用RMI(Remote Method Invocation)成为必然选择. With RMI, it is also convenient to invoke remote objects and invoke local objects. RMI uses JRMP (Java Remote method Protocol) communication protocol, which is a kind of remote calling method built on TCP/IP protocol.

4.2 JMS and RMI
    1. With the JMS service, and 对象是在物理上被异步从网络的某个JVM 上直接移动到另一个JVM 上(是消息通知机制) RMI对象是绑定在本地JVM 中,只有函数参数和返回值是通过网络传送的(是请求应答机制) .

    2. RMI一般都是同步的, that is, when the client calls a method of the server, it needs to wait for the other side to return, in order to continue to execute the client side, the process calls the local method feels the same, this is a feature of RMI.

      JMS is generally just a point to send a message to message Server, which generally does not care who uses this message.

      So, 一般RMI的应用是紧耦合,JMS的应用相对来说是松散耦合应用 .

4.3 WebService and RMI

RMI是在tcp协议上传递可序列化的java对象, can only be used on Java virtual machines, the binding language, both the client and the server must be java. WebService does not have this limitation webservice是在http协议上传递xml文本文件,与语言和平台无关 .

4.4 WebService and JMS

Webservice专注于远程服务调用,jms专注于信息交换

Most of Webservice是两系统间的直接交互(Consumer <--> Producer) the cases, and most of the cases jms是三方系统交互(Consumer <- Broker -> Producer) . Of course, JMS也可以实现request-response模式的通信,只要Consumer或Producer其中一方兼任broker即可 .

JMS可以做到异步调用完全隔离了客户端和服务提供者,能够抵御流量洪峰WebService服务通常为同步调用,需要有复杂的对象转换, compared to soap, now json,rest is a good HTTP architecture scheme;

JMS is a message specification on the Java platform. 一般jms消息不是一个xml,而是一个java对象,很明显,jms没考虑异构系统, frankly speaking, JMS does not consider things that are not java. 但是好在现在大多数的jms provider(就是JMS的各种实现产品)都解决了异构问题. It's different than WebService's cross-platform.

5 Optional Implementation Technology

The current Java domain can be used to implement remote communication framework or library, known as: Jboss-remoting, spring-remoting, Hessian, Burlap, XFire (Axis), ActiveMQ, Mina, Mule , EJB3 and so on, to do a simple introduction and evaluation of each kind 其实呢,要做分布式服务框架,这些东西都是要有非常深刻的了解的,因为分布式服务框架其实是包含了解决分布式领域以及应用层面领域两方面问题的 .

Of course, you can also implement your own communication framework or library based on the principle of Remote network communication (transport Protocol+net IO).

So what's the problem with learning about the framework or library of these remote communications?

    1. Based on what protocol is implemented?
    2. How do I initiate a request?
    3. How do I convert a request into a protocol-compliant format?
    4. What transport protocol is used for transmission?
    5. What mechanism is the response end based on to receive requests?
    6. How do I restore a stream to a transport format?
    7. How do I respond when I have finished processing?
5.1 spring-remoting

Spring-remoting is a remote communication framework provided by spring that provides the Java domain, and it is also possible to easily publish ordinary spring beans as a remote protocol, as well as to configure the bean that the spring bean calls remotely.

    1. Based on what protocol is implemented?

      As a framework for remote communication, Spring provides support for multiple protocols, such as RMI, Http+io, XML-RPC, BINARY-RPC, through the integration of multiple remote communication libraries.

    2. How do I initiate a request?

      In spring, because of its proxy implementation for a remote invocation of the bean, the originating request is made entirely through the service interface invocation.

    3. How do I convert a request into a protocol-compliant format?

      Spring converts the requested object information into a stream by protocol, for example, Spring HTTP Invoker is implemented based on a protocol defined by spring, and HTTP is used on the transport Protocol, and the request information is converted to a stream based on the Java serialization mechanism.

    4. What transport protocol is used for transmission?

      Supports a variety of transport protocols, such as RMI, HTTP, and so on.

    5. What mechanism is the response end based on to receive requests?

      The response side follows the protocol to receive the request, and for the consumer, it simply configures the normal spring bean as the response end or provides the service side through spring configuration.

    6. How do I restore a stream to a transport format?

      Follow the protocol to restore.

    7. How do I respond when I have finished processing?

      After processing is completed, the spring-remoting will be serialized according to the Protocol method.

5.2 Hessian

Hessian is a remote communication library provided by Caucho based on BINARY-RPC implementation.

    1. Based on what protocol is implemented?

      Implementation based on the BINARY-RPC protocol.

    2. How do I initiate a request?

      The request needs to be initiated through the API provided by Hessian itself.

    3. How do I convert a request into a protocol-compliant format?

      The Hessian uses its own serialization mechanism to serialize the request information, resulting in a binary stream.

    4. What transport protocol is used for transmission?

      The Hessian is transmitted based on the HTTP protocol.

    5. What mechanism is the response end based on to receive requests?

      The response side receives the request based on the API provided by Hessian.

    6. How do I restore a stream to a transport format?

      Hessian the request information according to its private serialization mechanism, which is the corresponding request information object when passed to the consumer.

    7. How do I respond when I have finished processing?

      After processing, the Hessian returns the resulting object, which is then serialized and transmitted to the calling end.

5.3 Burlap

Burlap is also provided by Caucho, which differs from Hessian in that it is based on the XML-RPC protocol.

    1. Based on what protocol is implemented?

      Implementation based on the XML-RPC protocol.

    2. How do I initiate a request?

      Based on the API provided by burlap.

    3. How do I convert a request into a protocol-compliant format?

      Converts the request information into a protocol-compliant XML format and translates it into a stream for transmission.

    4. What transport protocol is used for transmission?

      HTTP protocol.

    5. What mechanism is the response end based on to receive requests?

      Listens for HTTP requests.

    6. How do I restore a stream to a transport format?

      Restore according to the XML-RPC protocol.

    7. How do I respond when I have finished processing?

      The returned results are written to the XML and returned by burlap to the caller.

5.4 XFire, Axis

XFire, Axis is the WebService implementation framework, WebService can be considered as a complete SOA architecture implementation standard, so the use of XFire, axis These also means the use of WebService way.

    1. Based on what protocol is implemented?

      Based on the SOAP protocol.

    2. How do I initiate a request?

      Called directly after obtaining a proxy to the remote service.

    3. How do I convert a request into a protocol-compliant format?

      The request information is converted into an XML format that follows the SOAP protocol, and the framework is transformed into a stream for transmission.

    4. What transport protocol is used for transmission?

      HTTP protocol.

    5. What mechanism is the response end based on to receive requests?

      Listens for HTTP requests.

    6. How do I restore a stream to a transport format?

      Restore based on the SOAP protocol.

    7. How do I respond when I have finished processing?

      The returned results are written to the XML and returned by the framework to the calling side.

5.5 ActiveMQ

ACTIVEMQ is the implementation of JMS, and it is a good choice to realize remote communication based on the message mechanism of JMS, 毕竟消息机制本身的功能使得基于它可以很容易的去实现同步/异步/单向调用等,而且消息机制从容错角度上来说也是个不错的选择 which is an important basis for Erlang to be fault tolerant.

    1. Based on what protocol is implemented?

      Based on the JMS protocol.

    2. How do I initiate a request?

      Follow the JMS API to initiate the request.

    3. How do I convert a request into a protocol-compliant format?

      It is not clear that the conjecture should be a binary stream.

    4. What transport protocol is used for transmission?

      Supports a variety of transport protocols, such as sockets, HTTP, and so on.

    5. What mechanism is the response end based on to receive requests?

      Listen for ports that are compliant with the protocol.

    6. How do I restore a stream to a transport format?

      With question 3.

    7. How do I respond when I have finished processing?

      Follow the JMS API to generate the message and write it to the JMS queue.

5.6 Mina

Mina is an Apache-provided communication framework that has not previously mentioned network IO, and that the framework or library mentioned earlier is essentially bio-based Mina是采用NIO的,NIO在并发量增长时对比BIO而言会有明显的性能提升,而java性能的提升,与其NIO这块与OS的紧密结合是有不小的关系的 .

    1. Based on what protocol is implemented?

      Based purely on the Socket+nio.

    2. How do I initiate a request?

      The client API provided through Mina.

    3. How do I convert a request into a protocol-compliant format?

      The Mina follows the Java serialization mechanism to serialize the request object.

    4. What transport protocol is used for transmission?

      Supports a variety of transport protocols, such as sockets, HTTP, and so on.

    5. What mechanism is the response end based on to receive requests?

      The protocol port is monitored in NIO mode.

    6. How do I restore a stream to a transport format?

      The request object is deserialized by following the Java serialization mechanism.

    7. How do I respond when I have finished processing?

      Follow the Mina API to return.

MINA是NIO方式的,因此支持异步调用是毫无悬念的。

6 development and status of RPC framework

RPC(Remote Procedure Call)是一种远程调用协议,简单地说就是能使应用像调用本地方法一样的调用远程的过程或服务,可以应用在分布式服务、分布式计算、远程服务调用等许多场景。 Talking about RPC Everyone is not unfamiliar, the industry has a lot of good open source RPC framework, such as Dubbo, Thrift, Grpc, Hprose and so on. The following is a brief introduction to the RPC and common remote invocation of the characteristics, as well as some excellent open source RPC framework.

RPC 与其它远程调用方式比较,RPC 与 HTTP、RMI、Web Service 都能完成远程调用,但是实现方式和侧重点各有不同。

6.1 RPC with HTTP

HTTP (hypertext Transfer Protocol) is an application-layer communication protocol that uses standard semantics to access a specified resource (picture, interface, and so on), and the brokered server in the network can identify the protocol content. HTTP 协议是一种资源访问协议,通过 HTTP 协议可以完成远程请求并返回请求结果.

The advantages of HTTP are simple, easy-to-use, understandable and language-independent, and are widely used in remote service invocation including Weibo. HTTP 的缺点是协议头较重,一般请求到具体服务器的链路较长,可能会有 DNS 解析、Nginx 代理等.

RPC is a protocol specification 可以把 HTTP 看作是一种 RPC 的实现,也可以把 HTTP 作为 RPC 的传输协议来应用 . RPC services are highly automated, enabling powerful service governance capabilities, and a more user-friendly language and excellent performance. Compared with HTTP, the disadvantage of RPC is relatively complex, the learning cost is slightly higher.

6.2 RPC and RMI

RMI (remote method invocation) refers to the method invocation in the Java language, in which each method in the RMI has an approach signature, and the RMI client and server side make remote method calls through the method signature. RMI 只能在 Java 语言中使用,可以把 RMI 看作面向对象的 Java RPC.

6.3 RPC with Web Service

Web Service 是一种基于 Web 进行服务发布、查询、调用的架构方式,重点在于服务的管理与使用。 A WEB service typically describes a service through WSDL 使用 SOAP通过 HTTP 调用服务 .

RPC 是一种远程访问协议,而 Web Service 是一种体系结构, the Web service can also make service calls through RPC, so the Web service is better suited for comparison with the same RPC framework. 当 RPC 框架提供了服务的发现与管理,并使用 HTTP 作为传输协议时,其实就是 Web Service.

The relative Web SERVICE,RPC Framework provides finer-grained governance of services, including traffic control, SLA management, and more, with a greater advantage in microservices and distributed computing.

RPC 可基于 HTTP 或 TCP 协议,Web Service 就是基于 HTTP 协议的 RPC, it has good cross-platform, but its performance is inferior to RPC based on TCP protocol. The performance of RPC can be directly affected by the two aspects 一是传输方式,二是序列化 .

It is well known that, TCP 是传输层协议,HTTP 是应用层协议 while the transport layer is more low-level than the application layer, in the data transmission, the lower the faster, therefore, in general, TCP must be faster than HTTP.

7 Summary

In the field of remote communication, the knowledge points are still quite large, for example: 通信协议(Socket/tcp/http/udp/rmi/xml-rpc etc.)、消息机制、网络IO(BIO/NIO/AIO)、MultiThread、本地调用与远程调用的透明化方案(涉及java classloader、Dynamic Proxy、Unit Test etc.)、异步与同步调用、网络通信处理机制(自动重连、广播、异常、池处理等等)、Java Serialization (各种协议的私有序列化机制等)、各种框架的实现原理(传输格式、如何将传输格式转化为流的、如何将请求信息转化为传输格式的、如何接收流的、如何将流还原为传输格式的等等) , to be proficient in which things, according to the actual needs of the decision, only in the case of understanding the principle of the situation can be easily made to choose, even according to the requirements of the Private remote communication protocol, For those of you working on a distributed service platform or developing a larger distributed application, I think at least the knowledge points mentioned above need to be better understood.

Java Remote communication technology and principle analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.