欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的基本操作

 更新時(shí)間:2025年02月08日 09:37:27   作者:路 飛  
SpringBoot中的 redistemplate 是一個(gè)用于操作 Redis 數(shù)據(jù)庫(kù)的高級(jí)模板類,它提供了一組方法,可以方便地執(zhí)行常見(jiàn)的 Redis 操作,如存儲(chǔ)、檢索和刪除數(shù)據(jù),本文給大家介紹了SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的基本操作,需要的朋友可以參考下

實(shí)現(xiàn)效果如圖:

在這里插入圖片描述

注意:文章基于使用SpringBoot整合Redis,整合步驟和Redis基本操作默認(rèn)讀者已經(jīng)掌握。

1. redisTemplate獲取獲取redis信息

根據(jù)Connection獲取Redis緩存全部信息:

// 方式1:獲取Redis緩存全部信息
Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info();

// 方式2:根據(jù)Connection獲取Redis緩存指定信息:
Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info("xxx");

在這里插入圖片描述

  • 方式2可選參數(shù)如下:
    • server:有關(guān)Redis服務(wù)器的常規(guī)信息
    • clients:客戶端連接部分
    • memory:內(nèi)存消耗相關(guān)信息
    • persistence:RDB和AOF相關(guān)信息
    • stats:一般統(tǒng)計(jì)
    • replication:主/副本復(fù)制信息
    • cpu:CPU消耗統(tǒng)計(jì)信息
    • commandstats:Redis命令統(tǒng)計(jì)
    • cluster:Redis群集部分
    • keyspace:與數(shù)據(jù)庫(kù)相關(guān)的統(tǒng)計(jì)
    • 它還可以采用以下值:
      • all:返回所有部分
      • default
  • 返回值如下:
redis> INFO
# Server
# Redis服務(wù)器版本
redis_version:999.999.999
redis_git_sha1:3c968ff0
redis_git_dirty:0
redis_build_id:51089de051945df4
redis_mode:standalone
# Redis 服務(wù)器的宿主操作系統(tǒng)
os:Linux 4.8.0-1-amd64 x86_64
# 架構(gòu)(32 或 64 位)
arch_bits:64
# Redis 所使用的事件處理機(jī)制
multiplexing_api:epoll
atomicvar_api:atomic-builtin
# 編譯 Redis 時(shí)所使用的 GCC 版本
gcc_version:6.3.0
# 服務(wù)器進(jìn)程的 PID
process_id:9941
# Redis 服務(wù)器的隨機(jī)標(biāo)識(shí)符(用于 Sentinel 和集群)
run_id:b770a8af038963f3d1b55358c2e376d0b5e00182
# TCP/IP 監(jiān)聽(tīng)端口
tcp_port:6379
# 自 Redis 服務(wù)器啟動(dòng)以來(lái),經(jīng)過(guò)的秒數(shù)
uptime_in_seconds:1028993
# 自 Redis 服務(wù)器啟動(dòng)以來(lái),經(jīng)過(guò)的天數(shù)
uptime_in_days:11
hz:10
# 以分鐘為單位進(jìn)行自增的時(shí)鐘,用于 LRU 管理
lru_clock:10750613
executable:/usr/local/bin/redis-server
config_file:

# Clients
# 已連接客戶端的數(shù)量(不包括通過(guò)從屬服務(wù)器連接的客戶端)
connected_clients:4
# 當(dāng)前連接的客戶端當(dāng)中,最長(zhǎng)的輸出列表
client_longest_output_list:0
# 當(dāng)前連接的客戶端當(dāng)中,最大輸入緩存
client_biggest_input_buf:0
# 正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客戶端的數(shù)量
blocked_clients:0

