The story begins
It has been almost two years since I first got in touch with RPC. At that time, I just felt very impressive, and even after writing a Demo, I felt quite amazing. I knew nothing about the internal structure and principle, and I had no desire to explore.
Gradually, I got to know the terms of service registration, service discovery, registry and the execution process of something.
Later, the company’s distributed framework, which I felt was already familiar with, was transformed with Apache Tuscany. It was just an illusion at the time, because there wasn’t much of a problem, just a service problem, a little configuration problem. Just the CV’s slipping away.
CV is cool and fast, but the outsider feeling makes me uneasy all the time.
Start to buy relevant books to see, “really ยท can not go down”, the foundation, remember ah.
This way not I use the most original is the best effect, do Demo, first of these heard, called the name of the RPC framework are used again.
duang,duang,duang…. From Dubbo, SpringCloud to Thrift, GRPC. The layman’s feeling did not go away.
The story twists
I seem to have used all the tricks I can at this point, and it’s not working.
But I also know that there is a last resort to look at the source code, which tests the basic skills, including English reading ability, native library proficiency, these factors will affect the speed of reading understanding open source framework.
Into the pit, at that time I think the framework of the basic way should be about the same, so just look for someone else to analyze the source code of the blog first.
A soul, I found a source code interpretation of GRPC series of articles. But I was stuck in the first basic introduction of this six-part series of articles. I took notes, drew pictures, took notes, drew pictures… . I didn’t break the first article, but I found it easy to make a simple RPC framework. I learned from repeated notes and drawings what was needed and how to achieve what was needed.
Make a hole, take a wheel.
What does an RPC framework have
1. Dubbo is probably the RPC framework that appears most frequently in job postings
Seen from the figure
- 1.Provider (server)
- 2.Registry
- 3.Consumer
- 4. [‘ t? : n]
2. A more concise GRPC from the diagram
- 1. GRPC Server (Server)
- 2. GRPC Stub (client)
The great benefit of these maps is that they are very powerful generalizations of what the core components are.
However, just knowing what is far from enough, no matter how much theoretical knowledge you have and how much you have read, it is not as difficult to write for the first time.
Another is to learn cooking, for example, you can see the whole process from cutting vegetables to ignition, and then follow the process to practice. As for our daily business projects, how to build the framework and how to write business, we can see the practice of predecessors. It’s easy to do it yourself.
I used the code statistics software to look at dubbo about 10 lines, GRPC Java version more than 330,000 lines. / black question mark /๐ถ/๐๐ป
I had to find an RPC framework with a relatively easy structure to understand and less code, and specifically understand the general structure of RPC framework and how to implement it. Fortunately, I found baidu’s BRPC without much effort, the code is not large, and the structure is very clear for me.
Everything is ready, this is not difficult (I was young thought ๐ถ)
The story
Since Netty’s powerful client-server implementation is very convenient for sending and receiving data, I also built a garbage wheel using these elegant high-performance frameworks.
FRPC call procedure
Less than 700 lines of core code, using a third-party framework
- Netty Server The client sends and receives messages
- Lombok reduces code
- Curator ZooKeeper client
- Fastjson serialization
- Cglib dynamic proxy
- Slf4j logging
- Commons-pool2 Client Channel connection pool
github frost
Technical summary
Implementation functions: simple (Choulou) client, server, client connection pool (netty started with its own, with bad change), service registration, service discovery.
Difficulty: At the beginning, I did not consider that there would be a delay in the occurrence of a null pointer in the process of receiving a request, leading to a direct null pointer test. However, I immediately thought of the reason. First, I verified that EVERY time the client received a response, I always Sleep1 second. Validation is complete. Null pointer is gone. Then began a long search, by studying AbstractQueuedSynchronizer, already, understands CountDownLatch asynchronously concrete implementation to solve the problem.
Plan:
- 1. The simplest way is to compress the data first.
- 2. Transformation service registration service discovery, (now I can only register a service, discovery is to get this one ๐ถ) the goal is to imitate Dubbo or BRPC.
- 3. Modify the connection pool. (No direction yet)
Still, it is desirable to have as little code as possible and as clear a project structure as possible when it is fully functional.