背景:接上一篇JMeter對接Redis(一)
環境:Win7 + JMeter5.2.1 + Redis5.0集羣
目錄
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的使用