The first part of Java distributed communication

1. Communication between systems based on messages

A. Data transmission: TCP/IP, UDP/IP Differences between TCP (port number 23) and UDP (port number 53)

  • TCP connection-oriented (for example, dial up to establish a connection before making a phone call); UDP is connectionless, that is, no connection is required before sending data
  • TCP provides reliable services. That is to say, data transmitted through the TCP connection is error-free, not lost, not repeated, and in order to arrive; UDP does its best to deliver, i.e. reliable delivery is not guaranteed
  • TCP byte stream oriented, in fact, TCP treats data as a series of unstructured byte streams; UDP is packet-oriented UDP without congestion control. Therefore, network congestion does not reduce the sending rate of the source host (useful for real-time applications, such as IP phone calls and real-time video conferences).
  • Each TCP connection can be point-to-point only. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication
  • TCP header cost 20 bytes; The header of UDP has a small overhead of only 8 bytes
  • TCP logical communication channel is full-duplex reliable channel, UDP is unreliable channel

Conclusion:

  • TCP’s strength lies in its security
  • UDP’s strength lies in its performance

B. Data processing part: BIO, NIO, AIO Synchronous blocking BIO: Connected thread (cache, own processing) Synchronous non-blocking NIO: request thread (immediate, own processing) Asynchronous non-blocking AIO: valid request thread (immediate, delegate OS)

Synchronous vs asynchronous Synchronous: JAVA handles I/O reads and writes by itself Asynchronous: Delegates I/O reads and writes to the OS

Blocking (not immediately returned) vs non-blocking (immediately returned) Blocking: Reading and writing resources using the buffer and then notifying the application is relatively slow. Non-blocking: Threads may wait for back-end resources before processing, which is fast

Conclusion:

  • Both NIO and AIO are event-driven and require event registration and scanning.
  • BIO is implemented mainly through sockets.
  • The mysql database uses BIO, while Netty uses NIO.

2. Realize inter-system communication based on remote call

  1. RPC: C/S, cross-language cross-platform
  2. Webservice: request response mechanism, cross-platform
  3. RMI: reduced client-server coupling, Java remote interface call; Across the virtual machine
  4. JMS: JAVA messaging service, point-to-point, and publish-subscribe models such as ActiveMQ