grpcexercises/consuldemo/pkg/consul/consul.go

36 lines
610 B
Go
Raw Permalink Normal View History

2023-12-16 08:32:39 +08:00
package consul
import (
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
)
var client *api.Client
// InitConsulClient 初始化consul客户端
func InitConsulClient(address string) error {
var err error
config := api.DefaultConfig()
config.Address = address
client, err = api.NewClient(config)
if err != nil {
return errors.Wrap(err, "init consul client error")
}
_, err = client.Status().Leader()
if err != nil {
return errors.Wrap(err, "get consul leader error")
}
return nil
}
// GetConsulClient 获取consul客户端
func GetConsulClient() *api.Client {
return client
}