-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
72 lines (66 loc) · 1.71 KB
/
client.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
package es
import (
"context"
"github.com/jyk1987/es/tool"
"github.com/jyk1987/es/data"
"github.com/jyk1987/es/log"
"github.com/jyk1987/es/node"
"github.com/smallnest/rpcx/client"
)
var _RpcClientCache = make(map[string]client.XClient)
// getRpcClient 获取一个rpc客户端
func getRpcClient(nodeName string) (client.XClient, error) {
tool.RLock(nodeName)
c := _RpcClientCache[nodeName]
if c != nil {
tool.RUnlock(nodeName)
return c, nil
}
tool.RUnlock(nodeName)
tool.Lock(nodeName)
consulServers := []string{node.GetNodeConfig().Consul}
log.Log.Debug("consul server:", consulServers)
d, e := client.NewConsulDiscovery(data.DiscoverBasePath, nodeName, consulServers, nil)
if e != nil {
log.Log.Error(e)
return nil, e
}
c = client.NewXClient(nodeName, client.Failover, client.RoundRobin, d, client.DefaultOption)
_RpcClientCache[nodeName] = c
tool.Unlock(nodeName)
return c, nil
}
func callServiceExecute(nodeName, path, method string, params ...interface{}) (*data.Result, error) {
c, e := getRpcClient(nodeName)
if e != nil {
return nil, e
}
req := &data.Request{
NodeName: nodeName,
Path: path,
Method: method,
}
req.SetParameters(params...)
result := new(data.Result)
tool.RLock(nodeName)
defer tool.RUnlock(nodeName)
e = c.Call(context.Background(), node.RpcExecuteFuncName, req, result)
if e != nil {
return nil, e
}
return result, nil
}
func callServiceGetInfo(nodeName string) (*data.Result, error) {
c, e := getRpcClient(nodeName)
if e != nil {
return nil, e
}
result := new(data.Result)
tool.RLock(nodeName)
defer tool.RUnlock(nodeName)
e = c.Call(context.Background(), node.RpcGetInfoFuncName, nil, result)
if e != nil {
return nil, e
}
return result, nil
}