Golang Redis客户端连接状态

huangapple go评论85阅读模式
英文:

golang redis client connection status

问题

创建一个新的Redis客户端后,有没有办法检查连接的状态?

为了确保Sentinel处于健康状态,实例化后进行状态检查是理想的。

英文:

After creting a new Redis client, is there a way to check the status of the connection?

As a way to ensure that the Sentinel is in a healthy state, a status check after instantiation would be ideal.

答案1

得分: 2

一些客户端库提供了一个Ping()方法,用于执行Redis的PING命令来检查连接的状态:

redisClient := redis.NewClient(...)
if err := redisClient.Ping(ctx); err != nil {
    log.Fatal(err)
}
英文:

Some client libraries offer a Ping() method that executes Redis' PING command to check the status of the connection:

redisClient := redis.NewClient(...)
if err := redisClient.Ping(ctx); err != nil {
    log.Fatal(err)
}

答案2

得分: 0

我认为这取决于你使用的客户端。

例如,radix客户端(https://github.com/mediocregopher/radix)支持一个用于监控连接的函数(错误、重用、创建等)。

英文:

I think it depends on the client you use.

For example, the radix client (https://github.com/mediocregopher/radix) support a function to monitor for checking the connection (error, reused, created and so on.)

答案3

得分: 0

redisClient := redis.NewClient(...)
如果 err := redisClient.Ping(ctx).Err(); err != nil {
panic(err)
}

英文:
redisClient := redis.NewClient(...)
if err := redisClient.Ping(ctx).Err(); err != nil {
	panic(err)
}

答案4

得分: 0

如果你使用go-redis客户端连接到Redis,你可以尝试以下代码来检查Redis是否连接成功。

client := redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "",
    DB:       0,
})
if pong := client.Ping(ctx); pong.String() != "ping: PONG" {
    log.Println("-------------Error connection redis ----------:", pong)
}
英文:

If you use go-redis client for connecting to redis then you can try the following to check if redis is connected properly.

	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})
	if pong := client.Ping(ctx); pong.String() != "ping: PONG" {
		log.Println("-------------Error connection redis ----------:", pong)
	}

huangapple
  • 本文由 发表于 2022年8月6日 04:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73254949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定