别名
例程:alias
该插件可以给服务和它的方法起别名。
例如,以下代码使用a.b.c.d
作为Arith
的别名、Times
作为Mul
的别名。
func main() {
flag.Parse()
a := serverplugin.NewAliasPlugin()
a.Alias("a.b.c.d", "Times", "Arith", "Mul")
s := server.NewServer()
s.Plugins.Add(a)
s.RegisterName("Arith", new(example.Arith), "")
err := s.Serve("reuseport", *addr)
if err != nil {
panic(err)
}
}
客户端可以使用别名访问服务:
func main() {
flag.Parse()
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
option := client.DefaultOption
option.ReadTimeout = 10 * time.Second
xclient := client.NewXClient("a.b.c.d", client.Failtry, client.RandomSelect, d, option)
defer xclient.Close()
args := &example.Args{
A: 10,
B: 20,
}
reply := &example.Reply{}
err := xclient.Call(context.Background(), "Times", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
}