当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 网安学习-内网渗透3
- Markdown的使用
- When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
- MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
- Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
- MySQL 灵魂 16 问,你能撑到第几问?
- mysql remove duplicate data
- It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
- 手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
- 2022.7.30
猜你喜欢
随机推荐
Rust编译报错:error: linker `cc` not found
MySQL Soul 16 Questions, How Many Questions Can You Last?
2022.7.30
The most powerful and most commonly used SQL statements in history
ArrayList扩容机制分析
cookie和session区别
系统结构考点之CRAY-1向量处理机
language code table
Navicat new database
2022.7.30
基于ABP实现DDD--实体创建和更新
MySQL cursors
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
【菜鸡含泪总结】如何用pip、anaconda安装库
折叠旧版应用程序
Regular expression syntax and usage
TransGAN代码复现—九天毕昇平台
【问题】Mysql Waiting for table metadata lock 解决方案 修改lock_wait_timeout时间
Advanced c language: pointers (5)
1064 Complete Binary Search Tree