-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
48 lines (38 loc) · 1.04 KB
/
cli.go
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
package main
import (
"context"
"fmt"
// 引用上面生成的proto文件
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/client"
proto "helloworld/proto"
)
type logWrapper struct {
client.Client
}
func (l *logWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
fmt.Printf("wrapper client request to service: %s method %s\n", req.Service(), req.Endpoint())
return l.Client.Call(ctx, req, rsp)
}
// 实现client.Wrapper充当日志包装器
func logWrap(c client.Client) client.Client {
return &logWrapper{c}
}
func main() {
// new一个服务
// service := micro.NewService()
service := micro.NewService(micro.Name("greeter.client"), micro.WrapClient(logWrap))
// 解析命令行flag
service.Init()
// 使用proto创建一个客户端
cl := proto.NewGreeterService("greeter", service.Client())
// 发出请求
rsp, err := cl.Hello(context.Background(), &proto.HelloRequest{
Name: "John",
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(rsp.Greeting)
}