A custom RPC framework implemented by Netty+Kyro+Zookeeper.(一款基于 Netty+Kyro+Zookeeper 实现的自定义 RPC 框架-附详细实现过程和相关教程。)
中文|English
Sorry, I did not fully translate the Chinese readme. I have translated the important parts. You can translate the rest by yourself through Google.
Although the principle of RPC is not difficult, I encountered many problems in the process of implementation. guide-rpc-framework implements only the most basic features of the RPC framework, and some of the optimizations are mentioned below for those interested.
With this simple wheel, you can learn the underlying principles and principles of RPC framework as well as various Java coding practices.
You can even use the guide-rpc-framework as a choice for your graduation/project experience, which is very great! Compared to other job seekers whose project experience is based on a variety of systems, building wheels is a sure way to win an interviewer’s favor.
If you’re going to use the guide-rpc-framework as your graduation/project experience, I want you to understand it rather than just copy and paste my ideas. You can fork my project and then optimize it. If you think the optimization is valuable, you can submit PR to me, and I will deal with it as soon as possible.
guide-rpc-framework is an RPC framework based on Netty+Kyro+Zookeeper. Detailed code comments, clear structure make it ideal for reading and learning.
guide-rpc-framework/
├── rpc-framework-simple/ # RPC framework core implementation
├── rpc-framework-common/ # Common utilities and constants
├── hello-service-api/ # Example service interface definition
├── example-server/ # Service provider example
├── example-client/ # Service consumer example
├── docs/ # Related documentation
└── images/ # Project image resources
Due to the limited energy and ability of me, if you think there is something to be improved and perfected, welcome to fork this project, then clone it to local, and submit PR to me after local modification, I will Review your code as soon as possible.
Let’s start with a basic RPC framework design idea!
note :The RPC framework we mentioned here refers to a framework that allows clients to directly call server-side methods as simple as calling local methods, similar to the Dubbo, Motan, and gRPC I introduced earlier. If you need to deal with the HTTP protocol, parse and encapsulate HTTP requests and responses. Type frameworks are not considered “RPC frameworks”, such as Feign.
A schematic diagram of the simplest RPC framework usage is shown in the figure below, which is also the current architecture of guide-rpc-framework:
The service provider Server registers the service with the registry, and the service consumer Client gets the service-related information through the registry, and then requests the service provider Server through the network.
As a leader in the field of RPC framework Dubbo, the architecture is shown in the figure below, which is roughly the same as what we drew above.
**Under normal circumstances, the RPC framework must not only provide service discovery functions, but also provide load balancing, fault tolerance and other functions. Such an RPC framework is truly qualified. **
Please let me simply talk about the idea of designing a most basic RPC framework:
Consumption side:
Provider side:
Start Zookeeper
Using Docker to download and run:
docker pull zookeeper:3.5.8
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.5.8
Clone and Build Project
git clone https://github.com/Snailclimb/guide-rpc-framework.git
cd guide-rpc-framework
mvn clean install
Import Project
Open IntelliJ IDEA: File -> Open -> guide-rpc-framework
First, define the service interface in the hello-service-api
module:
public interface HelloService {
String hello(Hello hello);
}
Implement Service Interface
@RpcService(group = "test1", version = "version1")
public class HelloServiceImpl implements HelloService {
private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
static {
System.out.println("HelloServiceImpl被创建");
}
@Override
public String hello(Hello hello) {
logger.info("HelloServiceImpl收到: {}.", hello.getMessage());
String result = "Hello description is " + hello.getDescription();
logger.info("HelloServiceImpl返回: {}.", result);
return result;
}
}
Start Service Provider
@RpcScan(basePackage = {"github.javaguide.serviceimpl"})
public class NettyServerMain {
public static void main(String[] args) {
// Register service via annotation
new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyRpcServer nettyRpcServer = new NettyRpcServer();
nettyRpcServer.start();
}
}
Inject Service Reference
@Component
public class HelloController {
@RpcReference(version = "version1", group = "test1")
private HelloService helloService;
public void test() throws InterruptedException {
String hello = this.helloService.hello(new Hello("111", "222"));
assert "Hello description is 222".equals(hello);
}
}
Start Service Consumer
@RpcScan(basePackage = {"github.javaguide"})
public class NettyClientMain {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(NettyClientMain.class);
HelloController helloController = (HelloController) applicationContext.getBean("helloController");
helloController.test();
}
}
Start Zookeeper
docker run -d --name zookeeper -p 2181:2181 zookeeper:3.5.8
Ensure Zookeeper is running on 127.0.0.1:2181
Start Service Provider
Run the NettyServerMain
class in the example-server
module
Start Service Consumer
Run the NettyClientMain
class in the example-client
module
@RpcService: Used to mark service implementation classes for automatic registration
group
: Service group, used for service groupingversion
: Service version, used for version control@RpcReference: Used to inject remote service references
group
: Service group, must match the providerversion
: Service version, must match the provider@RpcScan: Used to specify the package path for scanning RPC services
basePackage
: Base package path for scanning127.0.0.1:2181
Common Issues:
Failed to connect to Zookeeper
Service registration failed
@RpcService
annotation is correctly configuredService call timeout
Serialization exception
Serializable
interfaceDebugging Tips:
We welcome contributions to improve this RPC framework! Here’s how you can contribute:
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This is a learning-oriented RPC framework. For production use, consider mature solutions like Apache Dubbo, gRPC, or Spring Cloud.