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:

  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
    • UDP: Connectionless unreliable transport protocol, providing lower latency
  2. Network I/O Models:

    • BIO (Blocking I/O): Synchronous blocking model
    • NIO (Non-blocking I/O): Synchronous non-blocking model
    • AIO (Asynchronous I/O): Asynchronous non-blocking model

Development Process

Server Development Steps

  1. Define Remote Interface: Create an interface extending java.rmi.Remote
  2. Implement Remote Interface: Create an implementation class extending java.rmi.server.UnicastRemoteObject
  3. Start RMI Server: Create registry and bind remote object

Client Development Steps

  1. Lookup Remote Object: Use Naming.lookup() method to get the remote object’s Stub
  2. Call Remote Method: Call method with same syntax as local method call

Code Implementation

Create Remote Interface

package icu.wzk.service;

import icu.wzk.model.WzkUser;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface WzkService extends Remote {
    String sayHello(WzkUser wzkUser) throws RemoteException;
}

Create Reference Object

package icu.wzk.model;

public class WzkUser implements Serializable {
    private String name;
    private int age;
    // Omitted get and set methods
}

Implement Remote Service Object

package icu.wzk.service.impl;

import icu.wzk.model.WzkUser;
import icu.wzk.service.WzkService;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class WzkServiceImpl extends UnicastRemoteObject implements WzkService {
    private static final long serialVersionUID = 1L;

    public WzkServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public String sayHello(WzkUser wzkUser) throws RemoteException {
        System.out.println("WzkService: " + wzkUser.getName() + ", " + wzkUser.getAge());
        return "success";
    }
}

Server Program

package icu.wzk;

import icu.wzk.service.WzkService;
import icu.wzk.service.impl.WzkServiceImpl;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class WzkServer {
    public static void main(String[] args) throws Exception {
        WzkService wzkService = new WzkServiceImpl();
        LocateRegistry.createRegistry(8888);
        Naming.bind("//127.0.0.1:8888/wzk", wzkService);
    }
}

Client Program

package icu.wzk;

import icu.wzk.model.WzkUser;
import icu.wzk.service.WzkService;
import java.rmi.Naming;

public class WzkClient {
    public static void main(String[] args) throws Exception {
        WzkService wzkService = (WzkService) Naming.lookup("//127.0.0.1:8888/wzk");
        WzkUser wzkUser = new WzkUser();
        wzkUser.setAge(18);
        wzkUser.setName("wzkicu");
        System.out.println(wzkService.sayHello(wzkUser));
    }
}

Test Results

After running WzkServer and WzkClient programs:

  • Client returns: success
  • Server outputs: WzkService: wzkicu, 18