Netty-implement RPC framework

Netty implements RPC

Basic introduction to RPC

  1. RPC (Remote Procedure Call) remote procedure call means that there is a program on computer A and a program on computer B. Application A can call the method of application B. The calling method is as simple as calling its own local method.
  2. Commonly used RPC frameworks are: Ali's Dubbo, Google's gRPC, Spring's SpringCloud, etc.

RPC call flow

Insert picture description here

  1. The service consumer (Client) calls the service by local calling
  2. ClientStub is responsible for encapsulating methods, parameters, etc. into a message body that can be transmitted over the network after receiving the call
  3. ClientStub encodes the message and sends it to the server
  4. ServerStub receives the message and decodes it
  5. ServerStub calls the local service according to the decoding result
  6. The local service is executed and the result is returned to the ServerStub
  7. ServerStub will return the import result, encode it and send it to the consumer
  8. ClientStub receives the message and decodes it
  9. The service consumer (Client) gets the result

Realize dubbo RPC by yourself (based on Netty)

  1. The bottom layer of dubbo uses netty as the network communication framework and requires Netty to implement a simple EPC framework
  2. In template dubbo, consumers and providers agree on interfaces and protocols. Consumers call the provider's services remotely, the provider returns a string, and the consumer prints the data returned by the provider. The underlying network communication protocol uses Netty 4.x

Common interface

Need to define a common interface that both service providers and service consumers need

Insert picture description here

service provider

Implement the HelloService interface

Insert picture description here

Write netty core classes

Insert picture description here

Write Handler to handle business

Insert picture description here

Service consumer

Write netty core classes

Insert picture description here

Write Handler to handle business

Insert picture description here

test

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44642403/article/details/110312568
Recommended