Skip to content

junlingsun/rpc

Repository files navigation

Simple Java RPC Framework

A lightweight RPC (Remote Procedure Call) framework built with:

  • Netty for network communication
  • Nacos for service registry and discovery
  • Java Dynamic Proxy for client-side invocation
  • Maven multi-module architecture

This project demonstrates how to build a minimal but complete RPC framework from scratch.


Project Structure

rpc-master
│
├── rpc-common          # Common request/response models & service map
├── rpc-client          # Client-side RPC implementation
├── rpc-server          # Server-side RPC implementation
├── rpc-registry        # Service registry abstraction (Nacos-based)
├── rpc-sample-api      # Example service API
├── rpc-sample-server   # Example server implementation
└── rpc-sample-client   # Example client usage

Architecture Overview

Client Proxy
     │
     ▼
RpcClient → NettyClient → Network → NettyServer → RequestHandler
     │                                           │
     ▼                                           ▼
 Service Registry (Nacos)                ServiceProvider

Core Flow

  1. Client invokes an interface method.
  2. Dynamic proxy builds an RpcRequest.
  3. Netty client sends the request to the server.
  4. Server receives the request and delegates it to RequestHandler.
  5. RequestHandler finds the implementation via ServiceProvider.
  6. Server executes the method using reflection.
  7. The result is returned as an RpcResponse.

Module Details

rpc-common

Contains shared models:

  • RpcRequest
  • RpcResponse
  • ServiceProvider

RpcRequest

Encapsulates:

  • Interface name
  • Method name
  • Parameter types
  • Parameters

RpcResponse

Encapsulates:

  • Result
  • Status
  • Error message (if any)

rpc-client

Handles client-side RPC invocation.

RpcClientProxy Example

UserService userService = rpcClientProxy.getProxy(UserService.class);

Responsibilities:

  • Intercepts method calls
  • Creates RpcRequest
  • Sends request via Netty

rpc-server

Handles server-side request processing.

Core responsibilities:

  1. Locate service implementation
  2. Use reflection to invoke method
  3. Wrap result into RpcResponse

rpc-registry

Service registration and discovery.

Defines:

  • register()
  • lookup()

Uses Nacos for service registry and a simple load balancing strategy.


How to Run

1. Start Nacos

startup.cmd   (Windows)
./startup.sh  (Linux/Mac)

Default address:

http://localhost:8848

2. Start RPC Server

Run:

rpc-sample-server/ServerTest

3. Start RPC Client

Run:

rpc-sample-client/ClientTest

Technology Stack

  • Java 8+
  • Netty
  • Nacos
  • Maven
  • Fastjson (for serialization)

Summary

This project demonstrates:

  • Client-side proxy abstraction
  • Netty-based communication
  • Registry-driven service discovery
  • Reflection-based invocation
  • Modular design

It provides a solid foundation for building a production-grade RPC system.

About

remote procedure call

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages