Fundamentals and practices of distributed Java Applications reading notes one

Source: Internet
Author: User

The architectural knowledge of distributed Java applications is simply divided into:

    • Network communication: Includes protocol and IO
    • Inter-system communication for messaging: Includes Java-based packages, open-source frameworks, Performance perspectives
    • Inter-system communication by remote invocation: Includes Java package based, open source framework, performance perspective

Large applications are split into subsystems that may be deployed on the same machine or in several different jvms of different machines, one for each JVM. But these subsystems are not completely independent, to communicate with each other to achieve business functions, for such Java reference, we call it a distributed Java reference. There are usually two typical ways to achieve this.

    • The communication between systems based on information is usually implemented based on network protocol.
    • Implement communication between systems based on remote invocation, usually based on RMI and WebService
Message mode based on Java's own technology

The methods of communication between systems based on the Java package are:

    • Tcp/ip+bio
    • Tcp/ip+nio
    • Udp/ip+bio
    • Udp/ip+nio
Tcp/ip+bio

In Java, it is possible to implement TCP/IP+BIO communication between systems based on sockets and ServerSocket. The socket is used to establish the connection and network IO, ServerSocket is used for monitoring the server port and acquiring the socket object.   This simple communication mode, applied to the actual system, usually need to face is the client to send multiple requests to the server side, the server side is also to accept multiple connections to send requests. To address the need for clients to send multiple requests to the server side at the same time, the workaround is:

    • Generating multiple sockets is the problem of consuming too many local resources on the client side, consuming too many connections on the server, and reducing the system performance due to long connections. The workaround is to maintain the socket by using a connection pooling method. The problem with connection pooling is the limited number of sockets that result in competition and waiting and how to reasonably control the time-out for waiting for a response.

In order to satisfy the request that the service side can accept multiple connections simultaneously, the solution is:

    • After the accept socket is acquired, the socket is processed in a thread, called a connection. The disadvantage is that a thread is consumed regardless of whether there is a real request on the connection. To prevent server-side resource exhaustion caused by too many threads, you must limit the number of threads created, and the number of connections that can be supported by the server side in Bio mode is limited.
Tcp/ip+nio

In Java, the communication between systems in Tcp/ip+nio mode can be implemented based on the correlation classes of channel and selector in Java.nio.channels. The Socketchannel is used to establish connections, listen for events and read and write, Serversocketchannel to listen for ports and listen for connection events, and the program selector to get events to be handled. NIO is a typical implementation of the reactor model by registering events of interest and scanning for events that are of interest, thus doing the corresponding actions. This simple communication mode, applied to the actual system, usually need to face is the client to send multiple requests to the server side, the server side is also to accept multiple connections to send requests.

To address the need for clients to send multiple requests to the server side at the same time, the workaround is:

    • Using multiple Socketchannel in a similar way to Tcp/ip+bio. However, the NIO method can be done without blocking, if the server side returns the corresponding can bring the request identity, the client can use a connection multiplexing. For cases where the connection is not used, the time-out of the synchronization request can be controlled based on Socket.setsotimeout, and for a connection reuse, the timeout for the synchronization request can be based on blockingqueue, the wait/notify mechanism of the object, or the future mechanism.

To address the need for the server to accept multiple connection requests, the workaround is to:

    • A thread listens for connected events, while another or more threads listen to the time the network stream reads and writes. When there is an actual network stream read-write event occurs, it is then put into the thread pool for processing. This approach is called a request-first thread, and the benefit is that many connections are accepted, and these connections are only created when there is a real request to process the thread. Great for service-side needs to support a large number of connections, but these connections do not send too many requests at the same time.

For high-traffic systems, the Tcp/ip+nio approach combined with a certain transformation in the client can bring higher performance, on the server side can support the higher number of connections.

Udp/ip+bio

Java to the UDP/IP mode of network data transmission also uses the socket mechanism, just do not establish a connection requirements, and can not communicate in both directions, unless both ends are UDP Server. In Java can be based on datagramsocket and datagrampacket to achieve Udp/ip+bio mode of inter-system communication, Datagramsocket is responsible for listening to the port and read and write data. The datagrampacket is transmitted as a data flow object.

UDP/IP communication mode of the two ends do not establish a connection, there will be no TCP/IP Communication connection competition problem, but the final reading and writing flow is synchronous. A client that sends multiple messages simultaneously can use multiple datagrampacket. The server receives multiple requests at the same time, which is implemented in a thread without receiving a packet.

Udp/ip+nio

In Java through the Datagramchannel and bytebuffer to achieve Udp/ip+nio mode of inter-system communication, Datagramchannel is responsible for listening to the port and read and write, Bytebuffer for data flow transmission.

For the UDP/IP approach, the benefit of NIO is that the corresponding IO operation is done only when there is a stream to read or write to the stream, rather than blocking the current thread directly like the bio.

Implementation of multicast

The above is a one-to-one, Java-based communication between systems, in the actual scenario, you may need to send messages to multiple machines, there are two solutions for this scenario:

    • Establish a connection for each target machine, with the disadvantage of causing heavy network traffic pressure on the message sending side
    • Multicast network communication can be implemented based on MulticastSocket and Datagrampakcet in Java using multicast protocol based on UDP/IP extension. In multicast communication, the receiving data end joins the multicast group to receive the data, the sending data also requests to join the multicast group to send, the multicast destination address has the specified address range, between 224.0.0.0 and 239.255.255.255.

In Java applications, multicasting is typically used to synchronize the state of multiple machines.

Based on open source framework

Mina is Apache's top-tier project, built on Java NIO, supports both TCP/IP and UDP/IP two protocols, Mina the complexity of Java NIO usage and optimizes performance.

When using Mina, the key classes encapsulate the process of sending and receiving messages in the form of filter Iosession,mina for Ioconnector, Ioacceptor, Iohandler, and chain, where the filter In the process of chain, messages can be processed, messages sent and received.

    • Ioconnector is responsible for configuring the client's message processor, IO event processing thread pool, message sending/receiving filter Chain, and so on.
    • The ioacceptor is responsible for configuring the server-side IO Event processing thread pool, message sending/receiving filter chain, and so on.
    • Iohandler as an interface for Mina and applications, Mina notifies the application of the Iohandler implemented when a connection event, IO event, or exception event occurs.
    • The iosession is similar to the Socketchannel package, but the Mina further abstracts the connection so that more connection control and output of the flow information can be made.

In addition to Mina, JBoss Netty is now a widely-watched Java communication framework that is measured by the performance of JBoss Netty Mina.

Summary

The use of Java packages to implement message-based communication between systems is still more cumbersome. In order for developers to focus more on business processing of data without paying too much attention to pure technical details, the open source industry has created many excellent frameworks for inter-system communication based on the various protocols above, such as Mina.

Fundamentals and practices of distributed Java Applications reading notes one

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.