From 0b544aec5c07cf64e9a4b0227404aaf3d5131aa3 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 23 Oct 2023 14:31:44 +0800 Subject: [PATCH] feat:consul redis --- common/config/config.go | 6 +++-- debug.ini | 2 ++ local.ini | 2 ++ main.go | 60 +++++++++++++++-------------------------- release.ini | 2 ++ 5 files changed, 31 insertions(+), 41 deletions(-) diff --git a/common/config/config.go b/common/config/config.go index c774286..67baa4e 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -26,8 +26,10 @@ type MysqlCodeConfig struct { //redis配置 type RedisConfig struct { - REDIS_HOST string - REDIS_PASSWORD string + REDIS_HOST string + REDIS_PASSWORD string + REDIS_CLUSTER_HOST string + REDIS_CLUSTER_PASSWORD string } //jwt diff --git a/debug.ini b/debug.ini index 696fef9..fc7ab8f 100644 --- a/debug.ini +++ b/debug.ini @@ -11,6 +11,8 @@ MYSQL_DB=hilo_code [REDIS] REDIS_HOST=47.244.34.27:6379 REDIS_PASSWORD=8QZ9JD1zLvPR3yHf +REDIS_CLUSTER_HOST=47.244.34.27:6379 +REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf [JWT] SECRET=hilo1632 ISSUER_API=hiloApi diff --git a/local.ini b/local.ini index 65d1298..f97ca1a 100644 --- a/local.ini +++ b/local.ini @@ -11,6 +11,8 @@ MYSQL_DB=hilo_code [REDIS] REDIS_HOST=47.244.34.27:6379 REDIS_PASSWORD=8QZ9JD1zLvPR3yHf +REDIS_CLUSTER_HOST=47.244.34.27:6379 +REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf [JWT] SECRET=hilo1632 ISSUER_API=hiloApi diff --git a/main.go b/main.go index 1af6de6..348a2a0 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,12 @@ import ( "context" "fmt" "github.com/go-redis/redis/v8" - consulapi "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-algoCenter/algo" - "hilo-algoCenter/common/consul" + "hilo-algoCenter/common/config" "hilo-algoCenter/common/mylogrus" "hilo-algoCenter/protocol/userCenter" "time" @@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{ } func main() { - client, err := consulapi.NewClient(consulapi.DefaultConfig()) //非默认情况下需要设置实际的参数 - if err != nil { - mylogrus.MyLog.Fatalln(err) - } - if client == nil { - mylogrus.MyLog.Fatalln("Fail to get consul client.") - } - - redisAddress := default_redis_address - redisPassword := default_redis_password - - kv := client.KV() - if kv != nil { - p, _, err := kv.Get("redis_address", nil) - if err == nil && p != nil { - redisAddress = string(p.Value) - } - p, _, err = kv.Get("redis_password", nil) - if err == nil && p != nil { - redisPassword = string(p.Value) - } - } + // init redis rdb := redis.NewClient(&redis.Options{ - Addr: redisAddress, - Password: redisPassword, + Addr: config.GetConfigRedis().REDIS_HOST, + Password: config.GetConfigRedis().REDIS_PASSWORD, DB: redis_section, }) - if rdb == nil { - mylogrus.MyLog.Fatalf("failed to connect redis %s\n", redisAddress) - } result, err := rdb.Ping(context.Background()).Result() if err != nil { @@ -67,20 +42,27 @@ func main() { mylogrus.MyLog.Fatalf("Invalid ping response %s", result) } - cataLog := client.Catalog() - if cataLog == nil { - mylogrus.MyLog.Fatalln("No catalog.") - } - addr, err := consul.GetServices(cataLog, "userCenter") + // init redis cluster + rdbCluster := redis.NewClient(&redis.Options{ + Addr: config.GetConfigRedis().REDIS_CLUSTER_HOST, + Password: config.GetConfigRedis().REDIS_CLUSTER_PASSWORD, + }) + redisKey := fmt.Sprintf("service:userCenter") + ipPorts, err := rdbCluster.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 { } - 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) } diff --git a/release.ini b/release.ini index 5897408..0b67f89 100644 --- a/release.ini +++ b/release.ini @@ -11,6 +11,8 @@ MYSQL_DB=hilo_code [REDIS] REDIS_HOST=r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379 REDIS_PASSWORD= +REDIS_CLUSTER_HOST=r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379 +REDIS_CLUSTER_PASSWORD= [JWT] SECRET=hilo1504 ISSUER_API=hiloApi -- 2.22.0