当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
- 【无标题】
- 【Network Security Column Directory】--Penguin Column Navigation
- 482-静态库、动态库的制作、使用及区别
- ValueError: Append mode is not supported with xlsxwriter解决方案
- Rust编译报错:error: linker `cc` not found
- PhpMetrics 使用
- navicat新建数据库
- The reason for not using bs4 is that the name is too long?Crawl lottery lottery information
- 系统结构考点之PM2I单级网络
猜你喜欢

MySQL Soul 16 Questions, How Many Questions Can You Last?

系统结构考点之并行主存

使用LVS和Keepalived搭建高可用负载均衡服务器集群

MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)

It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)

482-静态库、动态库的制作、使用及区别

基于ABP实现DDD--仓储实践

TransGAN代码复现—九天毕昇平台

【零代码工具】15 款企业级零代码开发平台推荐,总有一款是你心仪的

The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
随机推荐
IDEA2021.2安装与配置(持续更新)
Navigation Bar----Personal Center Dropdown
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
A simple rich text editor
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
MYSQL JDBC Book Management System
cookie和session区别
for...in 和 for...of 的区别
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
1064 Complete Binary Search Tree
2022.7.27
The most complete Redis basic + advanced project combat summary notes in history
力扣题(3)—— 无重复字符的最长子串
MySQL cursors
Gxlcms有声小说系统/小说听书系统源码
Solve the problem of centos8 MySQL password ERROR 1820 (HY000) You must reset your password using the ALTER USER
Learning about XML (1)
系统结构考点之并行主存
连号区间数
Successfully resolved ModuleNotFoundError: No module named 'Image'