From 95b69d1e3b7ac6aaa50d8d1c479139fceda0c9a5 Mon Sep 17 00:00:00 2001 From: Halooooooo <673574728@qq.com> Date: Tue, 15 Aug 2023 21:36:33 +0800 Subject: [PATCH] =?UTF-8?q?redis=20=E9=9B=86=E7=BE=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修改dev.yaml等配置文件格式:去除redisReport 配置,修改redis 为集群配置方式。 2、修改redis 引用类型几初始化方式。 3、新增一套用于redis集群快速搭建的docker-compose及redis配置 --- configs/dev.yaml | 16 ++-- configs/prd.yaml | 16 ++-- configs/test.yaml | 16 ++-- internal/init.go | 2 +- internal/pkg/conf/conf.go | 93 ++++++++++--------- internal/pkg/dal/redis.go | 10 +- internal/pkg/dal/redis_for_report.go | 43 ++++----- internal/pkg/logic/plan/plan.go | 2 +- internal/pkg/logic/report/report.go | 2 +- .../redisCluster/7000/redis.conf | 14 +++ .../redisCluster/7001/redis.conf | 14 +++ .../redisCluster/7002/redis.conf | 14 +++ .../redisCluster/7003/redis.conf | 14 +++ .../redisCluster/7004/redis.conf | 14 +++ .../redisCluster/7005/redis.conf | 14 +++ .../redisCluster/docker-compose-linux.yml | 61 ++++++++++++ .../redisCluster/docker-compose-windows.yml | 73 +++++++++++++++ 17 files changed, 319 insertions(+), 99 deletions(-) create mode 100644 middlewareCompose/redisCluster/7000/redis.conf create mode 100644 middlewareCompose/redisCluster/7001/redis.conf create mode 100644 middlewareCompose/redisCluster/7002/redis.conf create mode 100644 middlewareCompose/redisCluster/7003/redis.conf create mode 100644 middlewareCompose/redisCluster/7004/redis.conf create mode 100644 middlewareCompose/redisCluster/7005/redis.conf create mode 100644 middlewareCompose/redisCluster/docker-compose-linux.yml create mode 100644 middlewareCompose/redisCluster/docker-compose-windows.yml diff --git a/configs/dev.yaml b/configs/dev.yaml index 72ca579..b90efd4 100644 --- a/configs/dev.yaml +++ b/configs/dev.yaml @@ -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: diff --git a/configs/prd.yaml b/configs/prd.yaml index 72ca579..b90efd4 100644 --- a/configs/prd.yaml +++ b/configs/prd.yaml @@ -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: diff --git a/configs/test.yaml b/configs/test.yaml index 72ca579..b90efd4 100644 --- a/configs/test.yaml +++ b/configs/test.yaml @@ -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: diff --git a/internal/init.go b/internal/init.go index 8f54285..ce929b2 100644 --- a/internal/init.go +++ b/internal/init.go @@ -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") diff --git a/internal/pkg/conf/conf.go b/internal/pkg/conf/conf.go index d297b09..05057ef 100644 --- a/internal/pkg/conf/conf.go +++ b/internal/pkg/conf/conf.go @@ -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"` @@ -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"` @@ -208,7 +208,7 @@ func MustInitConfByEnv() { initClients() initProof() initRedis() - initRedisReport() + //initRedisReport() initSMTP() initSms() initInviteData() @@ -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")) diff --git a/internal/pkg/dal/redis.go b/internal/pkg/dal/redis.go index e882eb8..a58aa40 100644 --- a/internal/pkg/dal/redis.go +++ b/internal/pkg/dal/redis.go @@ -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 } diff --git a/internal/pkg/dal/redis_for_report.go b/internal/pkg/dal/redis_for_report.go index 31ec98d..ed4b930 100644 --- a/internal/pkg/dal/redis_for_report.go +++ b/internal/pkg/dal/redis_for_report.go @@ -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 +//} diff --git a/internal/pkg/logic/plan/plan.go b/internal/pkg/logic/plan/plan.go index 5e6b9b8..df13963 100644 --- a/internal/pkg/logic/plan/plan.go +++ b/internal/pkg/logic/plan/plan.go @@ -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 { diff --git a/internal/pkg/logic/report/report.go b/internal/pkg/logic/report/report.go index 84fede7..e3f6d3f 100644 --- a/internal/pkg/logic/report/report.go +++ b/internal/pkg/logic/report/report.go @@ -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) diff --git a/middlewareCompose/redisCluster/7000/redis.conf b/middlewareCompose/redisCluster/7000/redis.conf new file mode 100644 index 0000000..2e0e57e --- /dev/null +++ b/middlewareCompose/redisCluster/7000/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/7001/redis.conf b/middlewareCompose/redisCluster/7001/redis.conf new file mode 100644 index 0000000..6eb1bb6 --- /dev/null +++ b/middlewareCompose/redisCluster/7001/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/7002/redis.conf b/middlewareCompose/redisCluster/7002/redis.conf new file mode 100644 index 0000000..2868782 --- /dev/null +++ b/middlewareCompose/redisCluster/7002/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/7003/redis.conf b/middlewareCompose/redisCluster/7003/redis.conf new file mode 100644 index 0000000..f137a0b --- /dev/null +++ b/middlewareCompose/redisCluster/7003/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/7004/redis.conf b/middlewareCompose/redisCluster/7004/redis.conf new file mode 100644 index 0000000..4c6d15c --- /dev/null +++ b/middlewareCompose/redisCluster/7004/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/7005/redis.conf b/middlewareCompose/redisCluster/7005/redis.conf new file mode 100644 index 0000000..2e2be61 --- /dev/null +++ b/middlewareCompose/redisCluster/7005/redis.conf @@ -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 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/docker-compose-linux.yml b/middlewareCompose/redisCluster/docker-compose-linux.yml new file mode 100644 index 0000000..9429d46 --- /dev/null +++ b/middlewareCompose/redisCluster/docker-compose-linux.yml @@ -0,0 +1,61 @@ +version: "3" + +services: + + redis1: + image: redis + volumes: + - /var/runnergo/redisCluster/7000/:/etc/redis/ + - /var/runnergo/redisCluster/7000/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + + redis2: + image: redis + volumes: + - /var/runnergo/redisCluster/7001/:/etc/redis/ + - /var/runnergo/redisCluster/7001/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + + redis3: + image: redis + volumes: + - /var/runnergo/redisCluster/7002/:/etc/redis/ + - /var/runnergo/redisCluster/7002/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + redis4: + image: redis + volumes: + - /var/runnergo/redisCluster/7003/:/etc/redis/ + - /var/runnergo/redisCluster/7003/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + redis5: + image: redis + volumes: + - /var/runnergo/redisCluster/7004/:/etc/redis/ + - /var/runnergo/redisCluster/7004/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + redis6: + image: redis + volumes: + - /var/runnergo/redisCluster/7005/:/etc/redis/ + - /var/runnergo/redisCluster/7005/data:/data + network_mode: host + command: redis-server /etc/redis/redis.conf + + + redis-cluster: + image: redis + command: redis-cli -a runnergo!666 --cluster create {节点ip}:7000 {节点ip}:7001 {节点ip}:7002 {节点ip}:7003 {节点ip}:7004 {节点ip}:7005 --cluster-replicas 1 --cluster-yes + network_mode: "host" + depends_on: + - redis1 + - redis2 + - redis3 + - redis4 + - redis5 + - redis6 \ No newline at end of file diff --git a/middlewareCompose/redisCluster/docker-compose-windows.yml b/middlewareCompose/redisCluster/docker-compose-windows.yml new file mode 100644 index 0000000..2dfd4cf --- /dev/null +++ b/middlewareCompose/redisCluster/docker-compose-windows.yml @@ -0,0 +1,73 @@ +version: "3" + +services: + + redis1: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7000\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7000\data:/data + ports: + - "7000:7000" + - "17000:17000" + command: redis-server /etc/redis/redis.conf + + redis2: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7001\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7001\data:/data + ports: + - "7001:7001" + - "17001:17001" + command: redis-server /etc/redis/redis.conf + + redis3: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7002\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7002\data:/data + ports: + - "7002:7002" + - "17002:17002" + command: redis-server /etc/redis/redis.conf + redis4: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7003\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7003\data:/data + ports: + - "7003:7003" + - "17003:17003" + command: redis-server /etc/redis/redis.conf + redis5: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7004\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7004\data:/data + ports: + - "7004:7004" + - "17004:17004" + command: redis-server /etc/redis/redis.conf + redis6: + image: redis + volumes: + - C:\dockerV\runnergo\rediscluster\7005\:/etc/redis/ + - C:\dockerV\runnergo\rediscluster\7005\data:/data + ports: + - "7005:7005" + - "17005:17005" + command: redis-server /etc/redis/redis.conf + + + redis-cluster: + image: redis + command: redis-cli -a runnergo!666 --cluster create {节点ip}:7000 {节点ip}:7001 {节点ip}:7002 {节点ip}:7003 {节点ip}:7004 {节点ip}:7005 --cluster-replicas 1 --cluster-yes + network_mode: "host" + depends_on: + - redis1 + - redis2 + - redis3 + - redis4 + - redis5 + - redis6 \ No newline at end of file