This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathclient.go
More file actions
72 lines (60 loc) · 1.91 KB
/
client.go
File metadata and controls
72 lines (60 loc) · 1.91 KB
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
package main
import (
"context"
"fmt"
"time"
proto "github.com/micro-in-cn/tutorials/examples/basic-practices/micro-service/proto"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/util/log"
)
func NewCallWrapper1() client.CallWrapper {
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
// WrapCall在发生在均衡之后,此时已经知道要发给哪台服务
log.Info("[NewCallWrapper1] 准备发往服务节点:", node.Address)
// 修改请求参数
body := req.Body().(*proto.HelloRequest)
body.Name = "Micro China"
// 模拟随机错误
if time.Now().Second() < 20 {
return fmt.Errorf("随机错误,请求中止!")
}
newMd := metadata.Metadata{}
newMd["call-wrapped1"] = "call-wrapped-value1"
ctx = metadata.NewContext(ctx, newMd)
err := cf(ctx, node, req, rsp, opts)
return err
}
}
}
func NewCallWrapper2() client.CallWrapper {
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
log.Info("[NewCallWrapper2] Wrapper工作:", node.Address)
newMd, b := metadata.FromContext(ctx)
if !b {
newMd = metadata.Metadata{}
}
newMd["call-wrapped2"] = "call-wrapped-value2"
ctx = metadata.NewContext(ctx, newMd)
err := cf(ctx, node, req, rsp, opts)
return err
}
}
}
func main() {
cli := client.NewClient(
client.WrapCall(
NewCallWrapper1(),
NewCallWrapper2()),
)
req := client.NewRequest("wrap.call.service", "Greeter.Hello", &proto.HelloRequest{Name: "Micro中国"})
rsp := &proto.HelloResponse{}
err := cli.Call(context.Background(), req, rsp)
if err != nil {
panic(err)
}
log.Info(rsp.Greeting)
}