• golang常用库之-操作redis go-redis/redis库、 go-redsync/redsync库(redis分布式锁)


    golang常用库之-操作redis go-redis/redis库、 go-redsync/redsync库(redis分布式锁

    一、什么是go-redis/redis库

    官网文档:https://redis.uptrace.dev/guide/#ecosystem
    github: https://github.com/go-redis/redis

    goredislib “github.com/go-redis/redis/v8”

    go-redis中连接池以及相关参数

    redis连接池 go-redis
    参考URL: https://www.cnblogs.com/CJ-cooper/p/15149273.html
    go-redis 源码分析:连接池
    参考URL: http://t.zoukankan.com/ExMan-p-12396854.html

    go-redis模块自带连接池,所有参数都是可选的,参数配置说明:
    Options参数详解

    type Options struct {
        // 网络类型 tcp 或者 unix.
        // 默认是 tcp.
        Network string
        // redis地址,格式 host:port
        Addr string
    
        // 新建一个redis连接的时候,会回调这个函数
        OnConnect func(*Conn) error
    
        // redis密码,redis server没有设置可以为空。
        Password string
        
        // redis数据库,序号从0开始,默认是0,可以不用设置
        DB int
    
        // redis操作失败最大重试次数,默认不重试。
        MaxRetries int
        
        // 最小重试时间间隔.
        // 默认是 8ms ; -1 表示关闭.
        MinRetryBackoff time.Duration
        
        // 最大重试时间间隔
        // 默认是 512ms; -1 表示关闭.
        MaxRetryBackoff time.Duration
    
        // redis连接超时时间.
        // 默认是 5 秒.
        DialTimeout time.Duration
        
        // socket读取超时时间
        // 默认 3 秒.
        ReadTimeout time.Duration
        
        // socket写超时时间
        WriteTimeout time.Duration
    
        // redis连接池的最大连接数.
        // 默认连接池大小等于 cpu个数 * 10
        PoolSize int
        
        // redis连接池最小空闲连接数.
        MinIdleConns int
        // redis连接最大的存活时间,默认不会关闭过时的连接.
        MaxConnAge time.Duration
        
        // 当你从redis连接池获取一个连接之后,连接池最多等待这个拿出去的连接多长时间。
        // 默认是等待 ReadTimeout + 1 秒.
        PoolTimeout time.Duration
        // redis连接池多久会关闭一个空闲连接.
        // 默认是 5 分钟. -1 则表示关闭这个配置项
        IdleTimeout time.Duration
        // 多长时间检测一下,空闲连接
        // 默认是 1 分钟. -1 表示关闭空闲连接检测
        IdleCheckFrequency time.Duration
    
        // 只读设置,如果设置为true, redis只能查询缓存不能更新。
        readOnly bool
    }
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • MaxIdle: 最大空闲连接数;
      没有redis操作时依然可以保持这个连接数量,但要在IdleTimeout的时间范围内,不然就会关闭,结合IdleTimeout进行理解;

    • IdleTimeout: 5 * 60 * time.Second,
      表示空闲连接保活时间,超过该时间后,连接会自动关闭,其实在默认情况下,当程序空闲时,redis连接池中是没有连接的;详情参见github

    • MaxActive:最大连接数,一般为0,代表不限制;
      同一时间最多有这么多的连接,包括连接池中的连接以及连接池外的连接,假设最大空闲MaxIdle设置3,MaxActive设置4,那么,当连接池的连接全部处于忙碌状态时,如果这时候程序过来取连接,发现连接池没有取到,这时候就还可以再额外创建一个连接,当该连接关闭时,如果连接池中空闲数量小于3,则该连接会被放到连接池中,如果空闲数等于3,则该连接会直接关闭;

    go-redis包自带了连接池,会自动维护redis连接,因此创建一次client即可,不要查询一次redis就关闭client。

    网上代码demo:

    package main
     
    import (
    	"fmt"
    	"github.com/go-redis/redis"
    	"net/http"
    	"net"
    	"time"
    )
     
    var gClient *redis.Client
     
    func handler(w http.ResponseWriter, r *http.Request) {
    	gClient.Ping().Result()
    	printRedisPool(gClient.PoolStats())
    	fmt.Fprintf(w, "Hello")
    }
     
    func printRedisPool(stats *redis.PoolStats) {
    	fmt.Printf("Hits=%d Misses=%d Timeouts=%d TotalConns=%d IdleConns=%d StaleConns=%d\n",
    		stats.Hits, stats.Misses, stats.Timeouts, stats.TotalConns, stats.IdleConns, stats.StaleConns)
    }
     
    func printRedisOption(opt *redis.Options) {
    	fmt.Printf("Network=%v\n", opt.Network)
    	fmt.Printf("Addr=%v\n", opt.Addr)
    	fmt.Printf("Password=%v\n", opt.Password)
    	fmt.Printf("DB=%v\n", opt.DB)
    	fmt.Printf("MaxRetries=%v\n", opt.MaxRetries)
    	fmt.Printf("MinRetryBackoff=%v\n", opt.MinRetryBackoff)
    	fmt.Printf("MaxRetryBackoff=%v\n", opt.MaxRetryBackoff)
    	fmt.Printf("DialTimeout=%v\n", opt.DialTimeout)
    	fmt.Printf("ReadTimeout=%v\n", opt.ReadTimeout)
    	fmt.Printf("WriteTimeout=%v\n", opt.WriteTimeout)
    	fmt.Printf("PoolSize=%v\n", opt.PoolSize)
    	fmt.Printf("MinIdleConns=%v\n", opt.MinIdleConns)
    	fmt.Printf("MaxConnAge=%v\n", opt.MaxConnAge)
    	fmt.Printf("PoolTimeout=%v\n", opt.PoolTimeout)
    	fmt.Printf("IdleTimeout=%v\n", opt.IdleTimeout)
    	fmt.Printf("IdleCheckFrequency=%v\n", opt.IdleCheckFrequency)
    	fmt.Printf("TLSConfig=%v\n", opt.TLSConfig)
     
    }
     
    func main() {
    	gClient = redis.NewClient(&redis.Options{
    		//连接信息
    		Network:  "tcp",                  //网络类型,tcp or unix,默认tcp
    		Addr:     "127.0.0.1:6379", //主机名+冒号+端口,默认localhost:6379
    		Password: "",                     //密码
    		DB:       0,                      // redis数据库index
     
    		//连接池容量及闲置连接数量
    		PoolSize:     15, // 连接池最大socket连接数,默认为4倍CPU数, 4 * runtime.NumCPU
    		MinIdleConns: 10, //在启动阶段创建指定数量的Idle连接,并长期维持idle状态的连接数不少于指定数量;。
     
    		//超时
    		DialTimeout:  5 * time.Second, //连接建立超时时间,默认5秒。
    		ReadTimeout:  3 * time.Second, //读超时,默认3秒, -1表示取消读超时
    		WriteTimeout: 3 * time.Second, //写超时,默认等于读超时
    		PoolTimeout:  4 * time.Second, //当所有连接都处在繁忙状态时,客户端等待可用连接的最大等待时长,默认为读超时+1秒。
     
    		//闲置连接检查包括IdleTimeout,MaxConnAge
    		IdleCheckFrequency: 60 * time.Second, //闲置连接检查的周期,默认为1分钟,-1表示不做周期性检查,只在客户端获取连接时对闲置连接进行处理。
    		IdleTimeout:        5 * time.Minute,  //闲置超时,默认5分钟,-1表示取消闲置超时检查
    		MaxConnAge:         0 * time.Second,  //连接存活时长,从创建开始计时,超过指定时长则关闭连接,默认为0,即不关闭存活时长较长的连接
     
    		//命令执行失败时的重试策略
    		MaxRetries:      0,                      // 命令执行失败时,最多重试多少次,默认为0即不重试
    		MinRetryBackoff: 8 * time.Millisecond,   //每次计算重试间隔时间的下限,默认8毫秒,-1表示取消间隔
    		MaxRetryBackoff: 512 * time.Millisecond, //每次计算重试间隔时间的上限,默认512毫秒,-1表示取消间隔
     
    		//可自定义连接函数
    		Dialer: func() (net.Conn, error) {
    			netDialer := &net.Dialer{
    				Timeout:   5 * time.Second,
    				KeepAlive: 5 * time.Minute,
    			}
    			return netDialer.Dial("tcp", "127.0.0.1:6379")
    		},
     
    		//钩子函数
    		OnConnect: func(conn *redis.Conn) error { //仅当客户端执行命令时需要从连接池获取连接时,如果连接池需要新建连接时则会调用此钩子函数
    			fmt.Printf("conn=%v\n", conn)
    			return nil
    		},
     
    	})
    	defer gClient.Close()
     
    	printRedisOption(gClient.Options())
    	printRedisPool(gClient.PoolStats())
     
    	http.HandleFunc("/", handler)
     
    	http.ListenAndServe(":8080", nil)
    }
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    代码demo

    官网:https://redis.uptrace.dev/

    官方demo
    package main
    
    import (
    	"context"
    	"github.com/go-redis/redis/v8"
    )
    
    func main() {
    	ctx := context.Background()
    
    	rdb := redis.NewClient(&redis.Options{
    		Addr:	  "localhost:6379",
    		Password: "", // no password set
    		DB:		  0,  // use default DB
    	})
    
    	err := rdb.Set(ctx, "key", "value", 0).Err()
    	if err != nil {
    		panic(err)
    	}
    
    	val, err := rdb.Get(ctx, "key").Result()
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println("key", val)
    
    	val2, err := rdb.Get(ctx, "key2").Result()
    	if err == redis.Nil {
    		fmt.Println("key2 does not exist")
    	} else if err != nil {
    		panic(err)
    	} else {
    		fmt.Println("key2", val2)
    	}
    	// Output: key value
    	// key2 does not exist
    }
    
    • 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

    二、什么是go-redsync/redsync库

    Distributed locks with Redis:Redis 官方出品,用 Redis 实现分布式互斥锁。Go 的实现 github.com/go-redsync/redsync

    redsync是redis官方推荐的go版本分布式锁实现,标准的官方redlock算法实现。

    rs := redsync.New(goredis.NewPool(client))
    mutex:= rs.NewMutex("xxx", redsync.WithExpiry(10*time.Minute)),
    
    • 1
    • 2

    go-redis/redis操作redis结合redsync库的redis分布式锁

    使用思路:

    1. 我们用go-redis/redis库 NewClient方法,创建redis连接实例client
      引入 goredislib “github.com/go-redis/redis/v8”

    client := goredislib.NewClient(&goredislib.Options{
    Addr: fmt.Sprintf(“%s:%d”, cfg.RedisOptions.Host, cfg.RedisOptions.Port),
    Username: cfg.RedisOptions.Username,
    Password: cfg.RedisOptions.Password,
    })

    1. 然后调用 “github.com/go-redsync/redsync/v4” 库的goredis.NewPool(client),把它用这个库的pool结构体封装起来。
      再然后用"github.com/go-redsync/redsync/v4" 库,的edsync.New方法从给定的Redis连接池创建并返回一个新的Redsync实例。
    rs := redsync.New(goredis.NewPool(client))
    
    • 1
    1. 接下来调用 “github.com/go-redsync/redsync/v4” 库的 rs.NewMutex方法 ,NewMutex返回具有给定名称的新分布式互斥体。
    mutex:= rs.NewMutex("my-mutex", redsync.WithExpiry(10*time.Minute))
    
    • 1
    1. 然后在使用redis之前,先调这个mutex实例Lock()方法的加锁。
    if err := s.mutex.Lock(); err != nil {
    	log.Info("there is already an xxx instance running.")
    
    	return
    }
    defer func() {
    	if _, err := s.mutex.Unlock(); err != nil {
    		log.Errorf("could not release xxx lock. err: %v", err)
    	}
    }()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    代码demo

    官方示例demo:redis分布式锁

    github:https://github.com/go-redsync/redsync

    package main
    
    import (
    	goredislib "github.com/go-redis/redis/v8"
    	"github.com/go-redsync/redsync/v4"
    	"github.com/go-redsync/redsync/v4/redis/goredis/v8"
    )
    
    func main() {
    	// Create a pool with go-redis (or redigo) which is the pool redisync will
    	// use while communicating with Redis. This can also be any pool that
    	// implements the `redis.Pool` interface.
    	client := goredislib.NewClient(&goredislib.Options{
    		Addr: "localhost:6379",
    	})
    	pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)
    
    	// Create an instance of redisync to be used to obtain a mutual exclusion
    	// lock.
    	rs := redsync.New(pool)
    
    	// Obtain a new mutex by using the same name for all instances wanting the
    	// same lock.
    	mutexname := "my-global-mutex"
    	mutex := rs.NewMutex(mutexname)
    
    	// Obtain a lock for our given mutex. After this is successful, no one else
    	// can obtain the same lock (the same mutex name) until we unlock it.
    	if err := mutex.Lock(); err != nil {
    		panic(err)
    	}
    
    	// Do your work that requires the lock.
    
    	// Release the lock so other processes or threads can obtain a lock.
    	if ok, err := mutex.Unlock(); !ok || err != nil {
    		panic("unlock failed")
    	}
    }
    
    • 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

    代码解释:

    //用go-redis(或redigo)创建一个池,redisync将在与Redis通信时使用。也可以是任何实现redis.pool接口的池。

    client := goredislib.NewClient(&goredislib.Options{
    	Addr: "localhost:6379",
    })
    pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)
    
    // Create an instance of redisync to be used to obtain a mutual exclusion
    // lock.
    rs := redsync.New(pool)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    //创建一个用于获得相互排除的redisync的实例锁

    mutexname := "my-global-mutex"
    mutex := rs.NewMutex(mutexname)
    
    • 1
    • 2

    为我们给定的mutex获得一个锁。成功之后,没有其他人可以获取相同的锁(相同的mutex名称),直到我们解锁为止。

    if err := mutex.Lock(); err != nil {
    	panic(err)
    }
    
    • 1
    • 2
    • 3
  • 相关阅读:
    创建型之单例模式
    Visual Studio2019报错
    第10章_瑞萨MCU零基础入门系列教程之中断控制单元简介
    LabVIEW通过网络传输数据
    C++ 基础与深度分析 Chapter11 类与面向对象编程(析构与复制成员函数、字面值类、成员指针与bind交互)
    工作9年,1次转行,4次跳槽:拒绝被「稳定性」绑架
    【MySQL】MySQL 慢SQL如何避险
    MIT课程分布式系统学习02——RPC and threads
    HTML、CSS常用的vscode插件 +Css reset 和Normalize.css
    Django实现音乐网站 ⒄
  • 原文地址:https://blog.csdn.net/inthat/article/details/122525921