Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis 集群支持 #4

Open
wants to merge 1 commit into
base: open
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")

}

Expand Down
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 连接失败")
Expand Down
28 changes: 8 additions & 20 deletions model/request_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 7 additions & 6 deletions open.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down