preface

Welcome to our GitHub repository Star: github.com/bin39232820… The best time to plant a tree was ten years ago, followed by now

six-finger-web

The wheels of a Web backend framework roll themselves (easy) from handling Http requests [Netty request-level Web servers] to MVC [interface encapsulating and forwarding], to IOC [dependency injection], to AOP [aspect], to RPC [remote procedure calls] and finally to ORM [database operations].

github

Github.com/bin39232820…

Why the wheel

Actually is such, small six six oneself peacetime? Sometimes like to look at the source code such as Spring, but the level may not how, every time dazed at all, and then feel the inside of the details too difficult, then I can only view the overall thought, then I think if I can according to each elder some thought, rolled out a simple wheel, Is it easier for me to understand the author’s ideas? So six-Finger-Web came out, and it was really a learning process for me, and THEN I opened it up to help those of you who are having trouble learning the source code. In addition, we can exercise our coding ability, because we usually use Java API for CRUD. As time goes by, we are not familiar with many framework API classes, so we take this opportunity to exercise.

The characteristics of

  • Built-in HTTP server written by Netty, without additional dependence on Web services such as Tomcat.
  • Code is simple to understand (small 66 can not write their own framework big guy that kind of high clustering, low coupling code), ability a little bit stronger to see the code can understand, weakness also does not matter, small 66 has a supporting from 0 build tutorial.
  • Support MVC-related annotations to ensure that they are used similarly to SpringMVC
  • Support for Spring IOC and Aop related features
  • Support similar to Mybatis related functions
  • Supports RPC-related functionality similar to Dubbo
  • For data returns, only the Json format is supported

omg

The front is already written chapters, I will give you one by one to go through the construction process

  • Suitable for beginners and intermediate Java programmer training manual to build the entire Web project from 0 (A)
  • Build the whole Web project from 0 (2)
  • Build the whole Web project from 0 (3)
  • Build the whole Web project from 0 (4)
  • Build the whole Web project from 0 (5)
  • [Suitable for junior and intermediate Java programmer training manual to build the whole Web project from 0]
  • [Suitable for junior and intermediate Java programmer training manual to build the whole Web project from 0]

Imperceptibly also wrote the eighth, almost also have half! Basically these things are learned, the going to learn something big data technology stack (actually wanted to pre-school side, but it never having written a front-end work starts from feeling ability is still in the primary stage, unless you really want me to go to the front, or feeling is expected to lower efficiency, so even if the right, back to work)

Today we will really look at the implementation of RPC, the last article SPI is the basis, we must have a good understanding of it. Today’s RPC is easy with this. Java Guide RPC gitee.com/SnailClimb/… It’s basically its code, and I’ll just go through it.

Today is the first release that simply implements remote procedure calls without integrating Spring

Take a look at the package structure

If we look at the figure above, the main thing is RPC-Core because it’s the base version now so there are just a few core components

  • Config This is just some configuration
  • Loadbalance Operation of load balancing
  • Proxy is a proxy object generated when the client invokes the server interface
  • The zK services used on the registry side are registered in the discovery center
  • Remoting real remote call code, this base version is based on Socket, the next version is based on Netty

Specific code explanation

Because the code is more, I will talk about the screenshot part, the specific words of the article is an auxiliary, if you really want to understand, it is not difficult to pull down the code to see.

rpc-api

As anyone who’s written dubbo knows, we’re going to use a release API because both our clients and our servers are going to rely on it.

Also very simple, is to define an interface, we all understand

rpc-server

And then our service provider, think about it, what’s the service provider going to do? After we figure out what we need to do, it’s easy.

  • The first one must start a network communication, so since it is the Server, then do we need to start a Server this is for sure
  • Second, we are actually the server, so do we have to register our address with the registry? That’s right. We need to register ourselves

Okay, let’s look at the code for the RPC-Server startup class

And then I’m going to follow you through the code for these two methods

The registration service

ServiceProviderImpl->publishService

ServiceProviderImpl->addService

This is used to find services that are specifically implemented

ZkServiceRegistry-> registerService

Operating zk using CuratorFramework on this side of the small 66 point is that our zk server and our code client version should be the same, otherwise there will be a variety of errors, small 66 own for a long time. Ha ha.

Look at the process of starting the server side, this has to understand some network, in fact, we do business when you do not use the network, but as long as you write some middleware basically have to understand the network, so the network code is also very important.

SocketRpcServer->start

This is the general operation, set up a thread pool, and then handle the incoming requests, let’s see how it works

SocketRpcRequestHandlerRunnable->run

The above code is also very simple

The first step is to get the flow. What happens after you get the flow? Turn it into a request object, process it, return the result to the client, and continue with the request process

RpcRequestHandler->handle

2 lines of code is also very simple, that is, we have request parameters, through the request parameters we’ll find our server specific interface implementation class, and then by class and parameters, through the method name and argument types, to obtain a specific method to execution method through reflection, so the framework, is really much reflection.

To this? That’s it for the server side. Let’s look at the client side

rpc-client

The client, let’s think about what the client does.

  • The first step must be to start the network client
  • Then, with the request parameters, a proxy object is generated to invoke the methods of the remote service

Let’s see SocketRpcClient

The constructor first generates our service discovery through SPI, which is what the client needs to do

RpcClientProxy

This class generates a proxy object. How does that work

The first step, of course, is the JDK’s dynamic proxy. Then let’s take a look at its invoke method in the very core

The first is to build the request object, then go to the specific call server method, finally get the return value, and then verify the return value.

SocketRpcClient-> sendRpcRequest

We need to go to ZK to get our IP + port and then make network request. That’s the whole client process, and that’s about it

test

Let’s test the test

  • api

  • server

Then we start the server first

Then start the client

At the end

Ok, because the code is too much, so I here is a screenshot, or I hope you really want to know their own code to learn, you pull the code to follow, basically the whole process of RPC you can do know.

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest power of my creation, our next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!