1
0
mirror of synced 2026-04-15 12:58:40 +08:00

增加redis哨兵和集群模式的支持

This commit is contained in:
lifushen
2022-07-25 14:22:16 +08:00
parent a434e07c5d
commit 0b5f901097
2 changed files with 102 additions and 36 deletions

View File

@@ -5,11 +5,12 @@ import (
"fmt"
"github.com/go-redis/redis/v8"
"strconv"
"strings"
"sync"
"time"
)
var _client *redis.Client
var _client redis.UniversalClient
var _ctx = context.Background()
var _workerIdLock sync.Mutex
@@ -25,11 +26,22 @@ var _MaxWorkerId int32 = 0 // 最大WorkerId值超过此值从0开
var _RedisConnString = ""
var _RedisPassword = ""
var _RedisDB = 0
var _RedisMasterName = ""
const _WorkerIdIndexKey string = "IdGen:WorkerId:Index" // redis 中的key
const _WorkerIdValueKeyPrefix string = "IdGen:WorkerId:Value:" // redis 中的key
const _WorkerIdFlag = "Y" // IdGen:WorkerId:Value:xx 的值(将来可用 _token 替代)
const _Log = false // 是否输出日志
const _Log = false // 是否输出日志
type RegisterConf struct {
Address string // 注意:哨兵模式下,这里传入的是 Sentinel 节点,不是 Redis 节点
Password string
DB int
MasterName string // 注意:哨兵模式下,这里必须传入 Sentinel 服务名称
MaxWorkerId int32
TotalCount int32 // 注意:仅对 RegisterMany 生效
}
func Validate(workerId int32) int32 {
for _, value := range _workerIdList {
@@ -68,20 +80,24 @@ func autoUnRegister() {
}
}
func RegisterMany(ip string, port int32, password string, maxWorkerId int32, totalCount int32) []int32 {
if maxWorkerId < 0 {
func RegisterMany(conf RegisterConf) []int32 {
if conf.MaxWorkerId < 0 {
return []int32{-2}
}
if totalCount < 1 {
if conf.TotalCount < 1 {
return []int32{-1}
} else if conf.TotalCount == 0 {
conf.TotalCount = 1
}
autoUnRegister()
_MaxWorkerId = maxWorkerId
_RedisConnString = ip + ":" + strconv.Itoa(int(port))
_RedisPassword = password
_MaxWorkerId = conf.MaxWorkerId
_RedisConnString = conf.Address
_RedisPassword = conf.Password
_RedisDB = conf.DB
_RedisMasterName = conf.MasterName
_client = newRedisClient()
if _client == nil {
return []int32{-1}
@@ -102,7 +118,7 @@ func RegisterMany(ip string, port int32, password string, maxWorkerId int32, tot
//}
_lifeIndex++
_workerIdList = make([]int32, totalCount)
_workerIdList = make([]int32, conf.TotalCount)
for key := range _workerIdList {
_workerIdList[key] = -1 // 全部初始化-1
}
@@ -125,16 +141,18 @@ func RegisterMany(ip string, port int32, password string, maxWorkerId int32, tot
return _workerIdList
}
func RegisterOne(ip string, port int32, password string, maxWorkerId int32) int32 {
if maxWorkerId < 0 {
func RegisterOne(conf RegisterConf) int32 {
if conf.MaxWorkerId < 0 {
return -2
}
autoUnRegister()
_MaxWorkerId = maxWorkerId
_RedisConnString = ip + ":" + strconv.Itoa(int(port))
_RedisPassword = password
_MaxWorkerId = conf.MaxWorkerId
_RedisConnString = conf.Address
_RedisPassword = conf.Password
_RedisDB = conf.DB
_RedisMasterName = conf.MasterName
_loopCount = 0
_client = newRedisClient()
if _client == nil {
@@ -170,16 +188,18 @@ func register(lifeTime int) int32 {
return getNextWorkerId(lifeTime)
}
func newRedisClient() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: _RedisConnString,
Password: _RedisPassword,
DB: 0,
func newRedisClient() redis.UniversalClient {
client := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: strings.Split(_RedisConnString, ","),
Password: _RedisPassword,
DB: _RedisDB,
MasterName: _RedisMasterName,
//PoolSize: 1000,
//ReadTimeout: time.Millisecond * time.Duration(100),
//WriteTimeout: time.Millisecond * time.Duration(100),
//IdleTimeout: time.Second * time.Duration(60),
})
return client
}
func getNextWorkerId(lifeTime int) int32 {