Group

Example:group

When you register a service, maybe you have noticed there is third parameter and we set it as empty string at most examples. Actually you add some meta for those services.

You can manage rpcx service via rpcx-ui).

group is a meta data. If you set group metadata for some services, only clients in this group can access those services.

```go server.go

func main() { flag.Parse()

  1. go createServer1(*addr1, "")
  2. go createServer2(*addr2, "group=test")
  3. select {}

}

func createServer1(addr, meta string) { s := server.NewServer() s.RegisterName("Arith", new(example.Arith), meta) s.Serve("tcp", addr)}

func createServer2(addr, meta string) { s := server.NewServer() s.RegisterName("Arith", new(Arith), meta) s.Serve("tcp", addr)}

  1. Discovery can find the group. Client can use `option.Group` to set group.
  2. If you have not set `option.Group`, clients can access any services whether services set group or not.
  3. ```go client.go
  4. option := client.DefaultOption
  5. option.Group = "test"
  6. xclient := client.NewXClient("Arith", client.Failover, client.RoundRobin, d, option)
  7. defer xclient.Close()
  8. args := &example.Args{
  9. A: 10,
  10. B: 20,
  11. }
  12. for {
  13. reply := &example.Reply{}
  14. err := xclient.Call(context.Background(), "Mul", args, reply)
  15. if err != nil {
  16. log.Fatalf("failed to call: %v", err)
  17. }
  18. log.Printf("%d * %d = %d", args.A, args.B, reply.C)
  19. time.Sleep(1e9)
  20. }

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

原文:

http://doc.rpcx.site/part3/group.html