JMeter对接Redis(二)

背景:接上一篇JMeter对接Redis(一)

环境:Win7 + JMeter5.2.1 + Redis5.0集群

目录

3.连接Redis集群

1)引入依赖jar包

2)编码

3)打包

4)脚本


3.连接Redis集群

前一篇介绍的两种方法,在连接Redis单机时,是ok的。但当Redis为集群时,会出现部分key值查询不出来value的情况。这是由于Jedis仅适用于Redis,要对接Redis集群,则需要用JedisCluster。

但网上还没有发现现成插件,尝试利用BeanShell脚本实现时,BeanShell不支持Java泛型。所以,这里只能自己开发jar包。

1)引入依赖jar包

划重点:jar包一定要版本匹配。这里被坑惨了。。。看到控制台一直报这样的错,定位了好久。

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval    Sourced file: inline evaluation of: ......

WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ......

分别使用:jedis-3.2.0.jar 和 commons-pool2-2.8.0.jar。下载:https://mvnrepository.com/tags/maven

2)编码

package com.jmeter;


import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;


public class JmeterRedisClusterUtil{

    private JedisCluster jedisCluster = null;
    private Integer maxTotal = 60000;        // 最大连接数    
    private Integer maxIdle = 1000;            // 最大空闲数
    private Integer maxWaitMillis = 3000;       // 超时时间

    public void getRedisCluster(String clusterNodes, String password){
	//分割出集群节点
        String[] cNodes = clusterNodes.split(",");
	Set<HostAndPort> nodes = new HashSet<>();
	for (String node : cNodes) {
	    String[] ipAndPort = node.split(":");
	    nodes.add(new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1])));
	}
	
        // 配置连接池
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal); 
	jedisPoolConfig.setMaxIdle(maxIdle);
	jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        
        // 连接Redis集群
	jedisCluster = new JedisCluster(nodes, 3000, 3000, 5, password, jedisPoolConfig);
    }

    public String getValue(String key){
        return jedisCluster.get(key);
    }

    public static void main(String[] args){
    }
}

3)打包

如何打包的问题,可以问一下度娘。例如,可以参考:https://blog.csdn.net/qingfengmuzhu1993/article/details/80284739

4)脚本

a.引入上述步骤的jar包。(引入包的方式详见第一篇中的介绍)

b.添加BeanShell取样器,脚本如下:

举个栗子,集群IP和端口分别为: 172.168.0.2:6002; 172.168.0.3:6003; 172.168.0.4:6004

import com.jmeter.JmeterRedisClusterUtil;

//Redis信息
String nodes = "172.168.0.2:6002,172.168.0.3:6003,172.168.0.4:6004"
String password = "123456";
String key = "aaa";

//连接Redis
JmeterRedisClusterUtil jmeterRedisClusterUtil = new JmeterRedisClusterUtil();
try{
    jmeterRedisClusterUtil.getRedisCluster(nodes, password);
}catch(Throwable e){
    log.error("error: " + e);
    throw e;
}

//获取数据
String value = jmeterRedisClusterUtil.getValue(key);
log.info("value: " + value);

附:参考资料:

https://blog.csdn.net/zgz15515397650/article/details/84939987 -- Spring boo项目通过JedisCluster整合redis集群(redis有密码)

https://blog.csdn.net/lelemom/article/details/83753099 -- Jmeter Redis插件开发 -- 读写数据

https://blog.csdn.net/ninisui/article/details/80531577 -- jedis处理redis cluster集群的密码问题

https://www.cnblogs.com/moxiaotao/p/10069725.html -- JedisCluster链接Redis集群

https://www.cnblogs.com/snowstar123/p/5696052.html -- Jedis处理redis cluster集群密码问题

https://www.cnblogs.com/c-xiaohai/p/8376364.html -- Jedis、jedisCluster的使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章