-
Notifications
You must be signed in to change notification settings - Fork 83
/
quick_start.md
96 lines (73 loc) · 2.5 KB
/
quick_start.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
English | [中文](quick_start.zh_CN.md)
## Quick Start
### Prerequisites
- **[Go](https://go.dev/doc/install)**, should be greater or equal than go1.18.
- **[tRPC cmdline tools](https://github.com/trpc-group/trpc-cmdline)**, to generate stub codes from protobuf.
### Get Example Code
Example code is part of tRPC-Go repo.
Clone and change directory to helloworld.
```bash
$ git clone --depth 1 git@github.com:trpc-group/trpc-go.git
$ cd trpc-go/examples/helloworld
```
### Run the Example
1. Compile and execute the server code:
```bash
$ cd server && go run main.go
```
2. From a different terminal, compile and execute the client code:
```bash
$ cd client && go run main.go
```
You will see `Hello world!` displayed as a log.
Congratulations! You’ve just run a client-server application with tRPC-Go.
### Update protobuf
As you can see, service `Greeter` are defined in protobuf `./pb/helloworld.proto` as following:
```protobuf
service Greeter {
rpc Hello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string msg = 1;
}
message HelloReply {
string msg = 1;
}
```
It has only one method `Hello`, which takes `HelloRequest` as parameter and returns `HelloReply`.
Now, add a new method `HelloAgain`, with the same request and response:
```protobuf
service Greeter {
rpc Hello (HelloRequest) returns (HelloReply) {}
rpc HelloAgain (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string msg = 1;
}
message HelloReply {
string msg = 1;
}
```
Regenerate tRPC code by `$ make` in `./pb` directory.
The Makefile calls `trpc` which should be installed by prerequisites.
### Update and Run Server and Client
At server side `server/main.go`, add codes to implement `HelloAgain`:
```go
func (g Greeter) HelloAgain(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
log.Infof("got HelloAgain request: %s", req.Msg)
return &pb.HelloReply{Msg: "Hello " + req.Msg + " again!"}, nil
}
```
At client side `client/main.go`, add codes to call `HelloAgain`:
```go
rsp, err = c.HelloAgain(context.Background(), &pb.HelloRequest{Msg: "world"})
if err != nil {
log.Error(err)
}
log.Info(rsp.Msg)
```
Follow the `Run the Example` section to re-run your example and you will see `Hello world again!` in client log.
### What's Next
- Learn [tRPC Design](https://github.com/trpc-group/trpc).
- Read [basics tutorial](./basics_tutorial.md) to get deeper into tRPC-Go.
- Explore the [API reference](https://pkg.go.dev/trpc.group/trpc-go).