SpringBoot整合Redis可以通過RedisTemplate和Redisson兩種方式。
1. RedisTemplate獲取Redis信息
根據(jù)Connection獲取Redis緩存
信息文章來源:http://www.zghlxwxcb.cn/news/detail-808573.html
// 方式1:獲取Redis緩存全部信息
Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info();
// 方式2:根據(jù)Connection獲取Redis緩存指定信息:
Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info(“section”);
方式2可選參數(shù):
server: 常規(guī)信息
clients: 客戶端連接部分
memory: 內(nèi)存消耗相關信息
persistence: RDB和AOF相關信息
stats: 統(tǒng)計信息
replication: 主/從復制信息
cpu: CPU消耗統(tǒng)計
commandstats: Redis命令統(tǒng)計
cluster: 集群部分
keyspace: 數(shù)據(jù)庫、key相關統(tǒng)計
2. Redisson獲取Redis信息
2.1 Redisson版本3.18.1及之前
根據(jù)Connection獲取Redis緩存
信息
private final RedissonConnectionFactory connectionFactory;
RedisConnection connection = connectionFactory.getConnection();
// 方式1:獲取Redis緩存全部信息
Properties info = connection.info();
// 方式2:根據(jù)Connection獲取Redis緩存指定信息
Properties info = connection.info(“section”);
2.2 Redisson版本3.19.0及之后
根據(jù)Connection獲取Redis緩存
信息
private final RedissonConnectionFactory connectionFactory;
RedisConnection connection = connectionFactory.getConnection();
// 方式1:獲取Redis緩存全部信息
Properties info = connection.commands().info();
// 方式2:根據(jù)Connection獲取Redis緩存指定信息
Properties info = connection.commands().info(“section”);文章來源地址http://www.zghlxwxcb.cn/news/detail-808573.html
2.3 相關集成代碼
<!--redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</dependency>
@RequiredArgsConstructor
public class RedisMonitorUtil {
private final RedissonConnectionFactory connectionFactory;
/**
* 獲取redis服務器相關信息
* @return
*/
public CacheListInfoVO getInfo() {
CacheListInfoVO infoVo = new CacheListInfoVO();
RedisConnection connection = connectionFactory.getConnection();
Properties commandStats = connection.info("commandstats");
List<CommandstatsVO> pieList = new ArrayList<>();
if (commandStats != null) {
commandStats.stringPropertyNames().forEach(key -> {
CommandstatsVO commandstatsVO = new CommandstatsVO();
String property = commandStats.getProperty(key);
commandstatsVO.setName(StringUtils.removeStart(key, "cmdstat_"));
commandstatsVO.setCalls(StringUtils.substringBetween(property, "calls=", ",usec"));
commandstatsVO.setPerCall(StringUtils.substringAfter(property, "usec_per_call="));
pieList.add(commandstatsVO);
});
}
try {
ObjectMapper objectMapper = new ObjectMapper();
CacheInfoVO cacheInfoVO = JSON.parseObject(objectMapper.writeValueAsString(connection.info()), CacheInfoVO.class);
infoVo.setInfo(cacheInfoVO);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
infoVo.setDbSize(connection.dbSize());
infoVo.setCommandStats(pieList);
return infoVo;
}
}
3. Redis Info命令信息解釋
redis> INFO ALL
# Server
redis_version:5.0.5 #Redis服務器的版本
redis_git_sha1:00000000 #Git SHA1
redis_git_dirty:0 #Git dirty flag
redis_build_id:cdff23e4497417f9 #構(gòu)建ID
redis_mode:cluster #Redis啟動模式:standalone、Sentinel、Cluster
os:Linux 4.4.0-143-generic x86_64 #redis宿主機操作系統(tǒng)
arch_bits:64 #架構(gòu):32位、64位
multiplexing_api:epoll #事件循環(huán)機制
atomicvar_api:atomic-builtin #Atomicvar API
gcc_version:5.4.0 #編譯 Redis 時所使用的 GCC 版本
process_id:41450 #進程PID
run_id:7c1db72b9f235c5e52780aeb8817fd272230f1bc #標識Redis服務器的唯一隨機值,由Sentinel和Cluster使用
tcp_port:6379 #TCP、IP偵聽端口
uptime_in_seconds:129723 #自Redis服務器啟動以來的秒數(shù)
uptime_in_days:1 #自Redis服務器啟動以來的天數(shù)
hz:10 #調(diào)度serverCron每秒運行次數(shù)
configured_hz:10 #
lru_clock:10612903 #以分鐘為單位進行自增的時鐘,用于 LRU 管理
executable:/usr/local/redis5.0/bin/redis-server #服務器可執(zhí)行文件的路徑
config_file:/usr/local/redis5.0/6379/redis6379.conf #啟動 redis 配置文件
# Clients
connected_clients:1 #已連接客戶端的數(shù)量(不包括通過從服務器連接的客戶端)
client_recent_max_input_buffer:2 #當前連接的客戶端當中,最大輸入緩存
client_recent_max_output_buffer:0 #當前連接的客戶端當中,最長的輸出列表
blocked_clients:0 #正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客戶端的數(shù)量
# Memory
used_memory:2660408 #由 redis 分配器(標準libc,jemalloc或其他分配器,例如tcmalloc)分配的內(nèi)存總量,以字節(jié)(byte)為單位
used_memory_human:2.54M #以可讀的格式返回 redis 分配的內(nèi)存總量
used_memory_rss:9154560 #從操作系統(tǒng)的角度,返回 Redis 已分配的內(nèi)存總量(俗稱常駐集大?。?。這個值和 top、ps 等命令的輸出一致。
used_memory_rss_human:8.73M #以可讀的格式,操作系統(tǒng)角度,返回 redis 分配的內(nèi)存總量
used_memory_peak:204081928 #redis 的內(nèi)存消耗峰值(以字節(jié)為單位)
used_memory_peak_human:194.63M #以可讀的格式,返回 Redis 的內(nèi)存消耗峰值
used_memory_peak_perc:1.30% #used_memory_peak在used_memory中所占的百分比
used_memory_overhead:2565048 #分配用于管理其內(nèi)部數(shù)據(jù)結(jié)構(gòu)的所有開銷的總字節(jié)數(shù)
used_memory_startup:1449744 #啟動時消耗的初始內(nèi)存量(以字節(jié)為單位)
used_memory_dataset:95360 #數(shù)據(jù)集的大?。ㄒ宰止?jié)為單位,used_memory - used_memory_overhead)
used_memory_dataset_perc:7.88% #used_memory_dataset在凈內(nèi)存(used_memory-used_memory_startup)使用量中所占的百分比
allocator_allocated:2770640 #分配器分配的內(nèi)存
allocator_active:3371008 #分配器活躍的內(nèi)存
allocator_resident:11554816 #分配器常駐的內(nèi)存
total_system_memory:1021468672 #主機擁有的內(nèi)存總量
total_system_memory_human:974.15M #以可讀的格式返回主機擁有的內(nèi)存總量
used_memory_lua:37888 #Lua引擎使用的字節(jié)數(shù)
used_memory_lua_human:37.00K #以可讀的格式返回Lua引擎使用內(nèi)存
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0 #配置設置的最大可使用內(nèi)存值,默認0,不限制
maxmemory_human:0B #以可讀的格式返回最大可使用內(nèi)存值
maxmemory_policy:noeviction #內(nèi)存容量超過maxmemory后的處理策略,noeviction當內(nèi)存使用達到閾值的時候,所有引起申請內(nèi)存的命令會報錯
allocator_frag_ratio:1.22 #分配器的碎片率
allocator_frag_bytes:600368 #分配器的碎片大?。ㄒ宰止?jié)為單位)
allocator_rss_ratio:3.43 #分配器常駐內(nèi)存比例
allocator_rss_bytes:8183808 #分配器的常駐內(nèi)存大?。ㄒ宰止?jié)為單位)
rss_overhead_ratio:0.79 #常駐內(nèi)存開銷比例
rss_overhead_bytes:-2400256 #常駐內(nèi)存開銷大小(以字節(jié)為單位)
mem_fragmentation_ratio:3.50 #內(nèi)存碎片率,used_memory_rss 和 used_memory 之間的比率
mem_fragmentation_bytes:6536432 #內(nèi)存碎片的大?。ㄒ宰止?jié)為單位)
mem_not_counted_for_evict:0 #被驅(qū)逐的大小
mem_replication_backlog:1048576 #repl_backlog
mem_clients_slaves:16922 #clients_slaves
mem_clients_normal:49694 #clients_normal
mem_aof_buffer:0 #aof時,占用的緩沖
mem_allocator:jemalloc-5.1.0 #內(nèi)存分配器(在編譯時選擇)
active_defrag_running:0 #碎片整理是否處于活動狀態(tài)
lazyfree_pending_objects:0 #等待釋放的對象數(shù)(由于使用ASYNC選項調(diào)用UNLINK或FLUSHDB和FLUSHALL)
# Persistence
loading:0 #記錄服務器是否正在載入持久化文件
rdb_changes_since_last_save:0 #最近一次成功創(chuàng)建持久化文件之后,經(jīng)過了多少秒
rdb_bgsave_in_progress:0 #記錄了服務器是否正在創(chuàng)建 RDB 文件
rdb_last_save_time:1570890961 #最近一次成功創(chuàng)建 RDB 文件的 UNIX 時間戳
rdb_last_bgsave_status:ok #記錄最近一次創(chuàng)建 RDB 文件的狀態(tài),是成功還是失敗
rdb_last_bgsave_time_sec:0 #記錄了最近一次創(chuàng)建 RDB 文件耗費的秒數(shù)
rdb_current_bgsave_time_sec:-1 #如果正在創(chuàng)建 RDB 文件,記錄當前的創(chuàng)建操作已經(jīng)耗費的秒數(shù)
rdb_last_cow_size:249856 #上一次RBD保存操作期間寫時復制的大?。ㄒ宰止?jié)為單位)
aof_enabled:1 #AOF是否開啟
aof_rewrite_in_progress:0 #記錄了是否正在創(chuàng)建 AOF 文件
aof_rewrite_scheduled:0 #記錄了 RDB 文件創(chuàng)建完畢之后,是否需要執(zhí)行 AOF 重寫操作
aof_last_rewrite_time_sec:0 #最近一次創(chuàng)建 AOF 文件耗費的秒數(shù)
aof_current_rewrite_time_sec:-1 #如果正在創(chuàng)建 AOF 文件,那么記錄當前的創(chuàng)建操作耗費的秒數(shù)
aof_last_bgrewrite_status:ok #記錄了最近一次創(chuàng)建 AOF 文件的狀態(tài),是成功還是失敗
aof_last_write_status:ok #AOF的最后寫入操作的狀態(tài),是成功還是失敗
aof_last_cow_size:307200 #上一次AOF保存操作期間寫時復制的大?。ㄒ宰止?jié)為單位)
aof_current_size:115 #AOF 文件當前的大小
aof_base_size:115 #最近一次啟動或重寫時的AOF文件大小
aof_pending_rewrite:0 #記錄了是否有 AOF 重寫操作在等待 RDB 文件創(chuàng)建完畢之后執(zhí)行
aof_buffer_length:0 #AOF緩沖區(qū)的大小
aof_rewrite_buffer_length:0 #AOF 重寫緩沖區(qū)的大小
aof_pending_bio_fsync:0 #后臺 I/O 隊列里面,等待執(zhí)行的 fsync 數(shù)量
aof_delayed_fsync:0 #被延遲的 fsync 調(diào)用數(shù)量,如果該值比較大,可以開啟參數(shù):no-appendfsync-on-rewrite=yes
如果正在進行加載操作,會有以下狀態(tài):
loading_start_time: #加載操作開始的時間戳
loading_total_bytes: #加載文件總大小
loading_loaded_bytes: #已加載的字節(jié)數(shù)
loading_loaded_perc: #已加載的百分比
loading_eta_seconds: #完成加載所需的秒數(shù)(以秒為單位)
注意:
changes_since_last_save是指自上次調(diào)用SAVE或BGSAVE以來在數(shù)據(jù)集中產(chǎn)生某種更改的操作數(shù)。
# Stats
total_connections_received:11046 #服務器接受的連接總數(shù)
total_commands_processed:2086515 #服務器已執(zhí)行的命令數(shù)量
instantaneous_ops_per_sec:0 #服務器每秒鐘執(zhí)行的命令數(shù)量
total_net_input_bytes:116217212 #啟動以來,流入的字節(jié)總數(shù)
total_net_output_bytes:161658834 #啟動以來,流出的字節(jié)總數(shù)
instantaneous_input_kbps:0.02 #接收輸入的速率(每秒)
instantaneous_output_kbps:0.00 #輸出的速率(每秒)
rejected_connections:0 #由于maxclients限制而被拒絕的連接數(shù)
sync_full:1 #與slave full sync的次數(shù)
sync_partial_ok:14 #接受的部分重新同步(psync)請求的數(shù)量
sync_partial_err:1 #被拒絕的部分重新同步(psync)請求的數(shù)量
expired_keys:0 #key過期事件總數(shù)
expired_stale_perc:0.00 #過期的比率
expired_time_cap_reached_count:0 #過期計數(shù)
evicted_keys:0 #由于最大內(nèi)存限制而被驅(qū)逐的key數(shù)量
keyspace_hits:1057312 #key命中次數(shù)
keyspace_misses:38734 #key未命中次數(shù)
pubsub_channels:0 #發(fā)布/訂閱頻道的數(shù)量
pubsub_patterns:0 #發(fā)布/訂閱的模式數(shù)量
latest_fork_usec:1460 #最近一次 fork() 操作耗費的毫秒數(shù)(以微秒為單位)
migrate_cached_sockets:0 #為遷移而打開的套接字數(shù)
slave_expires_tracked_keys:0 #跟蹤過期key數(shù)量(僅適用于可寫從)
active_defrag_hits:0 #活躍碎片執(zhí)行的值重新分配的數(shù)量
active_defrag_misses:0 #活躍碎片執(zhí)行的中止值重新分配的數(shù)量
active_defrag_key_hits:0 #活躍碎片整理的key數(shù)
active_defrag_key_misses:0 #活躍碎片整理過程跳過的key數(shù)
# Replication
role:master #角色(master、slave),一個從服務器也可能是另一個服務器的主服務器
connected_slaves:1 #連接slave實例的個數(shù)
slave0:ip=192.168.163.132,port=6382,state=online,offset=64547142,lag=1 #連接的slave的信息
master_replid:1726c598c37f039c4b69db7a4281392a650eb88b #服務器的復制ID
master_replid2:0000000000000000000000000000000000000000 #第二服務器復制ID,用于故障轉(zhuǎn)移后的PSYNC,用于集群等高可用之后主從節(jié)點的互換
master_repl_offset:64547142 #復制偏移量1
second_repl_offset:-1 #第二服務器復制偏移量2
repl_backlog_active:1 #復制緩沖區(qū)狀態(tài)
repl_backlog_size:1048576 #復制緩沖區(qū)的大?。ㄒ宰止?jié)為單位)
repl_backlog_first_byte_offset:63498567 #復制緩沖區(qū)的偏移量,標識當前緩沖區(qū)可用范圍
repl_backlog_histlen:1048576 #復制緩沖區(qū)中數(shù)據(jù)的大小(以字節(jié)為單位)
#如果是從節(jié)點,會有以下狀態(tài):
master_host:192.168.163.132 #Master IP
master_port:6379 #Master Port
master_link_status:up #Master的連接狀態(tài)(up/down)
master_last_io_seconds_ago:8 #最近一次主從交互之后的秒數(shù)
master_sync_in_progress:0 #表示從服務器是否一直在與主服務器進行同步
slave_repl_offset:64547142 #復制偏移量
slave_priority:100 #從服務器的優(yōu)先級
slave_read_only:1 #從服務是否只讀
#如果正在進行SYNC操作,會有以下狀態(tài):
master_sync_left_bytes: #同步完成前剩余的字節(jié)數(shù)
master_sync_last_io_seconds_ago: #自SYNC操作以來最后一次傳輸I/O經(jīng)過的秒數(shù)
#如果主服務器和副本服務器之間的鏈接斷開,會有以下狀態(tài):
master_link_down_since_seconds: #主從連接斷開后經(jīng)過的秒數(shù)
connected_slaves: #已連從的數(shù)量
#如果服務器配置(的Redis 5)有min-slaves-to-write(或以min-replicas-to-write)指令,會有以下狀態(tài):
min_slaves_good_slaves: #當前認為良好的副本數(shù),對于每個副本,添加以下行:
slaveXXX: id, IP address, port, state, offset, lag
# CPU
used_cpu_sys:133.908000 #消耗的系統(tǒng)CPU
used_cpu_user:70.692000 #消耗的用戶CPU
used_cpu_sys_children:0.016000 #后臺進程占用的系統(tǒng)CPU
used_cpu_user_children:0.044000 #后臺進程占用的用戶CPU
# Commandstats #提供基于命令類型的統(tǒng)計信息,包括調(diào)用次數(shù),這些命令消耗的總CPU時間以及每個命令執(zhí)行消耗的平均CPU時間。
cmdstat_cluster:calls=33169,usec=3835426,usec_per_call=115.63
cmdstat_keys:calls=3,usec=7828,usec_per_call=2609.33
cmdstat_get:calls=1096046,usec=914358,usec_per_call=0.83
cmdstat_set:calls=872138,usec=984030,usec_per_call=1.13
cmdstat_monitor:calls=6,usec=4,usec_per_call=0.67
cmdstat_replconf:calls=74181,usec=103173,usec_per_call=1.3
cmdstat_client:calls=11,usec=2812,usec_per_call=255.64
cmdstat_flushdb:calls=2,usec=23058,usec_per_call=11529.00
cmdstat_dbsize:calls=22,usec=27,usec_per_call=1.23
cmdstat_auth:calls=10883,usec=19716,usec_per_call=1.81
cmdstat_info:calls=14,usec=6987,usec_per_call=499.07
cmdstat_config:calls=9,usec=5819,usec_per_call=646.56
cmdstat_psync:calls=15,usec=4288,usec_per_call=285.87
cmdstat_command:calls=16,usec=98434,usec_per_call=6152.12
# Cluster
cluster_enabled:1 #是否開啟集群模式
# Keyspace #鍵空間部分提供有關每個數(shù)據(jù)庫的統(tǒng)計信息。 統(tǒng)計信息是鍵的數(shù)量,以及帶有到期時間的鍵的數(shù)量。
db0:keys=2,expires=0,avg_ttl=0
- 參考資料
Redis 的info命令信息解釋
到了這里,關于SpringBoot整合Redis實現(xiàn)緩存信息監(jiān)控的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!