Github address: github.com/Snailclimb/… (Welcome star, welcome to improve! ‘!)

preface

About 2 months ago, I said that I would use my spare time to write a simple RPC framework, today (2020-06-05) finally open source it, hope to help friends.

Although the principle of RPC is not difficult, there are many problems in the process of implementation. Guide-rpc-framework currently only realizes the most basic functions of RPC framework, some optimization points are mentioned below, interested friends can improve themselves.

introduce

Guide-rpc-framework is an RPC framework based on Netty+Kyro+Zookeeper implementation. The code comments are detailed, the structure is clear, and the integration of the Check Style specification code structure is very easy to read and learn.

Due to the limited energy and ability of Guide brother, if you feel that there is any improvement or improvement, you are welcome to clone this project to your own local, and submit the PR to me after local modification, I will Review your code as soon as possible.

Let’s start with a basic RPC framework design ideas!

A basic RPC framework design ideas

A typical RPC application scenario is as follows. In general, AN RPC framework should not only provide service discovery functions, but also provide load balancing and fault tolerance functions, so that an RPC framework is truly qualified.

A complete RPC framework use diagram

Here are a few ideas for designing a basic RPC framework:

  1. Registries: Registries are a must, and Zookeeper is recommended. A registry is used to store relevant information such as the address of a remote method.
  2. Network transmission: since you want to call a remote method to send a request, the request should at least include you call the class name, method name and related parameters! The NIO-based Netty framework is recommended.
  3. Serialization: Since serialization is involved when it comes to network transport, you can’t just use the JDK’s built-in serialization. The JDK’s native serialization is inefficient and has security holes. So, you also need to consider which serialization protocol to use. The most common ones are Hession2, Kyro, and Protostuff.
  4. Dynamic proxy: In addition, dynamic proxy is also required. Since the main purpose of RPC is to make calling remote methods as easy as calling local methods, using dynamic proxies shields remote interface calls from details such as network transport.
  5. Load balancing: Load balancing is also required. Why? For example, we have a service on our system that gets a lot of traffic. We deploy this service on multiple servers. When a client initiates a request, multiple servers can handle the request. Then, choosing the right server to handle the request is critical. If you need a single server to handle requests for the service, the point of deploying the service on multiple servers ceases. Load balancing is to avoid a single server to respond to the same request, easy to cause server downtime, crash and other problems, we can obviously feel its significance from the four words of load balancing.
  6. .

Basic information and optimization points of the project

In order to step by step, AT first, I used the traditional BIO-based Socket for network transmission, and then used the SERIalization mechanism of JDK and memory to directly store relevant service information to implement the RPC framework.

Later, I optimized the original version, and the optimization points that have been completed and can be completed are listed below 👇.

Why list the optimizable points? I want to give some ideas to those who want to optimize the RPC framework. You are welcome to Clone the warehouse and optimize it yourself.


Project module overview


Run the project

1. Import the project

Cloning projects to their own local: git clone [email protected]: Snailclimb/guide xml-rpc – framework. Git

Then open with IDEA and wait for the project initialization to complete.

Initialize Git hooks

The main purpose of this step is to run the Check Style before committing the code, so that the code is formatted properly and cannot be committed if there is a problem.

For Mac/Linux, the Windows user needs to manually copy the pre-commit file from the config/git-hooks directory to the.git/hooks/ directory of the project.

Execute the following commands:

➜ guide-rpc-framework git:(master) qualify chmod +x./init.sh➜ guide-rpc-framework git:(master) qualify./init.shCopy the code

A brief introduction to how it is done!

This script copies the Git commit hook to the.git/hooks/ directory of your project, so that it will be executed every time you commit.

cp config/git-hooks/pre-commit .git/hooks/
chmod +x .git/hooks/pre-commit
Copy the code

Complaint: I’ve been using Gradle for a long time, but I haven’t used Maven for a long time. Gradle is better than Maven in many ways. Gradle projects rely on build. Gradle is cleaner and more concise than Maven’s pom. XML (Maven is a pot for XML). Gradel can also use the Groovy language……

The content of the pre-commit is as follows. It is used to run the Check Style command to Check the code format before submitting the code.

#! /bin/sh
#set -x

echo "begin to execute hook"
mvn checkstyle:check
 RESULT=$?  exit $RESULT Copy the code

3. Download and configure the CheckStyle plug-in

IntelliJ IDEA-> Preferences->Plugins-> Search Download the CheckStyle plugin and configure as follows.


Once configured, use the plug-in as follows!


4. Download and run ZooKeeper

Docker is used here to download and install.

Download:

Docker pull zookeeper: 3.4.14Copy the code

Run:

Docker run -d --name zooKeeper -p 2181:2181 ZooKeeper :3.4.14Copy the code

use

Service provider

Implementation interface:

public class HelloServiceImpl implements HelloService {
   @Override
    public String hello(Hello hello) {
.    }
} Copy the code

Publish service (transport using Netty) :

HelloService helloService = new HelloServiceImpl();
NettyServer nettyServer = new NettyServer("127.0.0.1".9999);
nettyServer.publishService(helloService, HelloService.class);
Copy the code

Service consumer

ClientTransport rpcClient = new NettyClientTransport();
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcClient);
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
String hello = helloService.hello(new Hello("111"."222"));
Copy the code

Issues related to

Why was this wheel built? Doesn’t Dubbo smell good?

The RPC framework was written primarily to learn by building wheels and to test the application of the knowledge you have learned.

Implementing a simple RPC framework is actually relatively easy, but a bit harder than writing AOP and IoC, if you understand the fundamentals of RPC.

I previously shared how to implement an RPC from a theoretical level on my knowledge planet. But the theory is just support, you understand the theory may only fool the interviewer. Hands-on skills are still the most important thing in a programmer’s field, even if you’re an architect. When you put your hands to work on something, when you put theory into practice, you will find a lot of holes waiting for you.

In actual projects, we should try to build fewer wheels and use them as soon as possible after we have excellent frames. Dubbo has done a good job in all aspects.

What I need to know in advance if I want to write it myself

Java:

  1. Dynamic proxy mechanism;
  2. Serialization mechanisms and comparisons of various serialization frameworks such as Hession2, Kyro, Protostuff.
  3. Use of thread pools;
  4. CompletableFutureThe use of
  5. .

Netty:

  1. Use Netty for network transmission;
  2. ByteBufintroduce
  3. Netty Sticking and unpacking packets
  4. Netty Long connection and heartbeat mechanism

Zookeeper :

  1. Basic concept;
  2. Data structure;
  3. How to use Open source ZooKeeper client framework of Netflix to add, delete, modify and check;

Recommended reading

  1. Close to 8000 words of Spring/SpringBoot common annotations summary! Arrangement!
  2. The interviewer asked me about the important new features of Java8~14 and I cried
  3. The first bomb! Arrangement! Amway 10 essential add-ons for your IDEA!
  4. End scatter flower! JavaGuide Interview shock edition is coming!

Github 80K Star project JavaGuide is the author of the project. Every week will update some of their own original dry goods in the public account. Public hao background reply “1” to receive Java engineers essential learning materials + interview surprise PDF.


This article is formatted using MDNICE