QuickStart

Installation

First, you should install rpcx:

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

It only install the basic features of rpcx. If you want to use etcd, you should add etcd 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:

  1. go get -u -v -tags "reuseport quic kcp zookeeper etcd consul ping" github.com/smallnest/rpcx/...

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

    Implement Service

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. type Arith int
  10. func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
  11. reply.C = args.A * args.B
  12. return nil
  13. }

Arith is a Go type and it has a one method Mul.The first parameter of Mul method is context.Context。The second parameter of Mul method is args。 It contains request data: A and B。The third parameter of Mul method is reply。It is a pointer to Reply.The Mul method returns an error (can be nil).Mul sets Reply.C with result of A * B.

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.

Implement Server

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:

  1. s.Register(new(example.Arith), "")

It uses the service type name as service name.

Implement Client

  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. if err != nil {
  16. log.Fatalf("failed to call: %v", err)
  17. }
  18. log.Printf("%d * %d = %d", args.A, args.B, reply.C)

#1 defines a service discovery implementation. In this example we use the simplest discovery: Peer2PeerDiscovery. Client get the server address by accessing this discovery and connect the server directly.

#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。

Call the service asynchronously

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. }

You must use xclient.Go to replace xclient.Call and ti returns a channel. You can read the result from the channel.

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

原文:

http://doc.rpcx.site/part1/quickstart.html