• golang 简单操作 reids-sentinel 代码


    golang 简单操作 reids-sentinel 代码

    go run main.go -a "172.23.0.105:26379,172.23.0.106:26379,172.23.0.107:26379" -db "0" -m "mymaster" -p "123456" -get k1
    go run main.go -a "172.23.0.105:26379,172.23.0.106:26379,172.23.0.107:26379" -db "0" -m "mymaster" -p "123456" -set k1,v1
    go run main.go -a "172.23.0.105:26379,172.23.0.106:26379,172.23.0.107:26379" -db "0" -m "mymaster" -p "123456" -get k1
    go run main.go -a "172.23.0.105:26379,172.23.0.106:26379,172.23.0.107:26379" -db "0" -m "mymaster" -p "123456" -del k1

    `
    package main

    import (
    "flag"
    "fmt"
    "github.com/go-redis/redis"
    "os"
    "strings"
    "time"
    )

    var (
    //mastername = flag.String("m", "mymaster", "mymaster")
    mastername = flag.String("m", "", "mymaster")
    //password = flag.String("p", "123456", "password")
    password = flag.String("p", "", "password")
    //addr = flag.String("a", "172.23.0.105:26379,172.23.0.106:26379,172.23.0.107:26379", "addr:port")
    addr = flag.String("a", "", "addr:port")
    db = flag.Int("db", 0, "db")
    getK = flag.String("get", "", "key")
    setKV = flag.String("set", "", "key,value")
    delK = flag.String("del", "", "key")
    redisCli *redis.Client
    timeStr = time.Now().Format("2006-01-02 15:04:05")
    )

    type RedisSentinelInfo struct {
    MasterName string
    SentinelAddrs []string
    Password string
    DB int
    }

    func NewRedisSentinelInfo(mastername, password string, addr []string, db int) *RedisSentinelInfo {
    return &RedisSentinelInfo{
    MasterName: mastername,
    SentinelAddrs: addr,
    Password: password,
    DB: db,
    }
    }

    func (r *RedisSentinelInfo) initClient() (err error) {
    redisCli = redis.NewFailoverClient(&redis.FailoverOptions{
    MasterName: r.MasterName,
    SentinelAddrs: r.SentinelAddrs,
    Password: r.Password,
    DB: r.DB,
    })

    _, err = redisCli.Ping().Result()
    if err != nil {
    	return err
    }
    
    return nil
    

    }

    func (r *RedisSentinelInfo) get(k string) (err error) {
    v, err := redisCli.Get(k).Result()
    if err != nil {
    fmt.Printf("[%s][ERROR] get %s %sfailed, err: %v ", timeStr, k, v, err)
    return
    }
    fmt.Printf("[%s][SUCCESSFUL] get key: %s, value: %s ", timeStr, k, v)
    return nil
    }

    func (r *RedisSentinelInfo) set(kvs []string) (err error) {
    k := kvs[0]
    v := kvs[1]
    err = redisCli.Set(k, v, 0).Err()
    if err != nil {
    fmt.Printf("[%s][ERROR] set %s failed, err: %v ", timeStr, k, err)
    return
    }
    r.get(k)
    return nil
    }

    func (r *RedisSentinelInfo) del(k string) (err error) {
    n, err := redisCli.Del(k).Result()
    if err != nil {
    fmt.Printf("[%s][ERROR] del %s failed, err: %v ", timeStr, k, err)
    return
    }
    fmt.Printf("[%s][SUCCESSFUL] 成功删除 %v 个key: %s ", timeStr, n, k)
    return nil
    }

    func main() {
    flag.Parse()
    if len(os.Args) < 2 {
    flag.Usage()
    return
    }
    rc := NewRedisSentinelInfo(*mastername, password, strings.Split(addr, ","), *db)
    err := rc.initClient()
    if err != nil {
    fmt.Printf("[%s][ERROR] redis-sentinel connect failed, err: %s ", timeStr, err)
    return
    }

    for _, v := range os.Args {
    	switch v {
    	case "-get":
    		rc.get(*getK)
    	case "-set":
    		rc.set(strings.Split(*setKV, ","))
    	case "-del":
    		rc.del(*delK)
    	}
    }
    

    }

    `

  • 相关阅读:
    emWin 界面切换注意事项
    emWin 工程之汉字显示
    emWin 使用 GUIBuilder 放置标题 TEXT 注意
    【转】系统调用和驱动程序中相应函数的参数对应关系
    主机 & 虚拟机 & 开发板 相互通信
    电脑通过网口连接开发板
    【转】ARM交叉编译工具链
    【转】vi 写完文件保存时才发现是 readonly
    【转】ubuntu 12.04下如何开启 NFS 服务 & 设置
    安装完打开 eclipse 提示 JVM 版本较低
  • 原文地址:https://www.cnblogs.com/xiaoqshuo/p/15392891.html
Copyright © 2020-2023  润新知