forked from apache/dubbo-go
/
test.go
161 lines (144 loc) · 4.78 KB
/
test.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
"fmt"
_ "net/http/pprof"
)
import (
// "github.com/AlexStocks/goext/log"
log "github.com/AlexStocks/log4go"
jerrors "github.com/juju/errors"
)
import (
"github.com/dubbo/dubbo-go/jsonrpc"
"github.com/dubbo/dubbo-go/public"
"github.com/dubbo/dubbo-go/registry"
)
func testJsonrpc(userKey string) {
var (
err error
service string
method string
serviceIdx int
user *JsonRPCUser
ctx context.Context
conf registry.ServiceConfig
req jsonrpc.Request
serviceURL *registry.ServiceURL
clt *jsonrpc.HTTPClient
)
clt = jsonrpc.NewHTTPClient(
&jsonrpc.HTTPOptions{
HandshakeTimeout: clientConfig.connectTimeout,
HTTPTimeout: clientConfig.requestTimeout,
},
)
serviceIdx = -1
service = "com.ikurento.user.UserProvider"
for i := range clientConfig.Service_List {
if clientConfig.Service_List[i].Service == service && clientConfig.Service_List[i].Protocol == public.CODECTYPE_JSONRPC.String() {
serviceIdx = i
break
}
}
if serviceIdx == -1 {
panic(fmt.Sprintf("can not find service in config service list:%#v", clientConfig.Service_List))
}
// Create request
method = string("GetUser")
// gxlog.CInfo("jsonrpc selected service %#v", clientConfig.Service_List[serviceIdx])
conf = registry.ServiceConfig{
Group: clientConfig.Service_List[serviceIdx].Group,
Protocol: public.CodecType(public.CODECTYPE_JSONRPC).String(),
Version: clientConfig.Service_List[serviceIdx].Version,
Service: clientConfig.Service_List[serviceIdx].Service,
}
// Attention the last parameter : []UserKey{userKey}
req = clt.NewRequest(conf, method, []string{userKey})
serviceURL, err = clientRegistry.Filter(req.ServiceConfig(), 1)
if err != nil {
log.Error("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
// gxlog.CError("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
return
}
log.Debug("got serviceURL: %s", serviceURL)
// Set arbitrary headers in context
ctx = context.WithValue(context.Background(), public.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": service,
"X-Method": method,
})
user = new(JsonRPCUser)
// Call service
if err = clt.Call(ctx, *serviceURL, req, user); err != nil {
log.Error("client.Call() return error:%+v", jerrors.ErrorStack(err))
// gxlog.CError("client.Call() return error:%+v", jerrors.ErrorStack(err))
return
}
log.Info("response result:%s", user)
// gxlog.CInfo("response result:%s", user)
}
func testJsonrpcIllegalMethod(userKey string) {
var (
err error
service string
method string
serviceIdx int
user *JsonRPCUser
ctx context.Context
conf registry.ServiceConfig
req jsonrpc.Request
serviceURL *registry.ServiceURL
clt *jsonrpc.HTTPClient
)
clt = jsonrpc.NewHTTPClient(
&jsonrpc.HTTPOptions{
HandshakeTimeout: clientConfig.connectTimeout,
HTTPTimeout: clientConfig.requestTimeout,
},
)
serviceIdx = -1
service = "com.ikurento.user.UserProvider"
for i := range clientConfig.Service_List {
if clientConfig.Service_List[i].Service == service && clientConfig.Service_List[i].Protocol == public.CODECTYPE_JSONRPC.String() {
serviceIdx = i
break
}
}
if serviceIdx == -1 {
panic(fmt.Sprintf("can not find service in config service list:%#v", clientConfig.Service_List))
}
// Create request
method = string("GetUser1")
// gxlog.CInfo("jsonrpc selected service %#v", clientConfig.Service_List[serviceIdx])
conf = registry.ServiceConfig{
Group: clientConfig.Service_List[serviceIdx].Group,
Protocol: public.CodecType(public.CODECTYPE_JSONRPC).String(),
Version: clientConfig.Service_List[serviceIdx].Version,
Service: clientConfig.Service_List[serviceIdx].Service,
}
// Attention the last parameter : []UserKey{userKey}
req = clt.NewRequest(conf, method, []string{userKey})
serviceURL, err = clientRegistry.Filter(req.ServiceConfig(), 1)
if err != nil {
log.Error("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
// gxlog.CError("registry.Filter(conf:%#v) = error:%s", req.ServiceConfig(), jerrors.ErrorStack(err))
return
}
log.Debug("got serviceURL: %s", serviceURL)
// Set arbitrary headers in context
ctx = context.WithValue(context.Background(), public.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": service,
"X-Method": method,
})
user = new(JsonRPCUser)
// Call service
if err = clt.Call(ctx, *serviceURL, req, user); err != nil {
log.Error("client.Call() return error:%+v", jerrors.ErrorStack(err))
// gxlog.CError("client.Call() return error:%+v", jerrors.ErrorStack(err))
return
}
log.Info("response result:%s", user)
// gxlog.CInfo("response result:%s", user)
}