Distributed java application

 Large-scale applications are usually split into multiple subsystems for implementation.

      For Java , these subsystems may be deployed in multiple different JVMs on the same machine , or may be deployed on different machines, but these subsystems are not completely independent and must communicate with each other to jointly implement business functions.

      For such Java applications, we call them distributed Java applications.

 

 

       For distributed Java applications, there are usually 2 ways to achieve this:

       1. Communication between systems based on messages

            Communication between systems requires sending out messages, which can be byte streams, byte arrays, or even Java objects.

           Message-based inter-system communication is usually implemented based on network protocols . Commonly used protocols are: TCP/IP , UDP/IP .   

           TCP/IP is a reliable network data transmission protocol. TCP/IP requires both parties to establish a connection first, and then transmit data. TCP/IP is responsible for ensuring the reliability of data transmission.

           UDP/IP is a network data transfer protocol that does not guarantee the arrival of data. Since UDP cannot guarantee reliable data transmission, the performance will be better.

           TCP/IP and UDP/IP can be used to complete the data transfer , but to complete the inter-system communication, the data also needs to be processed . For example: reading and writing data, according to the POSIX standard, is divided into: synchronous IO , asynchronous IO . The most commonly used synchronous IO are BIO (Blocking IO) and NIO (Non-Blocking IO).

 

           BIO: From a program point of view, BIO is a blocking method when an IO read or write operation is initiated , and resources are released only when the program reads the stream or writes the stream to the operating system.

           NIO : From a program perspective, when an IO read or write operation is initiated , it is non-blocking; when a Socket has a stream readable or writable to the Socket , the operating system will notify the corresponding application for processing, and the application will then transfer the stream to the Socket. Read to buffer or write to OS.

           AIO : Asynchronous IO mode. From a program point of view, when performing read and write operations, it is only necessary to directly call the read or write methods of the API . Both methods are asynchronous. For read operations, when there is a stream to read, the operating system will pass the readable stream into the buffer of the read method and notify the application; for write operations, when the operating system passes the stream to the write method When writing is complete, the operating system actively informs the application.

           Compared with NIO , AIO simplifies the writing of programs, and the reading and writing of streams are replaced by the operating system.

 

        2. Realize communication between systems based on remote calling

            When communicating between systems, a remote Java implementation can be called transparently by calling a method of a local Java interface . The details are done by Java or the framework.

 

            Using Java packages to implement message-based inter-system communication is rather cumbersome.

     In order to allow developers to focus more on business processing of data instead of paying too much attention to pure technical details, the open source industry has created many excellent frameworks for inter-system communication based on the above protocols, Mina .

            Mina is Apache 's top-level project, built on Java NIO , and supports both TCP/IP and UDP/IP protocols. Mina shields the complexity of using Java NIO to the outside world, and makes a lot of optimizations in performance.

 

            The way of remote calling is to make the communication between systems the same as in the system as much as possible, so that users feel that calling a remote calling is the same as calling a local one.

 

           Realization of inter-system communication in remote calling mode based on Java 's own technology

There are two main technologies for implementing remote invocation            in Java : RMI and WebService :

           RMI : It is an important mechanism used by java to implement transparent remote invocation. In a remote call, the client only has the interface provided by the server.

 

Before            JDK 6.0 , the RMI implementation was based on TCP/IP + BIO . The RMI server listened to the externally provided interface on a port by starting the RMI registration object, and its implementation instance was bound to the RMI registration in the form of a string. on the object. The RMI client proxies the access to the server-side interface by proxy . The RMI client encapsulates the server-side object string, methods and parameters to be accessed into an object, serializes it into a stream, and transmits it to RMI through TCP/IP+BIO Service-Terminal. After the RMI server receives the client's request object, it parses the object string, methods and parameters in it, finds the instance that provides the business function from the RMI registration object through the object string, and then combines the method to be accessed to reflect the method instance. object, pass in the parameters to complete the call to the server-side object instance, and the returned result is serialized as a stream and returned to the client in TCP/IP+BIO mode. returned to the caller.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326265754&siteId=291194637