From 248ed10c62c83f833aa3c8382b1f5e9eb5b2ec39 Mon Sep 17 00:00:00 2001 From: Blizzard Date: Sat, 26 Apr 2025 10:27:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9D=E5=A7=8B=E5=8C=96redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-dev.yaml | 12 ++++++++--- config-prod.yaml | 13 +++++++++--- config/config_redis.go | 9 ++++++--- global/global.go | 2 ++ go.mod | 3 +++ go.sum | 6 ++++++ initialize/redis.go | 45 ++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ 8 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 initialize/redis.go diff --git a/config-dev.yaml b/config-dev.yaml index 1e44fbe..a17b714 100644 --- a/config-dev.yaml +++ b/config-dev.yaml @@ -18,9 +18,15 @@ mysql: password: root redis: - host: 127.0.0.1 - password: sundynix - port: 6379 + addr: 127.0.0.1:6379 + clusteraddrs: + - 172.21.0.3:7000 + - 172.21.0.4:7001 + - 172.21.0.2:7002 + db: 1 + name: "" + password: "" + cluster: false zap: director: log diff --git a/config-prod.yaml b/config-prod.yaml index 6bdda07..2c4f0fc 100644 --- a/config-prod.yaml +++ b/config-prod.yaml @@ -16,10 +16,17 @@ mysql: singular: false user: root password: root + redis: - host: 192.168.100.127 - password: sundynix - port: 6379 + addr: 127.0.0.1:6379 + clusteraddrs: + - 172.21.0.3:7000 + - 172.21.0.4:7001 + - 172.21.0.2:7002 + db: 0 + name: "" + password: "sundynix" + cluster: false # zap日志配置 diff --git a/config/config_redis.go b/config/config_redis.go index 6649685..011cbb8 100644 --- a/config/config_redis.go +++ b/config/config_redis.go @@ -1,7 +1,10 @@ package config type Redis struct { - Host string `mapstructure:"host" json:"host" yaml:"host"` - Port int `mapstructure:"port" json:"port" yaml:"port"` - Password string `mapstructure:"password" json:"password" yaml:"password"` + Name string `mapstructure:"name" json:"name" yaml:"name"` // 代表当前实例的名字 + Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务器地址:端口 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 密码 + DB int `mapstructure:"db" json:"db" yaml:"db"` // 单实例模式下redis的哪个数据库 + Cluster bool `mapstructure:"cluster" json:"cluster" yaml:"cluster"` // 是否使用集群模式 + ClusterAddrs []string `mapstructure:"clusterAddrs" json:"clusterAddrs" yaml:"clusterAddrs"` // 集群模式下的节点地址列表 } diff --git a/global/global.go b/global/global.go index 1becbed..1770b0c 100644 --- a/global/global.go +++ b/global/global.go @@ -1,6 +1,7 @@ package global import ( + "github.com/redis/go-redis/v9" "github.com/spf13/viper" "go.uber.org/zap" "gorm.io/gorm" @@ -13,4 +14,5 @@ var ( Logger *zap.Logger Config *config.Config DB *gorm.DB + Redis redis.UniversalClient ) diff --git a/go.mod b/go.mod index 6e2c392..9943484 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,8 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/glebarez/go-sqlite v1.22.0 // indirect github.com/go-sql-driver/mysql v1.9.2 // indirect @@ -28,6 +30,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/redis/go-redis/v9 v9.7.3 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/sagikazarmark/locafero v0.7.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect diff --git a/go.sum b/go.sum index 00e70ad..4b9ba5e 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -48,6 +52,8 @@ github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNH github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM= +github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= diff --git a/initialize/redis.go b/initialize/redis.go new file mode 100644 index 0000000..8e18698 --- /dev/null +++ b/initialize/redis.go @@ -0,0 +1,45 @@ +package initialize + +import ( + "context" + "github.com/redis/go-redis/v9" + "go.uber.org/zap" + "sundynix-go/config" + "sundynix-go/global" +) + +// Redis +func Redis() { + client, err := initRedisClient(global.Config.Redis) + if err != nil { + global.Logger.Error("Redis connect failed,err:", zap.Error(err)) + return + } + global.Redis = client +} + +// 初始化Redis +func initRedisClient(redisConfig config.Redis) (redis.UniversalClient, error) { + var client redis.UniversalClient + //集群模式 + if redisConfig.Cluster { + client = redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: redisConfig.ClusterAddrs, + Password: redisConfig.Password, + }) + } else { + //单例模式 + client = redis.NewClient(&redis.Options{ + Addr: redisConfig.Addr, + Password: redisConfig.Password, + DB: redisConfig.DB, + }) + } + pong, err := client.Ping(context.Background()).Result() + if err != nil { + global.Logger.Error("Redis connect ping failed,err:", zap.String("name", redisConfig.Name), zap.Error(err)) + return nil, err + } + global.Logger.Info("Redis connect ping response:", zap.String("name", redisConfig.Name), zap.String("pong", pong)) + return client, nil +} diff --git a/main.go b/main.go index 4a1361f..3e94fc6 100644 --- a/main.go +++ b/main.go @@ -16,5 +16,7 @@ func main() { zap.ReplaceGlobals(global.Logger) //初始化gorm 连接数据库 global.DB = initialize.Gorm() + //redis连接 + initialize.Redis() }