Design and Implementation of Secure and Elegant RPC Calls
Li Wei
Title: Design and Implementation of Safe and Elegant RPC Calls
1. Overview
This article explains an RPC safe‑call utility class designed with functional programming concepts. By combining the RpcSupplier functional interface with the RpcCaller utility class, it offers a unified, safe, and maintainable solution for RPC invocations.
2. Design Goals
- Unified exception handling: eliminates repetitive try‑catch code
- Degradation protection: provides a default‑value mechanism to ensure system stability
- Log standardization: adopts a consistent log format
- Code reuse: reduces duplication and boosts development efficiency
- Ease of extension: supports future enhancements (monitoring, circuit breaking, etc.)
3. Core Class Design
3.1 RpcSupplier – Functional Interface
Design highlights:
- Uses the
@FunctionalInterfaceannotation, enabling lambda expressions - Generic design, supporting various return types
- Allows throwing
Exceptionto give upper layers flexible error handling
3.2 RpcCaller – Safe‑Call Utility Class
Core functions:
- Unified exception‑handling mechanism
- Null‑value protection logic
- Default‑value return mechanism
- Standardized logging
4. Application of Design Patterns
4.1 Strategy Pattern
The concrete RPC invocation logic is encapsulated in RpcSupplier, making the call strategy pluggable.
4.2 Template Method Pattern Idea
RpcCaller provides a fixed invocation template:
- Execute the RPC call
- Catch and handle exceptions
- Perform null checks
- Log the operation
- Return the result or a default value
5. Comparison of Usage Approaches
5.1 Traditional Direct Call
(code omitted for brevity)
5.2 Using RpcCaller
(code omitted for brevity)
6. Advantage Analysis
6.1 Code Quality Improvement
| Metric | Traditional | RpcCaller | Improvement |
|---|---|---|---|
| Code duplication rate | High | Low | Significantly reduced |
| Exception handling consistency | Inconsistent | Unified | Fully consistent |
| Maintenance cost | High | Low | Greatly reduced |
| Readability | Average | Excellent | Noticeably better |
6.2 System Stability
- Degradation protection: automatically returns a default value when an RPC call fails, preventing system crashes
- Null safety: built‑in null checks avoid NPEs (NullPointerExceptions)
- Exception isolation: unified handling prevents exception propagation
6.3 Development Efficiency
- Reduced boilerplate: no need to repeatedly write exception‑handling code
- Standardized development: a uniform call style lowers the learning curve
- Rapid development: focus on business logic while the utility handles technical details
7. Conclusion
The design of RpcSupplier and RpcCaller embodies the principle “encapsulate what changes, favor composition over inheritance.” By cleverly combining functional programming with utility classes, the approach keeps code concise while offering strong extensibility.
This pattern is especially suited for managing RPC calls in microservice architectures, markedly improving code quality, system stability, and development efficiency.
Originally written by Li Wei (李唯_) and published in Chinese on 后端技术栈全书 (Full-Stack Backend Engineering). Translated and adapted for DriftSeas with permission.