这里有个问题,getclient中每次生成新的会导致consul限流,如果使用同一个好像时间长了会出异常
This commit is contained in:
parent
a9ce2c9a82
commit
fedf99cc78
|
|
@ -12,32 +12,39 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
node, err := consul.FindNode("demo")
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "main",
|
||||
}).Errorf("%v", err)
|
||||
return
|
||||
}
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
for {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
// conn, err := grpc.Dial("127.0.0.1:9001", grpc.WithBlock(), grpc.WithInsecure())
|
||||
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", node.Address, node.Port), grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin"))
|
||||
if err != nil {
|
||||
cancel()
|
||||
}
|
||||
defer conn.Close()
|
||||
c := pb.NewHelloClient(conn)
|
||||
resp, err := c.Say(ctx, &pb.SayRequest{Name: "jiale", Day: "2008-08-08"})
|
||||
if err != nil {
|
||||
node, err := consul.FindNode("demo")
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "main",
|
||||
}).Errorf("%v", err)
|
||||
fmt.Printf("%v\n", err)
|
||||
continue
|
||||
}
|
||||
if node == nil {
|
||||
fmt.Println("node is empty")
|
||||
continue
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
// conn, err := grpc.Dial("127.0.0.1:9001", grpc.WithBlock(), grpc.WithInsecure())
|
||||
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", node.Address, node.Port), grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin"))
|
||||
if err != nil {
|
||||
cancel()
|
||||
}
|
||||
defer conn.Close()
|
||||
c := pb.NewHelloClient(conn)
|
||||
resp, err := c.Say(ctx, &pb.SayRequest{Name: "jiale", Day: "2008-08-08"})
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "main",
|
||||
}).Errorf("%v", err)
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"func": "main",
|
||||
}).Errorf("%v", err)
|
||||
}).Infof("%s", resp.Reply)
|
||||
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"func": "main",
|
||||
}).Infof("%s", resp.Reply)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,19 +13,33 @@ const (
|
|||
consulAddr string = "127.0.0.1:8500"
|
||||
)
|
||||
|
||||
//Register 注册服务到consul
|
||||
func Register(name string, addr string, port int, tags ...string) {
|
||||
var _client *consulapi.Client
|
||||
|
||||
func getClient() *consulapi.Client {
|
||||
if _client != nil {
|
||||
return _client
|
||||
}
|
||||
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = consulAddr
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "Register",
|
||||
"func": "getClient",
|
||||
}).Errorf("NewClient: %v", err)
|
||||
return nil
|
||||
}
|
||||
_client = client
|
||||
return client
|
||||
}
|
||||
|
||||
//Register 注册服务到consul
|
||||
func Register(name string, addr string, port int, tags ...string) {
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建注册到consul的服务到
|
||||
registration := new(consulapi.AgentServiceRegistration)
|
||||
registration.ID = fmt.Sprintf("%s-%s:%d", name, addr, port)
|
||||
|
|
@ -52,29 +66,18 @@ func Register(name string, addr string, port int, tags ...string) {
|
|||
|
||||
//DeRegister 取消consul注册的服务
|
||||
func DeRegister(name string, addr string, port int) {
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = consulAddr
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "DeRegister",
|
||||
}).Errorf("%v", err)
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client.Agent().ServiceDeregister(fmt.Sprintf("%s-%s:%d", name, addr, port))
|
||||
}
|
||||
|
||||
//FindNode 查找节点
|
||||
func FindNode(servicename string) (*consulapi.AgentService, error) {
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = consulAddr
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "FindNode",
|
||||
}).Errorf("NewClient: %v", err)
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return nil, nil
|
||||
}
|
||||
services, _, err := client.Health().Service(servicename, "", true, nil)
|
||||
if err != nil {
|
||||
|
|
@ -94,14 +97,9 @@ func FindNode(servicename string) (*consulapi.AgentService, error) {
|
|||
|
||||
//FindServer 从consul中发现服务
|
||||
func FindServer() {
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = consulAddr
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"func": "DeRegister",
|
||||
}).Errorf("%v", err)
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有service
|
||||
|
|
@ -126,28 +124,22 @@ func FindServer() {
|
|||
}
|
||||
|
||||
//CheckHeath 健康检查
|
||||
func CheckHeath() {
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = "127.0.0.1:8500"
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.Fatal("consul client error : ", err)
|
||||
func CheckHeath(serviceid string) {
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
// 健康检查
|
||||
a, b, _ := client.Agent().AgentHealthServiceByID("111")
|
||||
a, b, _ := client.Agent().AgentHealthServiceByID(serviceid)
|
||||
fmt.Println(a)
|
||||
fmt.Println(b)
|
||||
}
|
||||
|
||||
//KVTest test
|
||||
func KVTest() {
|
||||
// 创建连接consul服务配置
|
||||
config := consulapi.DefaultConfig()
|
||||
config.Address = "127.0.0.1:8500"
|
||||
client, err := consulapi.NewClient(config)
|
||||
if err != nil {
|
||||
log.Fatal("consul client error : ", err)
|
||||
client := getClient()
|
||||
if client == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// KV, put值
|
||||
|
|
|
|||
Loading…
Reference in New Issue