diff --git a/common/config/config.go b/common/config/config.go index c7742866444683314262222b550e0a9b853dc11c..67baa4eeccbdbc35cf2ed614dcd527b9ee46917c 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/common/mysql/mysql.go b/common/mysql/mysql.go index 5c95a918a806e458ffe44d0da65234cd262389bd..05e3c0033de1d84d264b0b33ca7c868044153e2e 100644 --- a/common/mysql/mysql.go +++ b/common/mysql/mysql.go @@ -23,7 +23,7 @@ func init() { options := "?charset=utf8mb4&parseTime=True&loc=Local&time_zone=" + url.QueryEscape("'+8:00'") dsn := "" + mysqlConfigData.MYSQL_USERNAME + ":" + mysqlConfigData.MYSQL_PASSWORD + "@(" + mysqlConfigData.MYSQL_HOST + ")/" + mysqlConfigData.MYSQL_DB + options - sqlLogger := logger.Default.LogMode(logger.Info) + sqlLogger := logger.Default.LogMode(logger.Error) // 这个服务不需要 if file := mylogrus.GetSqlLog(); file != nil { //sqlLogger = logger.New(log.New(file, "\r\n", log.Ldate|log.Lmicroseconds), logger.Config{ sqlLogger = MyNew(log.New(file, "", log.Ldate|log.Lmicroseconds), logger.Config{ diff --git a/common/redisCli/redis.go b/common/redisCli/redis.go index 15e4ca9b391f8e583d92dda13da724ba69ec7ff7..93d576ea1eaff339885ea3ef83f5dcfd8f8bf784 100644 --- a/common/redisCli/redis.go +++ b/common/redisCli/redis.go @@ -8,52 +8,27 @@ import ( "log" ) -var RedisClient *redis.Client - -var RedisClient1 *redis.Client +var RedisCluster *redis.Client func init() { - RedisClient = redis.NewClient(&redis.Options{ - Addr: config.GetConfigRedis().REDIS_HOST, - Password: config.GetConfigRedis().REDIS_PASSWORD, // no password set - DB: 0, // use default DB - PoolSize: 2000, - MinIdleConns: 200, - }) - mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_HOST) - mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_PASSWORD) - pong, err := RedisClient.Ping(context.Background()).Result() - if err != nil { - mylogrus.MyLog.Warn(err) - mylogrus.MyLog.Fatal("redis db0 connect fail") - } else { - mylogrus.MyLog.Info("redis db0 connection success - ", pong) - } - - RedisClient1 = redis.NewClient(&redis.Options{ - Addr: config.GetConfigRedis().REDIS_HOST, - Password: config.GetConfigRedis().REDIS_PASSWORD, // no password set - DB: 1, // use default DB + RedisCluster = redis.NewClient(&redis.Options{ + Addr: config.GetConfigRedis().REDIS_CLUSTER_HOST, + Password: config.GetConfigRedis().REDIS_CLUSTER_PASSWORD, // no password set + DB: 0, // use default DB PoolSize: 200, MinIdleConns: 20, }) - mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_HOST) - mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_PASSWORD) - pong, err = RedisClient1.Ping(context.Background()).Result() + pong, err := RedisCluster.Ping(context.Background()).Result() if err != nil { mylogrus.MyLog.Warn(err) - mylogrus.MyLog.Fatal("redis db1 connect fail") + mylogrus.MyLog.Fatal("redis db0 connect fail") } else { - log.Println("redis db1 connection success - " + pong) + log.Println("redis db0 connection success - " + pong) } // log hook //RedisClient.AddHook(redisHook{}) } -func GetRedis() *redis.Client { - return RedisClient -} - -func GetRedis1() *redis.Client { - return RedisClient1 +func GetRedisCluster() *redis.Client { + return RedisCluster } diff --git a/common/redisCli/util.go b/common/redisCli/util.go deleted file mode 100644 index 362baa826a2a0684e09305aa61d3496297c12444..0000000000000000000000000000000000000000 --- a/common/redisCli/util.go +++ /dev/null @@ -1,56 +0,0 @@ -package redisCli - -import ( - "context" - "hilo-socketCenter/common/mylogrus" - "strconv" - "time" -) - -//这个用户避免多个服务器并发问题。 -func SetNX(key string, value interface{}, expiration time.Duration, callBack func()) { - flag, err := RedisClient.SetNX(context.Background(), key, value, expiration).Result() - if err != nil { - mylogrus.MyLog.Errorf("key:%v lock start setNx err: %v", key, err) - } - if !flag { - mylogrus.MyLog.Infof("key:%v lock setNx has lock", key) - return - } - mylogrus.MyLog.Infof("key:%v lock setNx begin", key) - callBack() - //执行结束之后,移除key - //RedisClient.Del(context.Background(), key) - mylogrus.MyLog.Infof("key:%v lock setNx end", key) -} - -//setNx没有,结束后,没有移除 -/*func SetNxNoDel(key string, value interface{}, expiration time.Duration, callBack func()) { - flag, err := RedisClient.SetNX(context.Background(), key, value, expiration).Result() - if err != nil { - mylogrus.MyLog.Errorf("key:%v lock start setNx err: %v", key, err) - } - if !flag { - mylogrus.MyLog.Infof("key:%v lock setNx has lock", key) - return - } - mylogrus.MyLog.Infof("key:%v lock setNx begin", key) - callBack() - mylogrus.MyLog.Infof("key:%v lock setNx end", key) -}*/ - -func ClearExpired(key string, expireSec int64) error { - return GetRedis().ZRemRangeByScore(context.Background(), key, - "0", strconv.FormatInt(time.Now().Unix()-expireSec, 10)).Err() -} - -func Lock(key string, expiration time.Duration) bool { - flag, err := RedisClient.SetNX(context.Background(), key, 1, expiration).Result() - if err != nil { - return false - } - if !flag { - return false - } - return true -} diff --git a/debug.ini b/debug.ini index 696fef9c6411a031fdf849389161d0c75fa3b4d4..fc7ab8fef6dbc5bac298ff1a56ed4406590e1207 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 65d129829059055cba01617aaed3c857c77bbbc0..f97ca1a49dcae3dc950cd582cc3521bb6710e6b1 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 6528dba02e6a86a439b20c5b7be76ffb2ec5587d..f7e73464d87b6c0b445241bc3eb145958f416381 100644 --- a/main.go +++ b/main.go @@ -112,7 +112,7 @@ func check() { for { select { case <-tick.C: - l, err := redisCli.GetRedis().LLen(context.Background(), SocketQueueSendGift).Result() + l, err := redisCli.GetRedisCluster().LLen(context.Background(), SocketQueueSendGift).Result() if err != nil { mylogrus.MyLog.Infof("cron sendGiftChan msg error,left %v-%v", l, err) } @@ -122,7 +122,7 @@ func check() { mylogrus.MyLog.Errorf("dingding msg fail:%v", sErr) } }() - n, err := redisCli.GetRedis().Del(context.Background(), SocketQueueSendGift).Result() + n, err := redisCli.GetRedisCluster().Del(context.Background(), SocketQueueSendGift).Result() mylogrus.MyLog.Infof("del sendGiftChan msg queue:%v,n:%v,err:%v", SocketQueueSendGift, n, err) } if l > 0 { @@ -136,7 +136,7 @@ func deal() { for true { //不需要加锁,注意,阻塞。 // 后进先出 - strs, err := redisCli.GetRedis().BRPop(context.Background(), time.Second, SocketQueueSendGift).Result() + strs, err := redisCli.GetRedisCluster().BRPop(context.Background(), time.Second, SocketQueueSendGift).Result() if err != nil { if err != redis.Nil { mylogrus.MyLog.Errorf("cron sendGiftChan redisCli.GetRedis().BLPop err:+%v", err) @@ -144,7 +144,7 @@ func deal() { } if len(strs) >= 2 { content := strs[1] - mylogrus.MyLog.Infof("cron sendGiftChan content:%v", content) + //mylogrus.MyLog.Infof("cron sendGiftChan content:%v", content) msg := new(SendGiftMsg) if err := json.Unmarshal([]byte(content), msg); err != nil { mylogrus.MyLog.Errorf("cron sendGiftChan Unmarshal err:%+v, content:%v", err, content) @@ -179,7 +179,7 @@ func SendToUserCenter(sendUserId uint64, msg *userProxy.GlobalGiftBanner) { if err != nil { mylogrus.MyLog.Errorf("grpc GlobalGiftBanner send fail") } else { - mylogrus.MyLog.Info("grpc GlobalGiftBanner send success") + //mylogrus.MyLog.Info("grpc GlobalGiftBanner send success") } } else { } @@ -197,7 +197,7 @@ func broadcast(msgType uint32, data []byte) ([]uint64, error) { mylogrus.MyLog.Errorf("broadcast message failed %s", err.Error()) } if rsp != nil { - mylogrus.MyLog.Infof("broadcast message res:%v", rsp) + //mylogrus.MyLog.Infof("broadcast message res:%v", rsp) return rsp.FailedUids, err } else { return []uint64{}, err diff --git a/release.ini b/release.ini index 5897408046dcb6cff6e8a098d0ef2ab8c0d6de4e..0b67f890443379f88fe319b7362a4b64fe8b9d53 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