-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_go-redis.go
43 lines (37 loc) · 1.34 KB
/
example_go-redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"github.com/btnguyen2k/prom"
)
/*
Setup a Redis Sentinel using docker:
1$ docker network create app-tier --driver bridge
2$ docker run -d --name redis-server -e ALLOW_EMPTY_PASSWORD=yes --network app-tier -p 6379:6379 bitnami/redis:latest
3$ docker run -it --rm --name redis-sentinel -e REDIS_MASTER_HOST=redis-server --network app-tier -p 26379:26379 bitnami/redis-sentinel:latest
*/
func main() {
hostsAndPorts := "localhost:6379;localhost:26379" // first server is normal Redis server, second one is sentinel server
password := ""
maxRetries := 3
goRedisConnect := prom.NewGoRedisConnect(hostsAndPorts, password, maxRetries)
db := 0
client := goRedisConnect.GetClient(db)
{
fmt.Println("Redis client:", client)
result, err := client.FlushAll().Result()
fmt.Println("FlushAll:", result, err)
result, err = client.Ping().Result()
fmt.Println("Ping :", result, err)
fmt.Println("==============================")
}
goRedisConnect.SetSentinelMasterName("mymaster")
failoverClient := goRedisConnect.GetFailoverClient(db)
{
fmt.Println("Sentinel Redis client:", failoverClient)
result, err := failoverClient.FlushAll().Result()
fmt.Println("FlushAll:", result, err)
result, err = failoverClient.Ping().Result()
fmt.Println("Ping :", result, err)
fmt.Println("==============================")
}
}