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: main
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
16 changes: 8 additions & 8 deletions configs/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ log:
ErrPath:

redis:
address:
password:
db:

redisReport:
address:
password:
db:
# 集群地址 使用;分割
clusterAddress : "{redis ip}:7000;{redis ip}:7001;{redis ip}:7002;{redis ip}:7003;{redis ip}:7004;{redis ip}:7005"
password: "runnergo!666"

#redisReport:
# address: ""
# password: ""
# db: 0

smtp:
host:
Expand Down
16 changes: 8 additions & 8 deletions configs/prd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ log:
ErrPath:

redis:
address:
password:
db:

redisReport:
address:
password:
db:
# 集群地址 使用;分割
clusterAddress : "{redis ip}:7000;{redis ip}:7001;{redis ip}:7002;{redis ip}:7003;{redis ip}:7004;{redis ip}:7005"
password: "runnergo!666"

#redisReport:
# address: ""
# password: ""
# db: 0

smtp:
host:
Expand Down
16 changes: 8 additions & 8 deletions configs/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ log:
ErrPath:

redis:
address:
password:
db:

redisReport:
address:
password:
db:
# 集群地址 使用;分割
clusterAddress : "{redis ip}:7000;{redis ip}:7001;{redis ip}:7002;{redis ip}:7003;{redis ip}:7004;{redis ip}:7005"
password: "runnergo!666"

#redisReport:
# address: ""
# password: ""
# db: 0

