Manual RPC wheel series of articles

  • “Building RPC Wheels from Scratch” 01 Why would I want to build a wheel?
  • “Build RPC Wheel from scratch series” 02 demo wheel, is the donkey is the horse out for a walk
  • (TODO) “Build RPC Wheel from Scratch series” 03 Complete have, only need an environment to build
  • (TODO) “Building RPC Wheels from Scratch series” 04 hand-drawn 4 pictures to introduce you to RPC services framework
  • (TODO) “Building RPC Wheels from Scratch series” 05 Introduces the architecture principle of RPC framework in popular language

Hello everyone, I am Lei Xiaoshuai, recently I wrote a mini version of DUbbo RPC frame by hand, please check the manual!

Dangdang, these days promised to everyone RPC framework finally online, sa Hua Sa Hua Sa hua ~

Originally intended to speak about the principle and source code, but may be some boring, we always like to see the effect directly, that this time I put the order in reverse, directly demonstrate the operation effect.

Seeing the results might stimulate your interest in learning? !

Since you’re starting from scratch, I’m sure you’ll be able to teach yourself and give your interviewer a chance to brag *

Easy RPC framework introduction

I gave this project a name: Easy-RPC, which on the surface means very simple RPC, easy to learn, but in reality is a very rudimentary framework.

The main purpose of this framework is to lead you to a simple RPC framework from scratch, let you feel the joy of building wheels, in the process of happiness you can also learn a lot of things, isn’t it cool? !

I covered it in the last article, but I want to repeat it here. If you study this project carefully, you can master the following techniques:

  1. The bottom network layer is based on NetTY. There is no problem after learning NetTY.
  2. Using custom annotations, you can understand the basic operation mechanism of annotations after learning;
  3. Service registration based on ZooKeeper, learn zK entry no problem;
  4. It uses reflection;
  5. Dynamic proxy technology will be used;
  6. How to define a xxX-spring-boot-starter, understand spring Boot automatic configuration mechanism;
  7. Learn how to customize configuration items and bind them to beans;
  8. Learn to listen for events in the Spring container;
  9. … , etc.

Have a little heart? !

Project source code

The framework project source code I have all been hosted to Github, you can download at will, all free for you to whoring, ha ha ha, convenient words Star is grateful ~

The source code address of the project is unified in the public account background management. Wechat search official number: laughter-loving architect, reply keyword: RPC

Of course, if you are interested in writing RPC from scratch, or you have a problem, or you want to join the group and communicate with other friends, you are welcome to contact me and reply the code as above.

Well, below based on the source code to demonstrate the effect, continue to see ~

Quick start

Environment to prepare

  • JDK8 or above
  • Maven 3
  • Zookeeper Single-node or cluster instance
  • IntelliJ IDEA

Compiling and installing the source code

Knock on the board: The demo code covered in the following guidance documents is stored in the easy-Rpc-example directory.

Download the source code

git clone [email protected]:CoderLeixiaoshuai/easy-rpc.git
Copy the code

Compile and install jar packages into the local repository

mvn clean install
Copy the code

Create the Spring Boot Maven project

Create two new projects locally to simulate the client and server.

Introduction of depend on

<dependency>
    <groupId>com.leixiaoshuai</groupId>
    <artifactId>easy-rpc-spring-boot-starter</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
</dependency>
Copy the code

Server Configuration

Expose interfaces

Define a service interface

/**
 * Hello World
 *
 * @authorLei Xiaoshuai (official account search: laughing architect) *@since2021/11/29 * /
public interface HelloService {
    /** * say hello **@paramName the name *@returnGreetings * /
    String sayHello(String name);
}
Copy the code

Implement the interface, exposing a service interface with a custom annotation @ServiceExpose

/**
 * Hello World
 *
 * @authorLei Xiaoshuai (official account: laughing architect) *@since2021/11/29 * /
@ServiceExpose
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Greetings from Xiao Shuai Lei" : Hello" + name + "Congratulations on building RPC wheels!"; }}Copy the code

Configure the registry address

The current project only supports ZooKeeper as the registry. Providers use ZooKeeper to expose service interfaces.

# application.properties
Port exposed by RPC service
leixiaoshuai.easy.rpc.expose-port=6666
# zookeeper registration center address
leixiaoshuai.easy.rpc.zk-address=127.0.0.1:2181
Copy the code

Client Configuration

Inject remote services

Automatically inject the interface service exposed by the server using the custom @Servicereference annotation

/ * * *@authorLei Xiaoshuai (official account: laughing architect) *@since2021/12/1 * /
@RestController
public class HelloController {
    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @ServiceReference
    private HelloService helloService;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name) {
        final String rsp = helloService.sayHello(name);
        logger.info("Receive message from rpc server, msg: {}", rsp);
        returnrsp; }}Copy the code

Configure the registry address

Zookeeper is configured on the client to subscribe to the discovery of service interfaces exposed by the server

# application.properties
# ZooKeeper instance address
leixiaoshuai.easy.rpc.zk-address=127.0.0.1:2181
Copy the code

Start the test

Running the server (service provider)

/** * Service provider startup entry **@authorLei Xiaoshuai (official account: laughing architect) *@since2021/11/29 * /
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}Copy the code

If the following log is displayed, the startup is successful

Run the client (service consumer)

/** * Service consumer launch entry **@authorLei Xiaoshuai (official account: laughing architect) *@since2021/12/01 * /
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Copy the code

If the following log is displayed, the startup is successful

test

Using a browser to enter the request address test:

http://127.0.0.1:8081/hello/ input test stringCopy the code

Return the following string to indicate success

FAQ

1. The ZooKeeper connection fails

Solutions:

(1) Install and run the ZooKeeper instance on the local machine or server.

(2) Configure the ZooKeeper address correctly in the configuration file.

Next step

Behind will be divided into a number of articles on the framework project source code, speak about the design idea, this simple RPC learned, dubbo framework you also have a way to take.

If you’re interested in writing RPC projects from scratch, go to Github and download the source code to try them out: github.com/CoderLeixia…