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 use the rpc package in golang

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In this issue, the editor will bring you about how to use the rpc package in golang. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction

Golang's rpc supports three levels of RPC:TCP, HTTP, and JSONRPC. But Go's RPC package is a unique RPC, which is different from traditional RPC systems in that it only supports the interaction between server and client developed by Go, because internally, they are encoded by Gob.

Go RPC functions can be accessed remotely only if they meet the following conditions, otherwise they will be ignored. The detailed requirements are as follows:

Function must be exported (initials uppercase)

There must be two parameters of the export type

The first parameter is the received parameter, the second parameter is the parameter returned to the client, and the second parameter must be of pointer type.

The function also has a return value of error

For example, the correct RPC function format is as follows:

Func (t * T) MethodName (argType T1, replyType * T2) error

T, T1 and T2 types must be able to be encoded and decoded by encoding/gob packets.

Example

Give an example of http.

Here is the code on the http server side:

Package mainimport ("errors"net"net/rpc"log"net/http") type Args struct {A, B int} type Quotient struct {Quo, Rem int} type Arith intfunc (t * Arith) Multiply (args * Args, reply * int) error {* reply = args.A * args.B return nil} func (t * Arith) Divide (args * Args) Quo * Quotient) error {if args.B = = 0 {return errors.New ("divide by zero")} quo.Quo = args.A / args.B quo.Rem = args.A% args.B return nil} func main () {arith: = new (Arith) rpc.Register (arith) rpc.HandleHTTP () l, e: = net.Listen ("tcp") ": 1234") if e! = nil {log.Fatal ("listen error:", e)} http.Serve (l, nil)}

After a brief analysis of the above example, we first instantiate an Arith object arith, then register the rpc service for arith, and then mount the rpc to the http service. When the http service is opened, we can call the methods in arith that conform to the rpc standard through the rpc client.

Please look at the code of the client:

Package mainimport ("net/rpc"log"fmt") type Args struct {A, B int} type Quotient struct {Quo, Rem int} func main () {client, err: = rpc.DialHTTP ("tcp", "127.0.0.1 log 1234") if err! = nil {log.Fatal ("dialing:" Err)} / / Synchronous call args: = & Args {7 d*%d=%d 8} var reply int err = client.Call ("Arith.Multiply", args, & reply) if err! = nil {log.Fatal ("arith error:", err)} fmt.Printf ("Arith:% d*%d=%d\ n", args.A, args.B Reply) / / Asynchronous call quotient: = new (Quotient) divCall: = client.Go ("Arith.Divide", args, quotient, nil) replyCall: =

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

Servers

Wechat

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

12
Report