代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。