Skip to content

Redis 连接异常 #2562

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

Open
NewBee6 opened this issue Mar 24, 2025 · 2 comments
Open

Redis 连接异常 #2562

NewBee6 opened this issue Mar 24, 2025 · 2 comments
Labels

Comments

@NewBee6
Copy link

NewBee6 commented Mar 24, 2025

Question and Steps to reproduce

报错位置:center.go文件
具体代码:
var redis storage.Redis
redis, err = storage.NewRedis(config.Redis)
if err != nil {
return nil, err
}
获取的到redis是:unreadable: invalid interface type

Relevant logs and configurations

配置文件:etc/config.toml    中redis配置如下:
[Redis]
# address, ip:port or ip1:port,ip2:port for cluster and sentinel(SentinelAddrs)
Address = "127.0.0.1:6379"
# Username = ""
Password = "1234@QWE"
# DB = 0
# UseTLS = false
# TLSMinVersion = "1.2"
# standalone cluster sentinel
RedisType = "standalone"

Version

7.7.2

@710leo
Copy link
Member

710leo commented Mar 26, 2025

@NewBee6 新版本没复现你的问题。

  1. 可以确认下你的 redis 是否可以正常使用
  2. 可以使用最新的版本

@NewBee6
Copy link
Author

NewBee6 commented Mar 26, 2025

1.我的redis是正常的。用的是:github.com/redis/go-redis/v9@v9.0.2
2.代码的具体的位置是在:redis.go文件中:
下面是具体的代码:

`type Redis redis.Cmdable

func NewRedis(cfg RedisConfig) (Redis, error) {
var redisClient Redis
switch cfg.RedisType {
case "standalone", "":
redisOptions := &redis.Options{
Addr: cfg.Address,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
}

	if cfg.UseTLS {
		tlsConfig, err := cfg.TLSConfig()
		if err != nil {
			fmt.Println("failed to init redis tls config:", err)
			os.Exit(1)
		}
		redisOptions.TLSConfig = tlsConfig
	}

	redisClient2 := redis.NewClient(redisOptions)
	redisClient = redisClient2

case "cluster":
	redisOptions := &redis.ClusterOptions{
		Addrs:    strings.Split(cfg.Address, ","),
		Username: cfg.Username,
		Password: cfg.Password,
	}

	if cfg.UseTLS {
		tlsConfig, err := cfg.TLSConfig()
		if err != nil {
			fmt.Println("failed to init redis tls config:", err)
			os.Exit(1)
		}
		redisOptions.TLSConfig = tlsConfig
	}

	redisClient = redis.NewClusterClient(redisOptions)

case "sentinel":
	redisOptions := &redis.FailoverOptions{
		MasterName:       cfg.MasterName,
		SentinelAddrs:    strings.Split(cfg.Address, ","),
		Username:         cfg.Username,
		Password:         cfg.Password,
		DB:               cfg.DB,
		SentinelUsername: cfg.SentinelUsername,
		SentinelPassword: cfg.SentinelPassword,
	}

	if cfg.UseTLS {
		tlsConfig, err := cfg.TLSConfig()
		if err != nil {
			fmt.Println("failed to init redis tls config:", err)
			os.Exit(1)
		}
		redisOptions.TLSConfig = tlsConfig
	}

	redisClient = redis.NewFailoverClient(redisOptions)

default:
	fmt.Println("failed to init redis , redis type is illegal:", cfg.RedisType)
	os.Exit(1)
}

err := redisClient.Ping(context.Background()).Err()
if err != nil {
	fmt.Println("failed to ping redis:", err)
	os.Exit(1)
}
return redisClient, nil

}`

发现: redisClient2 := redis.NewClient(redisOptions)
redisClient2 是有值的。 但是复制给redisClient = redisClient2 时,出现那个错误

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants