Commit 0b544aec authored by hujiebin's avatar hujiebin

feat:consul redis

parent 7aca0b0f
...@@ -28,6 +28,8 @@ type MysqlCodeConfig struct { ...@@ -28,6 +28,8 @@ type MysqlCodeConfig struct {
type RedisConfig struct { type RedisConfig struct {
REDIS_HOST string REDIS_HOST string
REDIS_PASSWORD string REDIS_PASSWORD string
REDIS_CLUSTER_HOST string
REDIS_CLUSTER_PASSWORD string
} }
//jwt //jwt
......
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=47.244.34.27:6379 REDIS_HOST=47.244.34.27:6379
REDIS_PASSWORD=8QZ9JD1zLvPR3yHf REDIS_PASSWORD=8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST=47.244.34.27:6379
REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf
[JWT] [JWT]
SECRET=hilo1632 SECRET=hilo1632
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=47.244.34.27:6379 REDIS_HOST=47.244.34.27:6379
REDIS_PASSWORD=8QZ9JD1zLvPR3yHf REDIS_PASSWORD=8QZ9JD1zLvPR3yHf
REDIS_CLUSTER_HOST=47.244.34.27:6379
REDIS_CLUSTER_PASSWORD=8QZ9JD1zLvPR3yHf
[JWT] [JWT]
SECRET=hilo1632 SECRET=hilo1632
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
...@@ -4,13 +4,12 @@ import ( ...@@ -4,13 +4,12 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
consulapi "github.com/hashicorp/consul/api"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/keepalive" "google.golang.org/grpc/keepalive"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual" "google.golang.org/grpc/resolver/manual"
"hilo-algoCenter/algo" "hilo-algoCenter/algo"
"hilo-algoCenter/common/consul" "hilo-algoCenter/common/config"
"hilo-algoCenter/common/mylogrus" "hilo-algoCenter/common/mylogrus"
"hilo-algoCenter/protocol/userCenter" "hilo-algoCenter/protocol/userCenter"
"time" "time"
...@@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{ ...@@ -29,36 +28,12 @@ var kacp = keepalive.ClientParameters{
} }
func main() { func main() {
client, err := consulapi.NewClient(consulapi.DefaultConfig()) //非默认情况下需要设置实际的参数 // init redis
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)
}
}
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(&redis.Options{
Addr: redisAddress, Addr: config.GetConfigRedis().REDIS_HOST,
Password: redisPassword, Password: config.GetConfigRedis().REDIS_PASSWORD,
DB: redis_section, DB: redis_section,
}) })
if rdb == nil {
mylogrus.MyLog.Fatalf("failed to connect redis %s\n", redisAddress)
}
result, err := rdb.Ping(context.Background()).Result() result, err := rdb.Ping(context.Background()).Result()
if err != nil { if err != nil {
...@@ -67,20 +42,27 @@ func main() { ...@@ -67,20 +42,27 @@ func main() {
mylogrus.MyLog.Fatalf("Invalid ping response %s", result) mylogrus.MyLog.Fatalf("Invalid ping response %s", result)
} }
cataLog := client.Catalog() // init redis cluster
if cataLog == nil { rdbCluster := redis.NewClient(&redis.Options{
mylogrus.MyLog.Fatalln("No catalog.") Addr: config.GetConfigRedis().REDIS_CLUSTER_HOST,
} Password: config.GetConfigRedis().REDIS_CLUSTER_PASSWORD,
addr, err := consul.GetServices(cataLog, "userCenter") })
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 { 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 { if len(ipPorts) <= 0 {
mylogrus.MyLog.Fatalln("No userCenter available.") ipPorts = []string{"127.0.0.1:50040"}
} }
addresses := make([]resolver.Address, len(addr)) addresses := make([]resolver.Address, len(ipPorts))
for i, s := range addr { for i, s := range ipPorts {
addresses[i].Addr = s addresses[i].Addr = s
mylogrus.MyLog.Infof("address : %s", s) mylogrus.MyLog.Infof("address : %s", s)
} }
......
...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code ...@@ -11,6 +11,8 @@ MYSQL_DB=hilo_code
[REDIS] [REDIS]
REDIS_HOST=r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379 REDIS_HOST=r-eb3btxn8vfdsuwdbuf.redis.dubai.rds.aliyuncs.com:6379
REDIS_PASSWORD= REDIS_PASSWORD=
REDIS_CLUSTER_HOST=r-eb3yt6k8zgxs62kjjs.redis.dubai.rds.aliyuncs.com:6379
REDIS_CLUSTER_PASSWORD=
[JWT] [JWT]
SECRET=hilo1504 SECRET=hilo1504
ISSUER_API=hiloApi ISSUER_API=hiloApi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment