diff --git a/config/config.go b/config/config.go index dde4211..40c67fd 100644 --- a/config/config.go +++ b/config/config.go @@ -11,15 +11,15 @@ import ( var Conf Config type Config struct { - Http Http `yaml:"http"` - Kafka Kafka `yaml:"kafka"` - ReportRedis ReportRedis `yaml:"reportRedis"` - Redis Redis `yaml:"redis"` - Mongo Mongo `yaml:"mongo"` - Heartbeat Heartbeat `yaml:"heartbeat"` - Machine Machine `yaml:"machine"` - Management Management `yaml:"management"` - Log Log `yaml:"log"` + Http Http `yaml:"http"` + Kafka Kafka `yaml:"kafka"` + //ReportRedis ReportRedis `yaml:"reportRedis"` + Redis Redis `yaml:"redis"` + Mongo Mongo `yaml:"mongo"` + Heartbeat Heartbeat `yaml:"heartbeat"` + Machine Machine `yaml:"machine"` + Management Management `yaml:"management"` + Log Log `yaml:"log"` } type Log struct { @@ -52,15 +52,17 @@ type Kafka struct { TopIc string `yaml:"topIc"` } -type ReportRedis struct { - Address string `yaml:"address"` - Password string `yaml:"password"` - DB int64 `yaml:"DB"` -} +//type ReportRedis struct { +// Address string `yaml:"address"` +// ClusterAddress string `yaml:"clusterAddress"` +// Password string `yaml:"password"` +// DB int64 `yaml:"DB"` +//} + type Redis struct { - Address string `yaml:"address"` - Password string `yaml:"password"` - DB int64 `yaml:"DB"` + ClusterAddress string `yaml:"clusterAddress"` + Password string `yaml:"password"` + DB int64 `yaml:"DB"` } type Mongo struct { @@ -260,25 +262,11 @@ func initMongo() { } func initRedis() { - var runnerGoRedis Redis - var reportRedis ReportRedis - address := os.Getenv("RG_REDIS_ADDRESS") - if address == "" { - address = RedisAddress + Conf.Redis.ClusterAddress = os.Getenv("RG_REDIS_ADDRESS") + if Conf.Redis.ClusterAddress == "" { + Conf.Redis.ClusterAddress = "127.0.0.0:6379" } - reportRedis.Address = address - runnerGoRedis.Address = reportRedis.Address - runnerGoRedis.Password = os.Getenv("RG_REDIS_PASSWORD") - reportRedis.Password = runnerGoRedis.Password - db, err := strconv.ParseInt(os.Getenv("RG_REDIS_DB"), 10, 64) - if err != nil { - db = 0 - } - reportRedis.DB = db - runnerGoRedis.DB = reportRedis.DB - - Conf.Redis = runnerGoRedis - Conf.ReportRedis = reportRedis + Conf.Redis.Password = os.Getenv("RG_REDIS_PASSWORD") } diff --git a/main.go b/main.go index 865f091..eaaee2b 100644 --- a/main.go +++ b/main.go @@ -48,12 +48,8 @@ func initService() { // 初始化redis客户端 log.Logger.Info(fmt.Sprintf("机器ip:%s,初始化redis客户端", middlewares.LocalIp)) if err := model.InitRedisClient( - config.Conf.ReportRedis.Address, - config.Conf.ReportRedis.Password, - config.Conf.ReportRedis.DB, - config.Conf.Redis.Address, + config.Conf.Redis.ClusterAddress, config.Conf.Redis.Password, - config.Conf.Redis.DB, ); err != nil { log.Logger.Error(fmt.Sprintf("机器ip:%s, redis连接失败:", middlewares.LocalIp), err) panic("redis 连接失败") diff --git a/model/request_redis.go b/model/request_redis.go index 1781669..067357f 100644 --- a/model/request_redis.go +++ b/model/request_redis.go @@ -2,36 +2,24 @@ package model import ( "github.com/go-redis/redis" + "strings" "time" ) var ( - ReportRdb *redis.Client - RDB *redis.Client + RDB *redis.ClusterClient timeDuration = 3 * time.Second ) type RedisClient struct { - Client *redis.Client + Client *redis.ClusterClient } -func InitRedisClient(reportAddr, reportPassword string, reportDb int64, addr, password string, db int64) (err error) { - ReportRdb = redis.NewClient( - &redis.Options{ - Addr: reportAddr, - Password: reportPassword, - DB: int(reportDb), - }) - _, err = ReportRdb.Ping().Result() - if err != nil { - return err - } - - RDB = redis.NewClient( - &redis.Options{ - Addr: addr, +func InitRedisClient(clusterAddr, password string) (err error) { + RDB = redis.NewClusterClient( + &redis.ClusterOptions{ + Addrs: strings.Split(clusterAddr, ";"), Password: password, - DB: int(db), }) _, err = RDB.Ping().Result() return err @@ -59,7 +47,7 @@ func QuerySceneStatus(key string) (err error, value string) { } func QueryReportData(key string) (value string) { - values := ReportRdb.LRange(key, 0, -1).Val() + values := RDB.LRange(key, 0, -1).Val() if len(values) <= 0 { return } diff --git a/open.yaml b/open.yaml index c5d7cd5..183c6a5 100644 --- a/open.yaml +++ b/open.yaml @@ -12,14 +12,15 @@ http: port: 8002 #本服务端口 redis: - address: "" + # 集群地址 使用;分割 exp 127.0.0.1:7000;127.0.0.1:7001;127.0.0.1:7002;127.0.0.1:7003;127.0.0.1:7004;127.0.0.1:7005 + clusterAddress : "" password: "" - db: 1 -reportRedis: - address: "" - password: "" - db: 1 +#reportRedis: +# address: "" +# clusterAddress: "" +# password: "" +# db: 0 kafka: address: ""