当前位置:网站首页>Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
2022-07-30 22:10:00 【web18334137065】
前言
至于go-redis的cluster模式怎么调用,我就不多说了。可以参考我的另一篇文章:Go语学习笔记 - redis cluster集群模式 | Web框架Gin(十三)_的博客-CSDN博客
在我测试集群模式下redis调用的时候发现效率不如单一节点模式。
我们先复现一下问题是怎么出现的。
问题复现
先初始化构建一个cluster集群连接。
package connect
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var Cluster *redis.ClusterClient
func init() {
Cluster = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{
"xxxxx:7379",
"xxxxx:7380",
},
Password: "123456",
//DialTimeout: 100 * time.Microsecond,
ReadTimeout: 100 * time.Microsecond,
//WriteTimeout: 100 * time.Microsecond,
DialTimeout: 5 * time.Second, //连接建立超时时间,默认5秒。
//ReadTimeout: 3 * time.Second, //读超时,默认3秒, -1表示取消读超时
WriteTimeout: 3 * time.Second, //写超时,默认等于读超时
OnConnect: func(ctx context.Context, conn *redis.Conn) error {
fmt.Printf("创建新的连接: %v
", conn)
return nil
},
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Cluster.Ping(ctx).Result()
}
写一个简单的测试redis使用的demo。
package demo
import (
"RedisCluster/connect"
"context"
"fmt"
"time"
)
const Key = "test-111"
func SampleDemo() {
// 第一次写入数据,并设置10分钟缓存
connect.Cluster.Set(context.TODO(), Key, "666", 10*time.Minute)
cmd := connect.Cluster.Get(context.TODO(), Key)
result, _ := cmd.Result()
fmt.Println("result:", result)
// 第二次写入数据,并设置10分钟缓存
start := time.Now().UnixMilli()
connect.Cluster.Set(context.TODO(), Key, "777", 10*time.Minute)
end := time.Now().UnixMilli()
fmt.Println("set ->", (end - start))
cmd = connect.Cluster.Get(context.TODO(), Key)
end1 := time.Now().UnixMilli()
fmt.Println("get ->", (end1 - end))
result, _ = cmd.Result()
fmt.Println("result:", result)
}
代码说明
1、启动的时候会对Cluster进行初始化,我们先存取一次,然后接着存取第二次。主要看一下第二次的耗时情况。
2、在创建Cluster的时候建了一个钩子函数,如果创建新连接会打印一句话。
执行结果

可以看到在第二次执行的时候,创建了多个连接,直接导致设置key的时间到了150ms。
问题解决
我在网上找了挺久,类似问题没怎么找到,我开始试着看一遍入参。
最终是将ReadTimeout调整为3秒后,正常了。
代码调整如下:
//DialTimeout: 100 * time.Microsecond,
ReadTimeout: 3000 * time.Microsecond,
//WriteTimeout: 100 * time.Microsecond,
DialTimeout: 5 * time.Second, //连接建立超时时间,默认5秒。
//ReadTimeout: 3 * time.Second, //读超时,默认3秒, -1表示取消读超时
WriteTimeout: 3 * time.Second, //写超时,默认等于读超时
执行结果

小结
以后有时间还是要看看具体原因。

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- ValueError: Append mode is not supported with xlsxwriter解决方案
- MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
- MySQL 5.7 detailed download, installation and configuration tutorial
- MySQL 游标
- 史上超强最常用SQL语句大全
- ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
- MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
- 网安学习-内网渗透3
- MySQL删除表数据 MySQL清空表命令 3种方法
- TCP 连接 三次握手 四次挥手
猜你喜欢

MySql创建数据表

Gxlcms有声小说系统/小说听书系统源码

鳄梨价格数据集(Avocado Prices)

解决centos8 MySQL密码问题ERROR 1820 (HY000) You must reset your password using ALTER USER

【无标题】

【Network Security Column Directory】--Penguin Column Navigation

When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'

Navicat cannot connect to mysql super detailed processing method

CISP-PTE Zhenti Demonstration

类和对象——上
随机推荐
不用bs4的原因居然是名字太长?爬取彩票开奖信息
MySQL user authorization
2022.7.27
PhpMetrics 使用
Solve npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead
网安学习-内网渗透3
ELF: Loading process
MySQL cursors
Markdown的使用
cnpm installation steps
ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
代码越写越乱?那是因为你没用责任链
ValueError: Append mode is not supported with xlsxwriter解决方案
【Network Security Column Directory】--Penguin Column Navigation
Union, the difference between union and structure, the knowledge of enumeration of C language corners
基于ABP实现DDD--仓储实践
IDEA2021.2安装与配置(持续更新)
1064 Complete Binary Search Tree
navicat无法连接mysql超详细处理方法
IDEA 连接 数据库