Alias

Example:alias

This plugin can alias a service and its method.

For example, the below code use a.b.c.d as the alias of Arith, and Times as the alias of Mul.

  1. func main() {
  2. flag.Parse()
  3. a := serverplugin.NewAliasPlugin()
  4. a.Alias("a.b.c.d", "Times", "Arith", "Mul")
  5. s := server.NewServer()
  6. s.Plugins.Add(a)
  7. s.RegisterName("Arith", new(example.Arith), "")
  8. err := s.Serve("reuseport", *addr)
  9. if err != nil {
  10. panic(err)
  11. }
  12. }

Client can use alias to call the service:

  1. func main() {
  2. flag.Parse()
  3. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
  4. option := client.DefaultOption
  5. option.ReadTimeout = 10 * time.Second
  6. xclient := client.NewXClient("a.b.c.d", client.Failtry, client.RandomSelect, d, option)
  7. defer xclient.Close()
  8. args := &example.Args{
  9. A: 10,
  10. B: 20,
  11. }
  12. reply := &example.Reply{}
  13. err := xclient.Call(context.Background(), "Times", args, reply)
  14. if err != nil {
  15. log.Fatalf("failed to call: %v", err)
  16. }
  17. log.Printf("%d * %d = %d", args.A, args.B, reply.C)
  18. }

By smallnest updated 2018-03-27 11:12:10

原文:

http://doc.rpcx.site/part4/alias.html