4 Commits

Author SHA1 Message Date
b7d7dba4e7 增加redis server client mem 相关信息数据结构定义 2021-11-26 19:00:17 +08:00
34c9f035ee update 2021-11-26 15:56:30 +08:00
628eac60b4 add watch key with prefix 2021-11-26 14:31:25 +08:00
840ca01f87 更新gin 2021-11-26 12:29:51 +08:00
3 changed files with 139 additions and 7 deletions

4
go.mod
View File

@ -4,8 +4,6 @@ go 1.17
replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4 replace github.com/coreos/bbolt v1.3.4 => go.etcd.io/bbolt v1.3.4
replace github.com/gin-gonic/gin v1.7.5 => github.com/gin-gonic/gin v1.7.6
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0 replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
require ( require (
@ -13,7 +11,7 @@ require (
github.com/apolloconfig/agollo/v4 v4.0.9 github.com/apolloconfig/agollo/v4 v4.0.9
github.com/coreos/etcd v3.3.27+incompatible github.com/coreos/etcd v3.3.27+incompatible
github.com/ddliu/go-httpclient v0.6.9 github.com/ddliu/go-httpclient v0.6.9
github.com/gin-gonic/gin v1.7.5 github.com/gin-gonic/gin v1.7.6
github.com/go-redis/redis/v8 v8.11.4 github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis_rate/v9 v9.1.2 github.com/go-redis/redis_rate/v9 v9.1.2
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible

View File

@ -11,14 +11,16 @@ import (
"context" "context"
"math" "math"
"time" "time"
"go.etcd.io/etcd/clientv3"
) )
// WatchKey 监听key的变化,永久监听 // WatchKeyWithOption ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 2:58 下午 2021/11/23 // Date : 2:29 下午 2021/11/26
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) { func WatchKeyWithOption(ctx context.Context, watchKey string, callbackFunc WatcherHandler, optionList ...clientv3.OpOption) {
if nil == callbackFunc { if nil == callbackFunc {
// 变化之后,没有任何逻辑处理,视为不需要监听变化 // 变化之后,没有任何逻辑处理,视为不需要监听变化
return return
@ -27,7 +29,7 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler)
ctx = context.Background() ctx = context.Background()
} }
rch := Client.Watch(ctx, watchKey) // <-chan WatchResponse rch := Client.Watch(ctx, watchKey, optionList...) // <-chan WatchResponse
for watchResp := range rch { for watchResp := range rch {
for _, ev := range watchResp.Events { for _, ev := range watchResp.Events {
callbackFunc(ev) callbackFunc(ev)
@ -35,6 +37,24 @@ func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler)
} }
} }
// WatchKey 监听key的变化,永久监听
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:58 下午 2021/11/23
func WatchKey(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
WatchKeyWithOption(ctx, watchKey, callbackFunc)
}
// WatchWithKeyPrefix ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:30 下午 2021/11/26
func WatchWithKeyPrefix(ctx context.Context, watchKey string, callbackFunc WatcherHandler) {
WatchKeyWithOption(ctx, watchKey, callbackFunc, clientv3.WithPrefix())
}
// WatchKeyWithCancel 可以随时取消的 // WatchKeyWithCancel 可以随时取消的
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

114
middleware/redis/define.go Normal file
View File

@ -0,0 +1,114 @@
// Package redis ...
//
// Description : redis ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-26 5:07 下午
package redis
// FullServerInfo 获取服务器信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:07 下午 2021/11/26
type FullServerInfo struct {
ServerInfo *ServerInfo `json:"server_info"`
ClientInfo *ClientInfo `json:"client_info"`
}
// ServerInfo 服务器信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:09 下午 2021/11/26
type ServerInfo struct {
RedisVersion string `json:"redis_version" yaml:"redis_version"` // Redis 服务器版本
RedisGitSha1 string `json:"redis_git_sha1" yaml:"redis_git_sha1"` // Git SHA1
RedisGitDirty string `json:"redis_git_dirty" json:"redis_git_dirty"` // Git dirty flag
RedisBuildID string `json:"redis_build_id" json:"redis_build_id"` // 构建ID
RedisMode string `json:"redis_mode" yaml:"redis_mode"` // 运行模式(“独立”,“哨兵”或“集群”)
OS string `json:"os" yaml:"os"` // Redis 服务器的宿主操作系统
ArchBits string `json:"arch_bits" yaml:"arch_bits"` // 架构32 或 64 位)
MultiplexingApi string `json:"multiplexing_api" yaml:"multiplexing_api"` // Redis 所使用的事件处理机制
AtomicvarApi string `json:"atomicvar_api" yaml:"atomicvar_api"` // atomicvar_api
GCCVersion string `json:"gcc_version" yaml:"gcc_version"` // 编译 Redis 时所使用的 GCC 版本
ProcessID int `json:"process_id" yaml:"process_id"` // 服务器进程的 PID
ProcessSupervised string `json:"process_supervised" yaml:"process_supervised"` // 是否有进程监控
RunID string `json:"run_id" yaml:"run_id"` // Redis 服务器的随机标识符(用于 Sentinel 和集群)
TCPPort int `json:"tcp_port" yaml:"tcp_port"` // TCP/IP 监听端口
ServerTimeUsec int64 `json:"server_time_usec" yaml:"server_time_usec"` // server_time_usec
UptimeInSeconds int64 `json:"uptime_in_seconds" yaml:"uptime_in_seconds"` // 自 Redis 服务器启动以来,经过的秒数
UptimeInDays int64 `json:"uptime_in_days" yaml:"uptime_in_days"` // 自 Redis 服务器启动以来,经过的天数
Hz int64 `json:"hz" yaml:"hz"` // 当前服务器频率
ConfiguredHz int64 `json:"configured_hz" yaml:"configured_hz"` // 服务器的频率设置
LRUClock int64 `json:"lru_clock" yaml:"lru_clock"` // 以分钟为单位进行自增的时钟,用于 LRU 管理
Executable string `json:"executable" yaml:"executable"` // 可执行文件位置
ConfigFile string `json:"config_file" yaml:"config_file"` // 服务所使用的配置文件
IOThreadsActive int `json:"io_threads_active" yaml:"io_threads_active"` // 活跃的io线程数
}
// ClientInfo 客户端信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:29 下午 2021/11/26
type ClientInfo struct {
ConnectedClients int `json:"connected_clients" yaml:"connected_clients"` // 客户端连接数
ClusterConnections int `json:"cluster_connections" yaml:"cluster_connections"` // 集群连接数
MaxClients int `json:"maxclients" yaml:"max_clients"` // 最大客户端连接数
ClientRecentMaxInputBuffer int64 `json:"client_recent_max_input_buffer" yaml:"client_recent_max_input_buffer"` // 最近最大输入缓存
ClientRecentMaxOutputBuffer int64 `json:"client_recent_max_output_buffer" yaml:"client_recent_max_output_buffer"` // 最近最大输出缓存
BlockedClients int `json:"blocked_clients" yaml:"blocked_clients"` // 阻塞客户端数量, 正在等待阻塞命令BLPOP、BRPOP、BRPOPLPUSH的客户端的数量
TrackingClients int `json:"tracking_clients" yaml:"tracking_clients"` // tracking_clients
ClientsInTimeoutTable int `json:"clients_in_timeout_table" yaml:"clients_in_timeout_table"` // clients_in_timeout_table
}
// MemoryInfo 内存使用信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:08 下午 2021/11/26
type MemoryInfo struct {
UsedMemory int64 `json:"used_memory" yaml:"used_memory"` // 由 Redis 分配器分配的内存总量以字节byte为单位
UsedMemoryHuman string `json:"used_memory_human" yaml:"used_memory_human"` // UsedMemory 可读化表示
UsedMemoryRss int64 `json:"used_memory_rss" yaml:"used_memory_rss"` // 从操作系统的角度,返回 Redis 已分配的内存总量(俗称常驻集大小)。这个值和 top 、 ps 等命令的输出一致。
UsedMemoryRssHuman string `json:"used_memory_rss_human" yaml:"used_memory_rss_human"` // UsedMemoryRssHuman 可读化表示
UsedMemoryPeak int64 `json:"used_memory_peak" yaml:"used_memory_peak"` // Redis 的内存消耗峰值(以字节为单位)
UsedMemoryPeakHuman string `json:"used_memory_peak_human" yaml:"used_memory_peak_human"` // UsedMemoryPeak 可读化表示
UsedMemoryPeakPerc string `json:"used_memory_peak_perc" yaml:"used_memory_peak_perc"` // 峰值系统内存使用率
UsedMemoryOverhead int64 `json:"used_memory_overhead" yaml:"used_memory_overhead"` // Redis为了维护数据集的内部机制所需的内存开销包括所有客户端输出缓冲区、查询缓冲区、AOF重写缓冲区和主从复制的backlog
UsedMemoryStartup int64 `json:"used_memory_startup" yaml:"used_memory_startup"` // Redis服务器启动时消耗的内存
UsedMemoryDataset int64 `json:"used_memory_dataset" yaml:"used_memory_dataset"` // 数据占用的内存大小即used_memory-used_memory_overhead
UsedMemoryDatasetPerc int64 `json:"used_memory_dataset_perc" yaml:"used_memory_dataset_perc"` // 数据占用的内存大小的百分比100%*(used_memory_dataset/(used_memory-used_memory_startup))
AllocatorAllocated int64 `json:"allocator_allocated" yaml:"allocator_allocated"` // allocator_allocated
AllocatorActive int64 `json:"allocator_active" yaml:"allocator_active"` // allocator_active
AllocatorResident int64 `json:"allocator_resident" yaml:"allocator_resident"` // allocator_resident
TotalSystemMemory int64 `json:"total_system_memory" yaml:"total_system_memory"` // 操作系统内存(以字节为单位)
TotalSystemMemoryHuman string `json:"total_system_memory_human" yaml:"total_system_memory_human"` // TotalSystemMemory 可读化表示
UsedMemoryLua int64 `json:"used_memory_lua" yaml:"used_memory_lua"` // Lua脚本存储占用的内存以字节为单位
UsedMemoryLuaHuman int64 `json:"used_memory_lua_human" yaml:"used_memory_lua_human"` // UsedMemoryLua 可读化表示
UsedMemoryScripts int64 `json:"used_memory_scripts" yaml:"used_memory_scripts"` // Lua脚本使用的内存大小以字节为单位
UsedMemoryScriptsHuman int64 `json:"used_memory_scripts_human" yaml:"used_memory_scripts_human"` // UsedMemoryScripts 可读化表示
NumberOfCachedScripts int64 `json:"number_of_cached_scripts" yaml:"number_of_cached_scripts"` // 缓存的lua脚本数量
Maxmemory int64 `json:"maxmemory" yaml:"maxmemory"` // Redis实例的最大内存配置以字节为单位
MaxmemoryHuman int64 `json:"maxmemory_human" yaml:"maxmemory_human"` // Maxmemory 可读化表示
MaxmemoryPolicy string `json:"maxmemory_policy" yaml:"maxmemory_policy"` // 当数据达到最大内存之后的淘汰策略
AllocatorFragRatio float64 `json:"allocator_frag_ratio" yaml:"allocator_frag_ratio"` // allocator_frag_ratio
AllocatorFragBytes int64 `json:"allocator_frag_bytes" yaml:"allocator_frag_bytes"` // allocator_frag_bytes
AllocatorRssRatio float64 `json:"allocator_rss_ratio" yaml:"allocator_rss_ratio"` // allocator_rss_ratio
AllocatorRssBytes int64 `json:"allocator_rss_bytes" yaml:"allocator_rss_bytes"` // allocator_rss_bytes
RssOverheadRatio float64 `json:"rss_overhead_ratio" yaml:"rss_overhead_ratio"` // rss_overhead_ratio
RssOverheadBytes int64 `json:"rss_overhead_bytes" yaml:"rss_overhead_bytes"` // rss_overhead_bytes
MemFragmentationRatio float64 `json:"mem_fragmentation_ratio" yaml:"mem_fragmentation_ratio"` // 碎片率used_memory_rss/ used_memory正常情况下稍大于1。低于1Redis实例可能会把部分数据交换到硬盘上内存交换会严重影响Redis的性能所以应该增加可用物理内存。大于1.5表示碎片过多。额外碎片的产生是由于Redis释放了内存块但内存分配器并没有返回内存给操作系统这个内存分配器是在编译时指定的可以是libc、jemalloc或者tcmalloc。
MemFragmentationBytes int64 `json:"mem_fragmentation_bytes" yaml:"mem_fragmentation_bytes"` // 内存碎片大小(字节表示)
MemNotCountedForEvict int64 `json:"mem_not_counted_for_evict" yaml:"mem_not_counted_for_evict"` // mem_not_counted_for_evict
MemReplicationBacklog int64 `json:"mem_replication_backlog" yaml:"mem_replication_backlog"` // mem_replication_backlog
MemClientsSlaves int64 `json:"mem_clients_slaves" yaml:"mem_clients_slaves"` // mem_clients_slaves
MemClientsNormal int64 `json:"mem_clients_normal" yaml:"mem_clients_normal"` // mem_clients_normal
MemAofBuffer int64 `json:"mem_aof_buffer" yaml:"mem_aof_buffer"` // mem_aof_buffer
MemAllocator string `json:"mem_allocator" yaml:"mem_allocator"` // 内存分配器Redis支持glibcs malloc、jemalloc11、tcmalloc几种不同的内存分配器每个分配器在内存分配和碎片上都有不同的实现。不建议普通管理员修改Redis默认内存分配器因为这需要完全理解这几种内存分配器的差异也要重新编译Redis。
ActiveDefragRunning int64 `json:"active_defrag_running" yaml:"active_defrag_running"` // defrag:表示内存碎片整理, 0表示没有活动的defrag任务正在运行1表示有活动的defrag任务正在运行
LazyfreePendingObjects int64 `json:"lazyfree_pending_objects" yaml:"lazyfree_pending_objects"` // 延迟释放的挂起对象, 0表示不存在
LazyfreedObjects int64 `json:"lazyfreed_objects" yaml:"lazyfreed_objects"` // 延迟释放的对象数量
}