# Memory
# 由 Redis 分配器分配的內(nèi)存總量,以字節(jié)(byte)為單位
used_memory:154272800
# 以人類可讀的格式返回 Redis 分配的內(nèi)存總量
used_memory_human:147.13M
# 從操作系統(tǒng)的角度,返回 Redis 已分配的內(nèi)存總量(俗稱常駐集大?。_@個(gè)值和 top 、 ps等命令的輸出一致。
used_memory_rss:160612352
# 以人類可讀的格式返回
used_memory_rss_human:153.17M
# Redis 的內(nèi)存消耗峰值(以字節(jié)為單位)
used_memory_peak:154319968
# 以人類可讀的格式返回 Redis 的內(nèi)存消耗峰值
used_memory_peak_human:147.17M
# 使用內(nèi)存達(dá)到峰值內(nèi)存的百分比,即(used_memory/ used_memory_peak) *100%
used_memory_peak_perc:99.97%
# Redis為了維護(hù)數(shù)據(jù)集的內(nèi)部機(jī)制所需的內(nèi)存開(kāi)銷,包括所有客戶端輸出緩沖區(qū)、查詢緩沖區(qū)、AOF重寫(xiě)緩沖區(qū)和主從復(fù)制的backlog
used_memory_overhead:44082040
# Redis服務(wù)器啟動(dòng)時(shí)消耗的內(nèi)存
used_memory_startup:510704
# 數(shù)據(jù)占用的內(nèi)存大小,即used_memory-used_memory_overhead
used_memory_dataset:110190760
# 數(shù)據(jù)占用的內(nèi)存大小的百分比,100%*(used_memory_dataset/(used_memory-used_memory_startup))
used_memory_dataset_perc:71.66%
allocator_allocated:154256264
allocator_active:154550272
allocator_resident:159731712
# 整個(gè)系統(tǒng)內(nèi)存
total_system_memory:1044770816
# 以更直觀的格式顯示整個(gè)系統(tǒng)內(nèi)存
total_system_memory_human:996.37M
# Lua腳本存儲(chǔ)占用的內(nèi)存
used_memory_lua:37888
# 以更直觀的格式顯示Lua腳本存儲(chǔ)占用的內(nèi)存
used_memory_lua_human:37.00K
# Redis實(shí)例的最大內(nèi)存配置
maxmemory:0
# 以更直觀的格式顯示Redis實(shí)例的最大內(nèi)存配置
maxmemory_human:0B
# 當(dāng)達(dá)到maxmemory時(shí)的淘汰策略
maxmemory_policy:noeviction
allocator_frag_ratio:1.00
allocator_frag_bytes:294008
allocator_rss_ratio:1.03
allocator_rss_bytes:5181440
rss_overhead_ratio:1.01
rss_overhead_bytes:880640
# 碎片率,used_memory_rss/ used_memory
mem_fragmentation_ratio:1.04
mem_fragmentation_bytes:6422528
# 在編譯時(shí)指定的, Redis 所使用的內(nèi)存分配器??梢允?libc 、 jemalloc 或者 tcmalloc 。
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence,RDB 持久化和 AOF 持久化有關(guān)信息
# 一個(gè)標(biāo)志值,記錄了服務(wù)器是否正在載入持久化文件
loading:0
# 距離最近一次成功創(chuàng)建持久化文件之后,經(jīng)過(guò)了多少秒
rdb_changes_since_last_save:3813014
# 一個(gè)標(biāo)志值,記錄了服務(wù)器是否正在創(chuàng)建 RDB 文件
rdb_bgsave_in_progress:0
# 最近一次成功創(chuàng)建 RDB 文件的 UNIX 時(shí)間戳
rdb_last_save_time:1570002708
# 一個(gè)標(biāo)志值,記錄了最近一次創(chuàng)建 RDB 文件的結(jié)果是成功還是失敗
rdb_last_bgsave_status:ok
# 記錄了最近一次創(chuàng)建 RDB 文件耗費(fèi)的秒數(shù)
rdb_last_bgsave_time_sec:-1
# 如果服務(wù)器正在創(chuàng)建 RDB 文件,那么這個(gè)域記錄的就是當(dāng)前的創(chuàng)建操作已經(jīng)耗費(fèi)的秒數(shù)
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
# 一個(gè)標(biāo)志值,記錄了 AOF 是否處于打開(kāi)狀態(tài)
aof_enabled:0
# 一個(gè)標(biāo)志值,記錄了服務(wù)器是否正在創(chuàng)建 AOF 文件
aof_rewrite_in_progress:0
# 一個(gè)標(biāo)志值,記錄了在 RDB 文件創(chuàng)建完畢之后,是否需要執(zhí)行預(yù)約的 AOF 重寫(xiě)操作
aof_rewrite_scheduled:0
# 最近一次創(chuàng)建 AOF 文件耗費(fèi)的時(shí)長(zhǎng)
aof_last_rewrite_time_sec:-1
# 如果服務(wù)器正在創(chuàng)建 AOF 文件,那么這個(gè)域記錄的就是當(dāng)前的創(chuàng)建操作已經(jīng)耗費(fèi)的秒數(shù)
aof_current_rewrite_time_sec:-1
# 一個(gè)標(biāo)志值,記錄了最近一次創(chuàng)建 AOF 文件的結(jié)果是成功還是失敗
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats,一般統(tǒng)計(jì)信息
# 服務(wù)器已接受的連接請(qǐng)求數(shù)量
total_connections_received:96
# 服務(wù)器已執(zhí)行的命令數(shù)量
total_commands_processed:8700000
# 服務(wù)器每秒鐘執(zhí)行的命令數(shù)量
instantaneous_ops_per_sec:11
total_net_input_bytes:710523330
total_net_output_bytes:109073206
instantaneous_input_kbps:1.04
instantaneous_output_kbps:0.32
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
# 因?yàn)檫^(guò)期而被自動(dòng)刪除的數(shù)據(jù)庫(kù)鍵數(shù)量
expired_keys:13569
expired_stale_perc:0.00
expired_time_cap_reached_count:0
#  因?yàn)樽畲髢?nèi)存容量限制而被驅(qū)逐(evict)的鍵數(shù)量
evicted_keys:0
# 查找數(shù)據(jù)庫(kù)鍵成功的次數(shù)
keyspace_hits:1911035
# 查找數(shù)據(jù)庫(kù)鍵失敗的次數(shù)
keyspace_misses:749427
# 目前被訂閱的頻道數(shù)量
pubsub_channels:0
# 目前被訂閱的模式數(shù)量
pubsub_patterns:0
# 最近一次 fork() 操作耗費(fèi)的毫秒數(shù)
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication,主/從復(fù)制信息
# 服務(wù)器角色
role:master
# 已連接的從服務(wù)器數(shù)量
connected_slaves:0
master_replid:33f9e49948d61df7fa02e315ecff02bbc3b2c9aa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:925.48
used_cpu_user:3467.41
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=876234,expires=3,avg_ttl=204565882

