监控redis性能

17 篇文章 0 订阅

1.导言

     在开发过程中,为了提高系统的访问效率,我们会自己开发个缓存工具或使用第三方的缓存工具。笔者公司最近的开发中使用的是redis作为缓存工具。关于redis的介绍相信用过的读者都知道它的好处,这里不作介绍。这篇博客也不是关于redis是怎么使用,而是说一个如何去监控redis的性能,或者说是监控redis的一些主要的指标。毕竟在一些证券、银行的系统中,很关心的是系统的性能,包括redis,他们也会担心万一死了就坏事了。所以,这篇博客写一些代码,来监控redis的内存、CPU和连接数的情况

2.方法介绍

首先在项目中引入jedis-2.9.0.jar文件,然后JedisPool工具连接到自己的redi环境中。使用RedisTest的getRedisInfo方法,主可以获取redis的性能数据。读者可能对这些数据做处理,提取自己需要的部分数据。下面代码只是笔者处理数据取redis的内存、CPU和连接的数据。

  package com.owen.redis.monitor.test;

import java.text.DecimalFormat;

import redis.clients.jedis.Client;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Jedis;

public class RedisTest {

	public static void main(String[] args) {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMaxWaitMillis(-1);
		JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379, 10000, "root");
		RedisTest rt = new RedisTest();
		String info = rt.getRedisInfo(jedisPool);
		String[] infos = info.split("#");
		for(String inf : infos)
		{
			if(inf.contains("Clients"))
			{
				String[] clientInfo = inf.split("\n");
				for(String client : clientInfo)
				{
					String[] splitClient = client.split(":");
					String labelName = splitClient[0].trim();
					if(labelName.equals("connected_clients"))
					{
					
						System.out.println("**名称:"+splitClient[0]+"***数据:"+splitClient[1]);
					}
				}
				
			}else if(inf.contains("Memory"))
			{
				String[] clientInfo = inf.split("\n");
				double usedMemory = 0.0;
				for(String client : clientInfo)
				{
					String[] splitClient = client.split(":");
					String labelName = splitClient[0].trim();
				
					if(labelName.equals("used_memory"))
					{
					
						usedMemory = Double.valueOf(splitClient[1].trim());
						
						System.out.println("**内存**名称:"+splitClient[0]+"***数据:"+splitClient[1]+"****hh:"+usedMemory);
					}
					
					if(labelName.equals("maxmemory"))
					{
						DecimalFormat df = new DecimalFormat("######0.00");
						double maxMeneory = Double.valueOf(splitClient[1].trim());
						double pp = usedMemory/maxMeneory*100;
						String memoryRate =  df.format((Double.valueOf(usedMemory/maxMeneory)*100));
						System.out.println(pp+"*******hhh:"+maxMeneory +"***ooo:"+memoryRate);
						
					}
					
				}
			}else if(inf.contains("CPU"))
			{
				String[] clientInfo = inf.split("\n");
				double count = 0.0;
				double sysCpu = 0.0;
				DecimalFormat df = new DecimalFormat("######0.00");
				for(String client : clientInfo)
				{
					
					String[] splitClient = client.split(":");
					String labelName = splitClient[0].trim();
					
					if(splitClient.length >1)
					{
						double num = Double.valueOf(splitClient[1]);
						count += num;
					}
					
					if(labelName.equals("used_cpu_sys"))
					{
	
						sysCpu = Double.valueOf(splitClient[1]);
					}
				}
				double cpuRate = 0.0;
				if(count != 0.0)
				{
					cpuRate = (sysCpu /count) * 100;
				}
				System.out.println("******cpu使用率:"+df.format(cpuRate));
			}
			{
				
			}
		}
		jedisPool.close();
		
		
	}
	// 获取redis 服务器信息
		public String getRedisInfo(JedisPool jedisPool) {

			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				Client client = jedis.getClient();
				client.info();
				String info = client.getBulkReply();
				//System.out.println(info);
				return info;
			} finally {
				// 返还到连接池
				jedis.close();
			}
		}
		
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值