To use jmeter to retrieve the value of the login token from Redis
Prepare
The jar package of jedis needs to be prepared and placed under the lib of the jmeter installation path.
Download the jar package: https://mvnrepository.com/artifact/redis.clients/jedis/3.1.0-rc2
Set connection parameters
Set the parameters for connecting to Redis in User Defined Variables (user-defined variables)
name | value | describe |
---|---|---|
redis-host | 127.0.0.1 | Connection host address |
redis-port | 6379 | The port number |
redis-password | password | |
redis-index | 0 | which library |
redis-string-key | student | the key to get the value |
BeanShell script
Create a BeanShell Sampler (BeanShell sampler)
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
String host = "${redis-host}"; //服务器地址
int port = ${redis-port}; //端口号
String password = "${redis-password}"; //redis密码
int index = ${redis-index}; //redis db,0选择第一个db0
String key = "${redis-string-key}"; //key值
Jedis jedis = new Jedis(host, port);
if(StringUtils.isNotBlank(password)){
jedis.auth(password);
}
jedis.select(index);
//取出值
String str= jedis.get(key);
log.info("======================="+str);
//截取token
String token = str.substring(12,str.indexOf(","));
log.info("---------"+token);
//给外部用
vars.put("token",token);