SpringBoot服务获取Redis信息

利用ReatTemplate及方法获取Redis服务相关信息

@RestController
public class RedisCacheController {
    @Autowired
    private RedisTemplate redisTemplate;

  
    @GetMapping("/monitor/cache")
    public Ret getInfo() throws Exception {
        Properties info = (Properties) redisTemplate.execute((RedisCallback) connection -> connection.info());
        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback) connection -> connection.dbSize());

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

        List> pieList = new ArrayList<>();
        commandStats.stringPropertyNames().forEach(key -> {
            Map 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 Ret.success(result);
    }
} 
  
{
    "msg": "操作成功",
    "code": 200,
    "data": {
        "commandStats": [
            {
                "name": "scan",
                "value": "23"
            },
            {
                "name": "set",
                "value": "9051"
            },
            {
                "name": "del",
                "value": "7796"
            },
            {
                "name": "select",
                "value": "284"
            },
            {
                "name": "info",
                "value": "1855"
            },
            {
                "name": "auth",
                "value": "22020"
            },
            {
                "name": "exec",
                "value": "3"
            },
            {
                "name": "ttl",
                "value": "40"
            },
            {
                "name": "get",
                "value": "751353"
            },
            {
                "name": "unlink",
                "value": "150"
            },
            {
                "name": "dbsize",
                "value": "916"
            },
            {
                "name": "type",
                "value": "40"
            },
            {
                "name": "setex",
                "value": "18895"
            },
            {
                "name": "multi",
                "value": "3"
            },
            {
                "name": "ping",
                "value": "84"
            },
            {
                "name": "keys",
                "value": "2170"
            }
        ],
        "info": {
            "aof_rewrite_scheduled": "0",
            "number_of_cached_scripts": "0",
            "mem_clients_slaves": "0",
            "keyspace_misses": "4501",
            "mem_allocator": "jemalloc-5.1.0",
            "multiplexing_api": "epoll",
            "aof_last_write_status": "ok",
            "used_memory_peak_perc": "62.82%",
            "run_id": "48e1f18af1cdd4bac634f4c046b220a33b2668d9",
            "uptime_in_seconds": "6982491",
            "maxmemory": "0",
            "active_defrag_hits": "0",
            "atomicvar_api": "atomic-builtin",
            "total_system_memory_human": "3.48G",
            "gcc_version": "4.8.5",
            "second_repl_offset": "-1",
            "arch_bits": "64",
            "master_repl_offset": "0",
            "instantaneous_output_kbps": "0.00",
            "total_commands_processed": "814682",
            "repl_backlog_size": "1048576",
            "sync_full": "0",
            "sync_partial_err": "0",
            "aof_last_cow_size": "0",
            "configured_hz": "10",
            "used_memory_startup": "791440",
            "mem_clients_normal": "49694",
            "used_cpu_user": "4095.380630",
            "aof_enabled": "0",
            "redis_mode": "standalone",
            "rdb_changes_since_last_save": "1",
            "used_memory_lua": "37888",
            "redis_build_id": "eac856754d7fb32",
            "connected_slaves": "0",
            "expired_keys": "11564",
            "used_memory": "1217696",
            "active_defrag_misses": "0",
            "active_defrag_key_misses": "0",
            "used_memory_rss": "8663040",
            "rdb_last_bgsave_status": "ok",
            "process_id": "1591",
            "rejected_connections": "0",
            "master_replid": "b08f981e483ff1503d0046e042545ba8f1203b12",
            "used_memory_rss_human": "8.26M",
            "used_memory_scripts": "0",
            "rdb_bgsave_in_progress": "0",
            "rdb_last_cow_size": "532480",
            "cluster_enabled": "0",
            "tcp_port": "6379",
            "os": "Linux 4.19.91-25.al7.x86_64 x86_64",
            "mem_replication_backlog": "0",
            "aof_current_rewrite_time_sec": "-1",
            "allocator_rss_ratio": "2.41",
            "used_memory_overhead": "862086",
            "repl_backlog_histlen": "0",
            "slave_expires_tracked_keys": "0",
            "used_cpu_sys": "2461.753645",
            "role": "master",
            "used_memory_dataset_perc": "83.43%",
            "expired_stale_perc": "0.00",
            "allocator_frag_ratio": "1.39",
            "client_recent_max_input_buffer": "2",
            "aof_last_bgrewrite_status": "ok",
            "used_memory_peak_human": "1.85M",
            "hz": "10",
            "used_memory_human": "1.16M",
            "maxmemory_policy": "noeviction",
            "rss_overhead_ratio": "2.09",
            "mem_fragmentation_ratio": "7.37",
            "rdb_last_bgsave_time_sec": "0",
            "allocator_allocated": "1234544",
            "used_memory_dataset": "355610",
            "blocked_clients": "0",
            "used_cpu_sys_children": "6.147227",
            "repl_backlog_active": "0",
            "used_cpu_user_children": "10.364512",
            "sync_partial_ok": "0",
            "aof_rewrite_in_progress": "0",
            "redis_git_sha1": "00000000",
            "rdb_current_bgsave_time_sec": "-1",
            "active_defrag_running": "0",
            "aof_last_rewrite_time_sec": "-1",
            "master_replid2": "0000000000000000000000000000000000000000",
            "config_file": "/www/server/redis/redis.conf",
            "lazyfree_pending_objects": "0",
            "used_memory_scripts_human": "0B",
            "loading": "0",
            "allocator_rss_bytes": "2420736",
            "pubsub_channels": "0",
            "db1": "keys=90,expires=0,avg_ttl=0",
            "used_memory_lua_human": "37.00K",
            "db0": "keys=204,expires=83,avg_ttl=38845722",
            "active_defrag_key_hits": "0",
            "allocator_frag_bytes": "481680",
            "migrate_cached_sockets": "0",
            "redis_git_dirty": "0",
            "total_system_memory": "3738521600",
            "mem_aof_buffer": "0",
            "rss_overhead_bytes": "4526080",
            "client_recent_max_output_buffer": "0",
            "connected_clients": "1",
            "mem_fragmentation_bytes": "7487576",
            "lru_clock": "7899428",
            "pubsub_patterns": "0",
            "expired_time_cap_reached_count": "0",
            "repl_backlog_first_byte_offset": "0",
            "total_net_output_bytes": "1319784901",
            "executable": "/www/server/redis/src/redis-server",
            "evicted_keys": "0",
            "total_connections_received": "13012",
            "allocator_active": "1716224",
            "mem_not_counted_for_evict": "0",
            "redis_version": "5.0.8",
            "total_net_input_bytes": "67132355",
            "latest_fork_usec": "239",
            "maxmemory_human": "0B",
            "used_memory_peak": "1938504",
            "allocator_resident": "4136960",
            "keyspace_hits": "746932",
            "uptime_in_days": "80",
            "instantaneous_input_kbps": "0.00",
            "instantaneous_ops_per_sec": "0",
            "rdb_last_save_time": "1652066505"
        },
        "dbSize": 204
    }
}

你可能感兴趣的:(Redis,spring,boot,redis,java)