Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a RPC function in Golang

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to achieve a RPC function in Golang. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Client: the initiator of the service call, also known as the service consumer server: a program running on a remote computer, which contains the method client stub to be called and accessed by the client: store the server address and port message. the request parameters of the client are packaged into a network message and sent to the server. Receive the packet returned by the server. The program runs on the client. Server stub: receive the data packet sent by the client, parse the data packet, call the data packet, call the specific service method, package and send the call result to the client side. The program runs on the server side.

Working process:

1. If the client wants to initiate a remote procedure call, first call the name of the function method that you want to use by calling the local client Stub program

2. The client Stub program receives the function call request from the client, serializes the method name and parameters carried by the client request, and packages them into data packets.

3. The client Stub finds the IP address of the remote server program, calls the Socket communication protocol, and sends it to the server through the network.

4. The server Stub program receives the data packet information sent by the client, and deserializes the data through the agreed protocol to get the requested method name and request parameters.

5. The server Stub program prepares the relevant data, calls the corresponding functional methods of the local Server, and passes in the corresponding parameters for business processing.

6. The server program executes the calling process according to the existing business logic, and returns the execution result to the server Stub program when the business execution is finished.

7. The server Stub program serializes the result of the program call according to the agreed protocol and sends it back to the client Stub program through the network.

8. The client Stub program receives the return data sent by the server Stub, deserializes the data, and passes the data returned by the call to the client request initiator.

9. The client request initiator gets the result of the call, and the whole RPC call process ends.

The related technologies involved in RPC through the above series of text description and explanation, we have understood the origin of RPC and the whole calling process of RPC. We can see that RPC is a collection of operations, which involves a lot of data operations, as well as network communication. Therefore, we summarize and analyze the technologies involved in RPC:

1. Dynamic proxy technology: the Client Stub and Sever Stub programs mentioned above are automatically generated by using dynamic proxy technology in the specific coding and development practice.

Serialization and deserialization: during the RPC call, we can see that the data needs to be transferred from one machine to another. On the Internet, all data is transmitted in bytes. In the process of programming, we often use data objects, so if we want to transfer data objects and related variables on the network, we need to serialize and deserialize the data objects.

Serialization: the process of converting an object into a sequence of bytes is called object serialization, that is, the process of encoding.

Deserialization: the process of restoring a sequence of bytes to an object is called deserialization of the object, that is, the process of decoding.

Service definition and leakage:

Func (t * T) MethodName (request T1 response * T2) error the above code is the definition standard of external exposure service method officially given by go language, which contains several main rules, namely: 1. The external exposure method has and can only have two parameters, which can only be output type or built-in type, one of the two types. 2. The second parameter of the method must be a pointer type. 3. The return type of the method is error. 4. The type of the method is exportable. * 5. The method itself is also outputable.

Type MathUtil struct {} / / this method exposes to the outside: func (mu * MathUtil) CalculateCircleArea (req float32, resp * float32) error {* resp = math.Pi * req * req / / Circular area s = π * r * r return nil / / return type}

Code explanation: in the above case, we can see: 1, the Calculate method is the service method provided by the service object MathUtil, which is used to receive the incoming circle radius data, calculate the circle area and return. 2. The first parameter, req, represents the parameter passed by the client. 3. The second parameter, resp, which represents the result of the calculation to be returned to the caller, must be a pointer type. 4. Under normal circumstances, the return value of the method is error and nil. If an exception or special case is encountered, the error is returned to the caller as a string, and the resp parameter is no longer returned to the caller.

Client connects to server:

Client, err: = rpc.DialHTTP ("tcp", "localhost:8081") if err! = nil {panic (err.Error ())}

After the client of the remote method call successfully connects to the server, you can invoke the method of the server through the method call, as follows:

Var req float32 / / request value req = 3var resp float32 / / return value err = client.Call ("MathUtil.CalculateCircleArea", req, & resp) if err! = nil {panic (err.Error ())} fmt.Println (resp)

The core of the above invocation method lies in the call of the client.Call method, which has three parameters. The first parameter represents the method name of the remote service to be called, the second parameter is the parameter to be passed in during the call, and the third parameter is the return value to be received by the call. The above Call method invocation is implemented by synchronous invocation, in addition, there is an asynchronous way to implement the invocation. The asynchronous invocation code is implemented as follows:

Var respSync * float32// Asynchronous call method syncCall: = client.Go ("MathUtil.CalculateCircleArea", req, & respSync, nil) replayDone: =

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report