Micro Services Architecture road (b) of the RPC climbing

1. RPC Introduction

⚫ Remote Procedure Call (Remote Procedure Call, RPC) is a computer communications protocol

⚫ The agreement allows to run a subroutine call to another computer in a computer program, the programmer without additional programming for this interaction

⚫ If the software according to object-oriented programming, it may also be referred to as remote procedure calls or remote call remote method invocation

2. Comparison of popular RPC framework

 

 

3. golang how to achieve RPC 

⚫ golang RPC is very simple to implement, provides a packaged library official, there are some third-party libraries

⚫ golang official net / rpc library encoding / gob codec, support for tcp and http data transmission, as other languages ​​do not support gob codec, so golang of RPC between the server only supports the development of client golang alternately

⚫ official also provided net / rpc / jsonrpc library implements RPC methods, jsonrpc using JSON for data encoding and decoding, and thus support cross-language calls, currently jsonrpc library is based on the tcp protocol does not support the http transmission

⚫ golang of RPC four conditions must be met before they can

◼ structure field first letter should be capitalized, to cross-domain access, so uppercase

◼ function name must be capitalized (serial number can be derived)

The first parameter is a function ◼ reception parameter, the second parameter is returned to the client parameter must be a pointer type

◼ function must have a return value error

⚫ example: golang implement RPC program, seeking to achieve a rectangular area and perimeter

⚫ exercises: to imitate the previous example, to achieve their own RPC program, the server receives two parameters, you can do multiplication, you can also do quotient and remainder operations, client access and mass participation, obtain the following results:

⚫ In addition, net / rpc / jsonrpc library by json format codec, support for cross-language calls

4. RPC call flow

⚫ micro Services Architecture under the general data exchange is internal RPC, REST outside

 

 

 

⚫ split the service function module according to the various micro-services, collaboration project with improved efficiency, reduction module coupling, increase the system availability, etc., but the development of relatively high threshold, such as using RPC frame, post-service monitoring, etc.

⚫ Under normal circumstances, we will call a local function code directly, under the micro-service architecture, we need to function as a separate service running, the client through the network calls

The network data transmission format

⚫ mature frame have custom RPC transport protocol, where the network transmission format is defined as follows, in front of a fixed length message header, followed by variable length message body

 

6. Implement RPC server

What ⚫ server receives data needs to include?

◼ call function name, parameter list

◼ generally agreed upon function return value is the second error type

◼ achieved by reflection

What ⚫ server needs to solve the problem is?

◼ Client only pass over when you call the function name, the function name to map the need to maintain the mapping between the function

⚫ server core function of what?

◼ maintenance function names map to the function of the reflection values

◼ client-side pass the function name, the parameter list, the server should resolve to reflectance values, called for execution

◼ return value of the function package, and returned to the client through the network

7. implement RPC clients

⚫ client only function prototypes, using reflect.MakeFunc () function call to complete the prototype of

⚫ reflect.MakeFunc () is called from Client key function prototype to the network

8. implement an RPC communication test

⚫ a registered user queries to the server, the client will go RPC call

 

 

 

Guess you like

Origin www.cnblogs.com/zhangyafei/p/11930402.html