From 42fc206b5f25320b62f2b36888abcc19db415038 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 23 Oct 2023 13:55:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=8E=BB=E6=8E=89consul?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index f7e7346..64a74d4 100644 --- a/main.go +++ b/main.go @@ -6,13 +6,11 @@ import ( "fmt" "github.com/go-redis/redis/v8" "github.com/golang/protobuf/proto" - "github.com/hashicorp/consul/api" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver/manual" "hilo-socketCenter/common" - "hilo-socketCenter/common/consul" "hilo-socketCenter/common/dingding" "hilo-socketCenter/common/mylogrus" "hilo-socketCenter/common/redisCli" @@ -43,24 +41,22 @@ var kacp = keepalive.ClientParameters{ // 初始化userCenterClient func init() { - client, err := api.NewClient(api.DefaultConfig()) //非默认情况下需要设置实际的参数 + redisKey := fmt.Sprintf("service:userCenter") + ipPorts, err := redisCli.GetRedisCluster().ZRangeByScore(context.Background(), redisKey, &redis.ZRangeBy{ + Min: fmt.Sprintf("%d", time.Now().Add(-time.Second*15).Unix()), // 3倍心跳 + Max: "+inf", + }).Result() if err != nil { - mylogrus.MyLog.Fatalln(err) + failMsg := fmt.Sprintf("get service fail,svc:%v,err:%v", "userCenter", err) + mylogrus.MyLog.Errorf(failMsg) + } else if len(ipPorts) > 0 { } - cataLog := client.Catalog() - if cataLog == nil { - mylogrus.MyLog.Fatalln("No catalog.") - } - addr, err := consul.GetServices(cataLog, "userCenter") - if err != nil { - mylogrus.MyLog.Fatalln(err) - } - if len(addr) == 0 { - mylogrus.MyLog.Fatalln("No userCenter available.") + if len(ipPorts) <= 0 { + ipPorts = []string{"127.0.0.1:50040"} } - addresses := make([]resolver.Address, len(addr)) - for i, s := range addr { + addresses := make([]resolver.Address, len(ipPorts)) + for i, s := range ipPorts { addresses[i].Addr = s mylogrus.MyLog.Infof("address : %s", s) } -- 2.22.0