Go语言之 go-redis 基本使用

Redis 介绍

Redishttps://redis.io/

Redis 中文网https://www.redis.net.cn/

REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

macOS 安装Redis

brew install redis

Windows 安装 Redis

下载地址https://github.com/dmajkic/redis/tags/

https://github.com/ServiceStack/redis-windows

https://github.com/microsoftarchive/redis/releases

go-redis 使用

安装 go-redis 库 https://github.com/redis/go-redis

go get github.com/redis/go-redis/v9

Go-Redis 中文文档https://redis.uptrace.dev/zh/

安装

go-redis 支持 2 个最新的 go 版本且依赖Go modules,如果 你还没有 go mod,你需要首先初始化:

go mod init github.com/my/repo

安装 go-redis/v9 (支持所有的 redis 版本):

go get github.com/redis/go-redis/v9

#连接到 Redis 服务器

连接到 Redis 服务器示例,更多配置参数,请参照 redis.Options:

import "github.com/redis/go-redis/v9"

rdb := redis.NewClient(&redis.Options{
	Addr:	  "localhost:6379",
	Password: "", // 没有密码,默认值
	DB:		  0,  // 默认DB 0
})

同时也支持另外一种常见的连接字符串:

opt, err := redis.ParseURL("redis://<user>:<pass>@localhost:6379/<db>")
if err != nil {
	panic(err)
}

rdb := redis.NewClient(opt)

#使用 TLS

你需要手动设置 tls.Config,你可以在 这里 了解相关 tls.Config更多的配置信息:

rdb := redis.NewClient(&redis.Options{
	TLSConfig: &tls.Config{
		MinVersion: tls.VersionTLS12,
		ServerName: "you domain",
		//Certificates: []tls.Certificate{cert}
	},
})

如果你使用的是域名连接,且遇到了类似 x509: cannot validate certificate for xxx.xxx.xxx.xxx because it doesn't contain any IP SANs的错误 ,应该在 ServerName 中指定你的域名:更多详情请参考本链接

rdb := redis.NewClient(&redis.Options{
	TLSConfig: &tls.Config{
		MinVersion: tls.VersionTLS12,
		ServerName: "你的域名",
	},
})

#SSH 方式

使用 SSH 协议连接:

sshConfig := &ssh.ClientConfig{
	User:			 "root",
	Auth:			 []ssh.AuthMethod{ssh.Password("password")},
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	Timeout:		 15 * time.Second,
}

sshClient, err := ssh.Dial("tcp", "remoteIP:22", sshConfig)
if err != nil {
	panic(err)
}

rdb := redis.NewClient(&redis.Options{
	Addr: net.JoinHostPort("127.0.0.1", "6379"),
	Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
		return sshClient.Dial(network, addr)
	},
	// SSH不支持超时设置,在这里禁用
	ReadTimeout:  -1,
	WriteTimeout: -1,
})

#dial tcp: i/o timeout

当你遇到 dial tcp: i/o timeout 错误时,表示 go-redis 无法连接 Redis 服务器,比如 redis 服务器没有 正常运行或监听了其他端口,以及可能被防火墙拦截等。你可以使用一些网络命令排查问题,例如 telnet:

telnet localhost 6379
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

如果你使用 Docker、Kubernetes、Istio、Service Mesh、Sidecar 方式运行,应该确保服务在容器完全可用后启 动,你可以通过参考该地址、Readiness Gate、Istio holdApplicationUntilProxyStarts等。

Context 上下文

go-redis 支持 Context,你可以使用它控制 超时 或者传递一些数据, 也可以 监控 go-redis 性能。

ctx := context.Background()

go-redis 实操 https://pkg.go.dev/github.com/go-redis/redis

安装

Code/go/redis_demo via 
    

  
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/QiaoPengjun/p/17485294.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程