1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
master.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-10-22 01:06 . rpc ,排序权重
package consul
import (
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/server"
"gitee.com/h79/goutils/discovery/config"
"gitee.com/h79/goutils/discovery/resolver"
"gitee.com/h79/goutils/discovery/resolver/builder"
"gitee.com/h79/goutils/discovery/service"
consul "github.com/hashicorp/consul/api"
)
var _ service.Client = (*clClient)(nil)
var _ builder.Builder = (*clClient)(nil)
type clClient struct {
Base
node config.Node
}
func NewConsulClient(conf config.Config, endPoints config.EndPoints, resol resolver.Resolver) (service.Client, error) {
cli, err := NewClientWithPoints(endPoints)
if err != nil {
logger.Error("Consul.client: create consul failure, err= %+v", err)
return nil, err
}
logger.Info("Consul.client: create consul client")
b := &clClient{
Base: Base{
client: cli,
watcher: nil,
},
node: conf.Node,
}
resolver.Register(b)
return b, nil
}
func (cli *clClient) Start() error {
return nil
}
func (cli *clClient) Stop() {
}
// Resolve service.Client and resolver.Builder interface
func (cli *clClient) Resolve(target builder.Target) []builder.Address {
entries, _, err := cli.client.Health().Service(target.NameWith(""), "", true, &consul.QueryOptions{})
if err != nil {
logger.Error("Consul.client: Resolve service(%s) entry, err= %+v", target.NameWith("/"), err)
return nil
}
if len(entries) == 0 {
return nil
}
var adders []builder.Address
for _, entry := range entries {
logger.Debug("Consul.client: entry: %v", entry)
addr := server.Address{
Host: entry.Service.Address,
Port: entry.Service.Port,
}
addr.Adjust()
address := builder.Address{Addr: addr.To(), ServerName: target.SchemeWith("")}
adders = append(adders, address)
}
return adders
}
// Build
// builder.Builder interface
func (cli *clClient) Build(target builder.Target, cc builder.Connector) (builder.Resolver, error) {
logger.Info("def.Client: Build target: %+v", target)
cr := resolver.NewResolver(target, cc, cli)
return cr, nil
}
// Scheme
// builder.Builder interface
func (cli *clClient) Scheme() string {
return cli.node.Scheme
}
// Type
// builder.Builder interface
func (cli *clClient) Type() string {
return cli.node.Type
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.22.22

搜索帮助

0d507c66 1850385 C8b1a773 1850385