Distributed Architecture Network Communication

In distributed system design, network communication is the most fundamental and important component. In the Java ecosystem, there are various technologies for implementing remote service communication, each with its specific application scenarios and implementation principles:

  1. RMI (Remote Method Invocation): Java’s native remote invocation framework
  2. Hessian: HTTP-based lightweight RPC framework
  3. SOAP: XML-based Web service protocol
  4. ESB (Enterprise Service Bus): Enterprise service bus
  5. JMS (Java Message Service): Message-oriented middleware API

Basic Principles

Network Communication Basics

From the bottom layer of computer systems, the essence of network communication is transferring data from one computer to another. This process mainly involves two key components:

  1. Transport Protocol:

    • TCP: Connection-oriented reliable transport protocol, providing data sequence guarantee and retransmission mechanism
      • Three-way handshake to establish connection
      • Sliding window for flow control
      • Four-way handshake to terminate connection
    • UDP: Connectionless unreliable transport protocol, providing lower latency
      • Suitable for scenarios with high real-time requirements but allowing少量丢包
      • Commonly used in video conferencing, online gaming, etc.
  2. Network I/O Models:

    • BIO (Blocking I/O): Synchronous blocking model, each connection requires an independent thread to handle; suitable for scenarios with few connections
    • NIO (Non-blocking I/O): Synchronous non-blocking model, based on Selector for multiplexing; suitable for high-concurrency scenarios
    • AIO (Asynchronous I/O): Asynchronous non-blocking model, based on callback mechanism; highest performance but complex implementation

Higher-Level Abstractions

In practical applications, developers rarely directly manipulate underlying protocols but use higher-level abstractions:

  • HTTP/HTTPS protocols
  • WebSocket protocol
  • Various RPC frameworks

What is RPC

RPC (Remote Procedure Call) is a communication paradigm for distributed systems. Its core goal is to make remote service calls as simple as local calls.

Core RPC Characteristics

  1. Transparency: Calling a remote method is like calling a local method
  2. Cross-language: Many RPC frameworks support multi-language interoperability
  3. High performance: Reduces communication overhead through protocol optimization
  4. Scalability: Supports distributed features such as service registration and discovery

RPC Architecture

A complete RPC architecture includes the following components and their detailed workflows:

1. Client

  • Service consumer, initiates remote calls
  • Typical behavior: Obtain service proxy, construct call request, wait for response

2. Client Stub

  • Proxy object, core responsibilities include:
    • Service discovery: Get service address from registry
    • Protocol encoding: Convert method calls into network messages
    • Network transmission: Send requests through communication framework
    • Result decoding: Deserialize response data into objects

3. Server

  • Service provider, contains actual business implementation
  • Typical behavior: Register service to registry, listen and process requests, execute local methods

4. Server Stub

  • Request processor, core responsibilities include:
    • Network listening: Receive client requests
    • Protocol decoding: Parse request data
    • Method invocation: Reflect and invoke local implementation
    • Result encoding: Serialize return results

Note: Regardless of the type of data, it ultimately needs to be converted into binary streams for network transmission. The sender needs to convert objects into binary streams, and the receiver needs to restore binary streams into objects.

Common RPC frameworks: Hessian, gRPC, Thrift, HSF, Dubbo, etc.


RMI (Remote Method Invocation)

Basic Introduction

Java RMI (Remote Method Invocation) is a distributed remote call framework natively supported by Java. It uses JRMP (Java Remote Messaging Protocol) as its communication protocol and is a pure Java version of distributed remote call solution. RMI is mainly used for communication between different Java Virtual Machines (JVMs). These JVMs can run on different hosts or on the same host. Through RMI, an object in one JVM can call methods of an object in another JVM as simply as calling local object methods.

Working Principles

Client Components

  1. Stub

    • The remote object’s proxy on the client side, responsible for hiding network communication details
    • Implements the same interface as the remote object
    • When the client calls a method, the Stub serializes the call request and sends it over the network to the server
  2. Remote Reference Layer

    • Parses and executes remote reference protocols
    • Handles remote object reference semantics
    • Manages the lifecycle of remote objects
  3. Transport Layer

    • Responsible for establishing and managing network connections
    • Sends method call requests
    • Passes remote method parameters
    • Receives and returns remote method execution results
    • Uses TCP/IP protocol for communication

Server Components

  1. Skeleton

    • Receives requests sent by clients through Stub
    • Deserializes method parameters
    • Calls the actual object method on the server side
    • Serializes execution results and returns them to the client
    • In newer versions of RMI, Skeleton has been replaced by dynamic proxy mechanism
  2. Remote Reference Layer

    • Handles remote reference semantics
    • Forwards requests to appropriate Skeleton or target object
    • Manages registration and lookup of remote objects
  3. Transport Layer

    • Listens for inbound client connections (default port 1099)
    • Receives client requests
    • Forwards requests to remote reference layer
    • Manages connection pools and thread pools

Advantages and Disadvantages

Advantages:

  • Simple and easy to use, similar syntax to local method calls
  • Completely implemented in Java, good cross-platform support
  • Supports object serialization and dynamic class loading
  • Built-in security mechanism

Disadvantages:

  • Only supports communication between Java languages
  • Performance not as efficient as some binary protocols
  • Poor firewall penetration capability
  • May encounter connection problems in complex network environments