Jmeter连接redis获取数据,一直连不上报错。最后只能通过java代码连接测试,最后只能自己动手。
import redis.clients.jedis.*;import java.io.IOException;
import java.util.HashSet;
import java.util.Set;/*** 单机版的Jedis连接池的用法*/
public class RedisClient {public static Jedis GetRedisClient(String ip, int port, String password, int database) throws IOException {Integer maxTotal = 60000; // 最大连接数Integer maxIdle = 1000; // 最大空闲数Integer MinIdle = 1; //Integer maxWaitMillis = 3000; // 超时时间JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(maxTotal);poolConfig.setMaxIdle(maxIdle);poolConfig.setMinIdle(MinIdle);JedisPool jedisPool = new JedisPool(poolConfig, ip, port, 2000, password, database);// 从连接池中获取jedis对象Jedis jedis = jedisPool.getResource();return jedis;}/*** 集群的Jedis连接池的用法*/public static JedisCluster getRedisCluster(String clusterNodes, String password) {JedisCluster jedisCluster = null;Integer maxTotal = 60000; // 最大连接数Integer maxIdle = 1000; // 最大空闲数Integer maxWaitMillis = 3000;//分割出集群节点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);return jedisCluster;}}
在jmeter beanshell
import cn.oscar.common.RedisClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;String nodes = "IP";
String password = "passwd";
String key = "redis_key";Jedis testJedis = RedisClient.GetRedisClient(nodes,6379,password,0);value=testJedis.get(key);
testJedis.close();log.info("========redis返回值=============="+value.toString());