多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 实例 <details> <summary>server.go</summary> ``` package main import ( "context" "flag" "fmt" "github.com/smallnest/rpcx/server" ) var ( addr = flag.String("addr", "localhost:8973", "server address") ) type Args struct { A int B int } type Reply struct { C int } type Arith struct{} // the second parameter is not a pointer func (t *Arith) Mul(ctx context.Context, args Args, reply *Reply) error { reply.C = args.A * args.B fmt.Println("C=", reply.C) return nil } func main() { flag.Parse() s := server.NewServer() //s.Register(new(Arith), "") s.RegisterName("Arith", new(Arith), "") err := s.Serve("tcp", *addr) if err != nil { panic(err) } } ``` </details> <br/> <details> <summary>client.go</summary> ``` package main import ( "context" "flag" "log" "github.com/smallnest/rpcx/protocol" "github.com/smallnest/rpcx/client" ) var ( addr = flag.String("addr", "localhost:8973", "server address") ) func main() { type Args struct { A int B int } type Reply struct { C int } flag.Parse() d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "") opt := client.DefaultOption opt.SerializeType = protocol.JSON xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, opt) defer xclient.Close() args := Args{ A: 10, B: 20, } reply := &Reply{} err := xclient.Call(context.Background(), "Mul", args, reply) if err != nil { log.Fatalf("failed to call: %v", err) } log.Printf("%d * %d = %d", args.A, args.B, reply.C) } ``` </details> <br/>