Commit b35cfd91 authored by hujiebin's avatar hujiebin

feat:consul redis

parent 7aca0b0f
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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)
}
......
......@@ -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
......
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