代码拉取完成,页面将自动刷新
package rpcClientService
import (
"context"
"gitee.com/jmesyan/impetus/filter"
"gitee.com/jmesyan/impetus/log"
"github.com/kudoochui/rpcx/client"
"sync"
)
var _rpcClientService *RpcClientService
var once sync.Once
type RpcClientService struct {
opts *Options
rpcClient *client.OneClient
lock sync.RWMutex
rpcFilter filter.Filter
}
func GetRpcClientService() *RpcClientService {
once.Do(func() {
_rpcClientService = &RpcClientService{
}
})
return _rpcClientService
}
func (r *RpcClientService) Initialize(opts ...Option) {
options := newOptions(opts...)
r.opts = options
var d client.ServiceDiscovery
switch r.opts.RegistryType {
case "consul":
d = client.NewConsulDiscovery(r.opts.BasePath, "", []string{r.opts.RegistryAddr}, nil)
case "etcd":
d = client.NewEtcdDiscovery(r.opts.BasePath, "", []string{r.opts.RegistryAddr}, nil)
case "etcdv3":
d = client.NewEtcdV3Discovery(r.opts.BasePath, "", []string{r.opts.RegistryAddr}, nil)
case "zookeeper":
d = client.NewZookeeperDiscovery(r.opts.BasePath, "", []string{r.opts.RegistryAddr}, nil)
}
var s client.SelectMode
switch r.opts.SelectMode {
case "RoundRobin":
s = client.RoundRobin
case "WeightedRoundRobin":
s = client.WeightedRoundRobin
case "WeightedICMP":
s = client.WeightedICMP
case "ConsistentHash":
s = client.ConsistentHash
case "Closest":
s = client.Closest
default:
s = client.RandomSelect
}
r.lock.Lock()
r.rpcClient = client.NewOneClient(client.Failtry, s, d, client.DefaultOption)
r.lock.Unlock()
}
func (r *RpcClientService) Call(servicePath string, serviceMethod string, args interface{}, reply interface{}) error {
if r.rpcFilter != nil {
r.rpcFilter.Before(servicePath + "." + serviceMethod, args)
}
r.lock.RLock()
err := r.rpcClient.Call(context.TODO(), servicePath, serviceMethod, args, reply)
if err != nil {
log.Error("rpc call error: %v", err)
}
r.lock.RUnlock()
if r.rpcFilter != nil {
r.rpcFilter.After(servicePath + "." + serviceMethod, reply)
}
return err
}
func (r *RpcClientService) Go(servicePath string, serviceMethod string, args interface{}, reply interface{}, chanRet chan *client.Call) {
r.lock.RLock()
defer r.lock.RUnlock()
if _,err := r.rpcClient.Go(context.TODO(),servicePath, serviceMethod, args, reply, chanRet); err != nil {
log.Error("rpc go error: %v", err)
}
}
// Set a filter for rpc
func (r *RpcClientService) SetRpcFilter(f filter.Filter) {
r.rpcFilter = f
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。