QuickStart

    It only install the basic features of rpcx. If you want to use etcd, you should add tag:

    1. go get -u -v -tags "etcd" github.com/smallnest/rpcx/...

    And if you want to use quic, you also need to add quic tag:

    1. go get -u -v -tags "quic etcd" github.com/smallnest/rpcx/...

    I recommend you to install all tags even though you won't use them so far:

    tags means:

    • quic: support quic transport
    • kcp: support kcp transport
    • zookeeper: support zookeeper register
    • etcd: support etcd register
    • consul: support consul register
    • ping: support network quality load balancing
    • reuseport: support reuseport

    You can write your service just like writing a plain Go struct:

    1. import "context"
    2. type Args struct {
    3. A int
    4. B int
    5. }
    6. type Reply struct {
    7. C int
    8. }
    9. func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
    10. reply.C = args.A * args.B
    11. return nil
    12. }

    Now you has defined one service Arith and implemented its Mul method. we will introduce how to explore it to server and how clients to call it in next steps.

    You can use three lines to explore the above service:

    1. s := server.NewServer()
    2. s.RegisterName("Arith", new(Arith), "")
    3. s.Serve("tcp", ":8972")

    You register your service with Name Arith

    You can register service by the below:

    It uses the service type name as service name.

    1. // #1
    2. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
    3. // #2
    4. xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)
    5. defer xclient.Close()
    6. // #3
    7. args := &example.Args{
    8. A: 10,
    9. B: 20,
    10. }
    11. // #4
    12. reply := &example.Reply{}
    13. // #5
    14. err := xclient.Call(context.Background(), "Mul", args, reply)
    15. log.Fatalf("failed to call: %v", err)
    16. }

    #2 creates a XClient, and passes FailMode, SelectMode and default option.FailMode indicates the client how to handle call failures: retry, return fast or retry another server?SelectMode indicates the client how to select a server if there are multiple servers for one service.

    #3 defines the request: we want to get result of 10 * 20. Of course we know the result must be 200 but we will see whether reply from the server is 200.

    #4 defines the result object, it is a zero value and actually rpcx can use it to know type of the result and then unmarshal the result to it.

    #5 call the remote service and gets the result synchronously。

    You can call the service synchronously:

    1. d := client.NewPeer2PeerDiscovery("tcp@"+*addr2, "")
    2. xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)
    3. defer xclient.Close()
    4. args := &example.Args{
    5. A: 10,
    6. B: 20,
    7. }
    8. reply := &example.Reply{}
    9. call, err := xclient.Go(context.Background(), "Mul", args, reply, nil)
    10. if err != nil {
    11. log.Fatalf("failed to call: %v", err)
    12. }
    13. replyCall := <-call.Done
    14. if replyCall.Error != nil {
    15. log.Fatalf("failed to call: %v", replyCall.Error)
    16. } else {
    17. log.Printf("%d * %d = %d", args.A, args.B, reply.C)
    18. }

    By smallnest updated 2018-12-04 11:47:26