smtp:
host:
Expand Down
2 changes: 1 addition & 1 deletion internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func InitProjects(readConfMode int, configFile string) {
proof.MustInitProof()
//dal.MustInitGRPC()
dal.MustInitRedis()
dal.MustInitRedisForReport()
//dal.MustInitRedisForReport()
dal.MustInitBigCache()
// 初始化logger
zap.S().Debug("初始化logger")
Expand Down
93 changes: 47 additions & 46 deletions internal/pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
var Conf Config

type Config struct {
Base Base `yaml:"base"`
Http Http `yaml:"http"`
GRPC GRPC `yaml:"grpc"`
MySQL MySQL `yaml:"mysql"`
JWT JWT `yaml:"jwt"`
MongoDB MongoDB `yaml:"mongodb"`
Prometheus Prometheus `yaml:"prometheus"`
Kafka Kafka `yaml:"kafka"`
ES ES `yaml:"es"`
Clients Clients `yaml:"clients"`
Proof Proof `yaml:"proof"`
Redis Redis `yaml:"redis"`
RedisReport RedisReport `yaml:"redisReport"`
Base Base `yaml:"base"`
Http Http `yaml:"http"`
GRPC GRPC `yaml:"grpc"`
MySQL MySQL `yaml:"mysql"`
JWT JWT `yaml:"jwt"`
MongoDB MongoDB `yaml:"mongodb"`
Prometheus Prometheus `yaml:"prometheus"`
Kafka Kafka `yaml:"kafka"`
ES ES `yaml:"es"`
Clients Clients `yaml:"clients"`
Proof Proof `yaml:"proof"`
Redis Redis `yaml:"redis"`
//RedisReport RedisReport `yaml:"redisReport"`
SMTP SMTP `yaml:"smtp"`
Sms Sms `yaml:"sms"`
InviteData inviteData `yaml:"inviteData"`
Expand Down Expand Up @@ -137,16 +137,16 @@ type Proof struct {
}

type Redis struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
type RedisReport struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
DB int `yaml:"db"`
ClusterAddress string `yaml:"clusterAddress"`
Password string `yaml:"password"`
}

//type RedisReport struct {
// Address string `yaml:"address"`
// Password string `yaml:"password"`
// DB int `yaml:"db"`
//}

type SMTP struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Expand Down Expand Up @@ -208,7 +208,7 @@ func MustInitConfByEnv() {
initClients()
initProof()
initRedis()
initRedisReport()
//initRedisReport()
initSMTP()
initSms()
initInviteData()
Expand Down Expand Up @@ -325,33 +325,34 @@ func initProof() {
}
}
func initRedis() {
Conf.Redis.Address = os.Getenv("RG_REDIS_ADDRESS")
if Conf.Redis.Address == "" {
Conf.Redis.Address = "127.0.0.0:6379"
Conf.Redis.ClusterAddress = os.Getenv("RG_REDIS_ADDRESS")
if Conf.Redis.ClusterAddress == "" {
Conf.Redis.ClusterAddress = "127.0.0.0:6379"
}
Conf.Redis.Password = os.Getenv("RG_REDIS_PASSWORD")

redisDB, err := strconv.Atoi(os.Getenv("RG_REDIS_DB"))
if err != nil {
Conf.Redis.DB = 0
} else {
Conf.Redis.DB = redisDB
}
}
func initRedisReport() {
Conf.RedisReport.Address = os.Getenv("RG_REDIS_ADDRESS")
if Conf.RedisReport.Address == "" {
Conf.RedisReport.Address = "127.0.0.0:6379"
}
Conf.RedisReport.Password = os.Getenv("RG_REDIS_PASSWORD")

redisDB, err := strconv.Atoi(os.Getenv("RG_REDIS_DB"))
if err != nil {
Conf.RedisReport.DB = 0
} else {
Conf.RedisReport.DB = redisDB
}
}
//redisDB, err := strconv.Atoi(os.Getenv("RG_REDIS_DB"))
//if err != nil {
// Conf.Redis.DB = 0
//} else {
// Conf.Redis.DB = redisDB
//}
}

// func initRedisReport() {
// Conf.RedisReport.Address = os.Getenv("RG_REDIS_ADDRESS")
// if Conf.RedisReport.Address == "" {
// Conf.RedisReport.Address = "127.0.0.0:6379"
// }
// Conf.RedisReport.Password = os.Getenv("RG_REDIS_PASSWORD")
//
// redisDB, err := strconv.Atoi(os.Getenv("RG_REDIS_DB"))
// if err != nil {
// Conf.RedisReport.DB = 0
// } else {
// Conf.RedisReport.DB = redisDB
// }
// }
func initSMTP() {
Conf.SMTP.Host = os.Getenv("RG_SMTP_HOST")
port, err := strconv.Atoi(os.Getenv("RG_SMTP_PORT"))
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/dal/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package dal
import (
"fmt"
"github.com/go-redis/redis/v8"
"strings"

"github.com/Runner-Go-Team/RunnerGo-management-open/internal/pkg/conf"
)

var rdb *redis.Client
var rdb *redis.ClusterClient

func MustInitRedis() {
fmt.Println("redis initialized")
rdb = redis.NewClient(&redis.Options{
Addr: conf.Conf.Redis.Address,
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(conf.Conf.Redis.ClusterAddress, ";"),
Password: conf.Conf.Redis.Password,
DB: conf.Conf.Redis.DB,
})
}

func GetRDB() *redis.Client {
func GetRDB() *redis.ClusterClient {
return rdb
}
43 changes: 22 additions & 21 deletions internal/pkg/dal/redis_for_report.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package dal

import (
"fmt"
"github.com/go-redis/redis/v8"

"github.com/Runner-Go-Team/RunnerGo-management-open/internal/pkg/conf"
)

var rdbReport *redis.Client

func MustInitRedisForReport() {
fmt.Println("redis_report initialized")
rdbReport = redis.NewClient(&redis.Options{
Addr: conf.Conf.RedisReport.Address,
Password: conf.Conf.RedisReport.Password,
DB: conf.Conf.RedisReport.DB,
})
}

func GetRDBForReport() *redis.Client {
return rdbReport
}
//
//import (
// "fmt"
// "github.com/go-redis/redis/v8"
//
// "github.com/Runner-Go-Team/RunnerGo-management-open/internal/pkg/conf"
//)
//
//var rdbReport *redis.Client
//
//func MustInitRedisForReport() {
// fmt.Println("redis_report initialized")
// rdbReport = redis.NewClient(&redis.Options{
// Addr: conf.Conf.RedisReport.Address,
// Password: conf.Conf.RedisReport.Password,
// DB: conf.Conf.RedisReport.DB,
// })
//}
//
//func GetRDBForReport() *redis.Client {
// return rdbReport
//}
2 changes: 1 addition & 1 deletion internal/pkg/logic/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ func InsertReportData(ctx *gin.Context, req *rao.NotifyStopStressReq) error {
log.Logger.Info("NotifyStopStress--从MongoDB库查询报告详情结果为,err:", err, " ok:", ok)
if err != nil || !ok {
log.Logger.Info("NotifyStopStress--把redis数据写到mg库")
rdb := dal.GetRDBForReport()
rdb := dal.GetRDB()
key := fmt.Sprintf("reportData:%s", req.ReportID)
dataList := rdb.LRange(ctx, key, 0, -1).Val()
if len(dataList) < 1 {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/logic/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func GetReportDetail(ctx context.Context, req rao.GetReportReq) (ResultData, err
_, ok := dataMap["data"]
if err != nil || !ok {
log.Logger.Info("mango数据为空,开始查询redis")
rdb := dal.GetRDBForReport()
rdb := dal.GetRDB()
key := fmt.Sprintf("reportData:%s", req.ReportID)
dataList := rdb.LRange(ctx, key, 0, -1).Val()
log.Logger.Info("查询redis报告数据,报告数据的Key:", key, ",数组长度为:", len(dataList), dataList)
Expand Down
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7000/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7000
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7000.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7000
cluster-announce-bus-port 17000
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7001/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7001
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7001.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7001
cluster-announce-bus-port 17001
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7002/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7002
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7002.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7002
cluster-announce-bus-port 17002
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7003/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7003
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7003.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7003
cluster-announce-bus-port 17003
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7004/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7004
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7004.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7004
cluster-announce-bus-port 17004
14 changes: 14 additions & 0 deletions middlewareCompose/redisCluster/7005/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bind 0.0.0.0
port 7005
dir /data
requirepass runnergo!666
masterauth runnergo!666
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes7005.conf
cluster-node-timeout 15000
cluster-announce-ip {本机IP}
cluster-announce-port 7005
cluster-announce-bus-port 17005
Loading