企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 概述 `Fork`表示向所有服务器发送请求,只要任意一台服务器正确返回就成功。此时FailMode 和 SelectMode的设置是无效的。 ## 实例 <details> <summary>server.go</summary> ``` package main import ( "context" "flag" "github.com/smallnest/rpcx/server" ) var ( addr1 = flag.String("addr1", "localhost:8972", "server1 address") addr2 = flag.String("addr2", "localhost:9981", "server2 address") ) type Args struct { A int B int } type Reply struct { C int } type Arith int func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error { reply.C = args.A * args.B * 100 return nil } func main() { flag.Parse() go createServer1(*addr1, "") go createServer2(*addr2, "") select {} } func createServer1(addr, meta string) { s := server.NewServer() s.RegisterName("Arith", new(Arith), meta) s.Serve("tcp", addr) } func createServer2(addr, meta string) { s := server.NewServer() s.RegisterName("Arith", new(Arith), meta) s.Serve("tcp", addr) } ``` </details> <br/> <details> <summary>描述</summary> ``` package main import ( "context" "flag" "log" "time" "github.com/smallnest/rpcx/client" ) var ( addr1 = flag.String("addr1", "tcp@localhost:8972", "server1 address") addr2 = flag.String("addr2", "tcp@localhost:9981", "server2 address") ) func main() { type Args struct { A int B int } type Reply struct { C int } flag.Parse() d := client.NewMultipleServersDiscovery([]*client.KVPair{{Key: *addr1}, {Key: *addr2}}) xclient := client.NewXClient("Arith", client.Failover, client.RoundRobin, d, client.DefaultOption) defer xclient.Close() args := Args{ A: 10, B: 20, } for { reply := &Reply{} err := xclient.Fork(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) time.Sleep(1e9) } } ``` </details> <br/>