2. CacheController接口代碼

/**
 * Redis緩存監(jiān)控Controller接口
 * 
 * @author csp
 */
@RestController
@RequestMapping("/monitor/cache")
public class CacheController
{
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")// 自定義權(quán)限注解
    @GetMapping()
    public AjaxResult getInfo() throws Exception
    {
        // 獲取redis緩存完整信息
        //Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info();
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());

        // 獲取redis緩存命令統(tǒng)計(jì)信息
        //Properties commandStats = redisTemplate.getRequiredConnectionFactory().getConnection().info("commandstats");
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));

        // 獲取redis緩存中可用鍵Key的總數(shù)
        //Long dbSize = redisTemplate.getRequiredConnectionFactory().getConnection().dbSize();
        Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());

        Map<String, Object> result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);

        List<Map<String, String>> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map<String, String> data = new HashMap<>(2);
            String property = commandStats.getProperty(key);
            data.put("name", StringUtils.removeStart(key, "cmdstat_"));
            data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
            pieList.add(data);
        });
        result.put("commandStats", pieList);

        return AjaxResult.success(result);
    }
}

其中自定義權(quán)限注解@PreAuthorize("@ss.hasPermi('monitor:cache:list')")是基于Spring Security做的權(quán)限控制,AjaxResult返回結(jié)果對(duì)象是自定義的,二者可不參考~

到此這篇關(guān)于SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的基本操作的文章就介紹到這了,更多相關(guān)SpringBoot RedisTemplate緩存信息監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論