In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to achieve Java synchronous blocking". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
One: distributed architecture network communication
In the distributed service framework, one of the most basic problems is how remote services communicate. There are many technologies that can realize remote communication in the field of Java, such as RMI, Hessian, SOAP, ESB, JMS and so on.
1.1: basic principles
In order to realize the communication between network machines, what needs to be done is to transfer the stream from one computer to another, based on the transmission protocol and network IO.
Among them, the well-known transport protocols are tcp, udp and so on. Tcp and udp are all extended for certain application scenarios based on the concept of Socket.
There are three main ways of network IO: BIO, NIO and AIO
1.2:RPC
The full name of RPC is remote procedure call, which means remote procedure call. With the help of RPC, remote services can be invoked like local calls, which is a way of communication between processes. Common RPC frameworks are: Hessian, gRPC, Thrift, HSF, Dubbo and so on.
Note: it is important to note that RPC is not a specific technology, but refers to the entire network remote call process; for the RPC framework, the core modules are communication and serialization.
There are two types of remote procedure calls, which are now the main ways to communicate between services:
It is a generalized remote call in the form of restful based on HTTP, represented by feign and restTemplate of spring could, the protocol adopted is layer 7 call protocol of HTTP, and the serialization of parameters and responses of the protocol is mainly in JSON format and XML format.
It is a narrow RPC remote call based on TCP, represented by Ali's Dubbo, mainly through netty to achieve layer 4 network protocol, NIO for asynchronous transmission, serialization can be JSON or hessian2 and java's own serialization, etc., can be configured.
1.2.1 RPC architecture
A complete RPC architecture contains four core components, namely Client,Client Stub,Server and Server Stub. This Stub can be understood as a stub.
Client (Client), caller of the service.
The client stub (Client Stub) stores the address message of the server, then packages the request parameters of the client into a network message, and then sends it to the server remotely through the network.
The Server, the real service provider.
The server stub (Server Stub) receives the message sent by the client, unpacks the message, and invokes the local method.
1.2.2 RPC calling procedure
Note: no matter what type of data it is, it eventually needs to be converted to a binary stream for transmission over the network, and the sender of the data needs to convert the object to a binary stream (serialization). The receiver of the data needs to restore the binary stream to the object (deserialization).
1.3:RMI
Java RMI refers to remote method invocation (Remote Method Invocation), which is supported natively by java. Using JRMP (Java Remote Messageing protocol) as the communication protocol, it can be considered as a pure java version of distributed remote calling solution. RMI is mainly used for communication between different virtual machines, which can be on different hosts or on the same host. So the communication here can be understood as an object on one virtual machine calling the method of an object on another virtual machine.
1.3.1 introduction to RMI
Registry:
Registry (Registry): registers the remote object as URL and replies to the client with a reference to the remote object.
Client:
Stub / Stub: the proxy of remote objects on the client
Remote reference layer (Remote Reference Layer): parses and executes the remote reference protocol
Transport layer (Transport): send calls, pass remote method parameters, and receive the results of remote method execution.
Server:
Skeleton: read the method parameters passed by the client, call the actual object method of the server, and receive the return value after the method execution
Remote reference layer (Remote Reference Layer): sends remote method calls to the skeleton after processing remote references
Transport layer (Transport): listens to the inbound connection of the client, receives and forwards calls to the remote reference layer.
1.3.2 RMI calling procedure
Remote invocation procedure:
1) the client queries and obtains the remote object reference from the registry of the remote server.
2) the pile object and the remote object have the same interface and method list, and when the client calls the remote object, it is actually done by the corresponding pile object agent.
3) after converting the local reference of the pile to the remote reference of the object on the server, the remote reference layer passes the call to the transport layer (Transport), which sends the call through the TCP protocol.
4) on the server side, the transport layer listens for inbound connections, and once it receives a remote call from the client, it forwards the reference to the remote reference layer above it.
5) the server-side remote reference layer converts the remote application sent by the client into a reference to the local virtual machine, and then passes the request to the skeleton (Skeleton)
6) the skeleton reads the parameters, then passes the request to the server, and finally the server makes the actual method call.
1.4:BIO 、 NIO 、 AIO
BIO: synchronous blocking; NIO: synchronous non-blocking; AIO: asynchronous non-blocking.
Synchronous and asynchronous (for application-kernel interaction):
Synchronization: when the user process triggers the IO operation, it uses wait or rotation to see if the IO operation is ready.
For example: the bank withdraws money, I go to withdraw the money myself, and wait in the process of withdrawing the money.
Async: when an asynchronous process call is made, the caller does not get the result immediately. Instead, after the call is made, the callee notifies the caller through status, notification, or handles the call through a callback function.
For example, I asked my friend to withdraw the money for me, and he returned it to me when he got it.
Blocking and non-blocking (different ways are taken according to the ready state of the IO operation when the process accesses data (read and write operations):
Blocking: reads and writes will be waiting in blocking mode.
Ex.: ATM queuing up to withdraw money, you can only wait in line all the time
Non-blocking: when using non-blocking IO, if the Java call cannot be read or written, it will return immediately. When the IO event dispatcher notifies that it can read and write, it will continue to read and write, and continue to cycle until the read and write is complete.
Ex.: withdraw money from the counter, take a number, and then sit on the chair and do other things. Wait for the broadcast notice. You can't go until your number arrives, but you can keep asking the lobby manager if he has lined up.
1.4.1 BIO (synchronous blocking)
BIO: synchronous blocking IO,B stands for blocking. The server implementation mode is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread to process it. If the connection does nothing, it will cause unnecessary thread overhead. Of course, it can be improved through the thread pool mechanism.
Simple implementation:
Server code
Public static void main (String [] args) throws IOException {ServerSocket serverSocket = new ServerSocket (); serverSocket.bind (new InetSocketAddress ("127.0.0.1", 8888)); while (true) {/ / synchronous blocking Socket socket = serverSocket.accept (); new Thread ()-> {try {byte [] bytes = new byte [1024] Int len = socket.getInputStream () .read (bytes); System.out.println (new String (bytes,0,len)); socket.getOutputStream () .write (bytes,0,len); socket.getOutputStream () .flush ();} catch (IOException e) {e.printStackTrace () }) .start ();}}
Client code
Public static void main (String [] args) throws IOException {Socket socket = new Socket ("127.0.0.1", 8888); socket.getOutputStream () .write ("hello" .getBytes ()); socket.getOutputStream () .flush (); System.out.println ("server send back data ="); byte [] bytes = new byte [1024]; int len = socket.getInputStream () .read (bytes) System.out.println (new String (bytes,0,len));} 1.4.2 NIO (synchronous non-blocking)
Synchronous non-blocking IO (non-blocking IO / new io). The server implementation mode is that a request corresponds to a channel (Channel), that is, all connection requests sent by the client are registered with the multiplexer, and the multiplexer starts a thread for processing only when the multiplexer polls that the connection has an IO request.
Channels: the channel for the Channel data connection. Data can be read from Channel to Buffer or written from Buffer to Channel.
Buffers: the channel channel can write data to the buffer Buffer or store data like buffer.
Selector (Selector): using a selector, with the help of a single thread, a large number of active Icano channels can be monitored and maintained in real time.
Characteristics of NIO
When a connection is created, there is no need for a thread, the connection will be registered with the multiplexer, all connections need only one thread to operate, and the multiplexer of that thread will poll and start a thread when a request is found in the connection.
The role of selector in the NIO model, after a connection comes, instead of creating a while endless loop to listen for data to read, register the connection directly to the selector, and then, by checking the selector, you can batch monitor connections with readable data, and then read the data.
The use of NIO
Example of using nio
1.4.3 AIO (Asynchronous non-blocking)
Asynchronous non-blocking IO. A stands for asynchronize
Features:
When there is a stream to read, the operating system passes the readable stream into the buffer of the read method and notifies the application
For write operations, the operating system actively notifies the application when OS finishes writing the stream of the write method. Therefore, both read and write are asynchronous, and the callback function is called when it is finished.
Usage scenarios: architectures with a large number of connections and long connections (reoperation), such as an album server. Focus on calling OS to participate in concurrent operations, Java7 began to support.
This is the end of "how to achieve Java synchronization blocking". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.