侧边栏壁纸
博主头像
SeaDream乄造梦

Dream,Don't stop a day of hard and don't give up a little hope。 ——不停止一日努力&&不放弃一点希望。

  • 累计撰写 77 篇文章
  • 累计创建 21 个标签
  • 累计收到 14 条评论

目 录CONTENT

文章目录

redis 哨兵模式搭建与使用

SeaDream乄造梦
2024-12-31 / 0 评论 / 0 点赞 / 63 阅读 / 1,444 字
温馨提示:
亲爱的,如果觉得博主很有趣就留下你的足迹,并收藏下链接在走叭

哨兵模式

如果最开始选定的主节点挂掉,哨兵会自动进行选举,通过选举使得其他副节点成为主节点。

来看看GPT的官方解释
image.png
image.png
image.png

看成果
image.png

进入主节点查看副节点配置

image.png
image.png

进入哨兵查看主从状态

image.png

代码中引用

redis:
  host: 192.0.0.1
  port: 26379
  password: xxx

redis1:
  host: 192.0.0.1
  port: 26379
package internal

import (
	"fmt"
	"github.com/go-redis/redis"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"time"
)

var RedisClient *redis.Client

func InitRedis() {
	host := viper.GetString("redis.host")
	port := viper.GetString("redis.port")
	addr := fmt.Sprintf("%s:%s", host, port)
	addrFailover := fmt.Sprintf("%s:%s", host, "26379")

	password := viper.GetString("redis.password")

	env := viper.GetString("server.env")

	RedisClient = redis.NewClient(&redis.Options{
		Addr:         addr,
		Password:     password,
		DB:           0,  // 使用默认数据库
		PoolSize:     10, // 连接池大小
		DialTimeout:  time.Second * 5,
		ReadTimeout:  time.Second * 3,
		WriteTimeout: time.Second * 3,
		PoolTimeout:  4 * time.Second,
	})

	if env == "test" {
		RedisClient = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName: "mymaster",
			SentinelAddrs: []string{
				addrFailover,
			},
			Password:     password,
			DB:           0,
			DialTimeout:  time.Second * 5,
			ReadTimeout:  time.Second * 3,
			WriteTimeout: time.Second * 3,
			PoolTimeout:  4 * time.Second,
		})
	}

	_, err := RedisClient.Ping().Result()
	if err != nil {
		zap.S().Error("Redis.Ping err" + err.Error())
		fmt.Println("Redis.Ping err" + err.Error())
	} else {
		fmt.Println("Redis初始化完成。。。")
	}
}


参考教程地址:
https://blog.csdn.net/weixin_42599091/article/details/139289444?spm=1001.2014.3001.5501

0

评论区