前几日接到一个需求,运维那边对redis进行了封装,原生的命令如:set、get、del等都不再适用,要用封装的命令操作redis,这类资料太少了,特此记录
定义java封装类采用jedis执行命令
@Component
public class RedisUtil {
//从YML配置文件中读取数据赋值到属性
//@Value("${spring.redis.you.host}")
private String host;
//@Value("${spring.redis.you.port}")
private int port;
//@Value("${spring.redis.you.password}")
private String password;
public List<String> execCommandRedisLine(Jedis jedis, String command, String... args) throws InvocationTargetException, IllegalAccessException {
Protocol.Command cmd = Protocol.Command.valueOf(command.toUpperCase());
Client client = jedis.getClient();
Method method = MethodUtils.getMatchingMethod(Client.class, "sendCommand", Protocol.Command.class, String[].class);
method.setAccessible(true);
method.invoke(client, cmd, args);
try {
List<String> respList = new ArrayList<>();
Object response = client.getOne();
if (response instanceof List) {
for (Object itemResp : ((List) response)) {
String string = new String((byte[]) itemResp);
respList.add(string);
}
return respList;
} else {
return Collections.singletonList(new String((byte[]) response));
}
} catch (JedisException e) {
e.printStackTrace();
return Collections.singletonList(e.getMessage());
}
}
public List<String> execRedisCommand(Jedis jedis, String command, String... args) throws Exception {
byte[] cmd = SafeEncoder.encode(command);
byte[][] bargs = new byte[args.length][];
for(int i = 0; i < args.length; ++i) {
bargs[i] = SafeEncoder.encode(args[i]);
}
Client client = jedis.getClient();
Field field= Connection.class.getDeclaredField("outputStream");
field.setAccessible(true);
Method[] methods = Protocol.class.getDeclaredMethods();
for (int j = 0; j < methods.length; j ++) {
methods[j].setAccessible(true);
if("sendCommand".equals(methods[j].getName())){
if(methods[j].getParameterTypes().length > 0){
if(methods[j].getParameterTypes()[1] == byte[].class){
methods[j].invoke(null, field.get(client), cmd, bargs);
break;
}
}
}
}
try {
List<String> respList = new ArrayList<>();
String response = client.getBulkReply();
respList.add(response);
return respList;
} catch (JedisException e) {
e.printStackTrace();
return Collections.singletonList(e.getMessage());
}
}
public Jedis getJedis(){
Jedis jedis = null;
try {
JedisPool jedisPool = new JedisPool(host ,port);
jedis = jedisPool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
return jedis;
}
public static void main(String[] args) {
Jedis jedis = null;
try {
//初始化Jedis连接池对象
JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
//从连接池子获取Jedis对象
jedis = jedisPool.getResource();
RedisUtil redisUtil = new RedisUtil();
String key1 = "name";
String value1 = "yes";
//第二个参数是自定义的redis命令,如:原生的set name 王八 自定义后 redisCommand_type set name 王八 set替换成redisCommand_type set ,redis必须是运维对redis有redisCommand_type set这命令进行了封装才可用
//List<String> setList = redisUtil.execRedisCommand(jedis, "redisCommand_type", "set", "name" , value);
//以下是原生写法
List<String> stringList = redisUtil.execRedisCommand(jedis, "set", key1, value1);
for (String s : stringList) {
if (s.equals("OK")){
//String isSave = jedis.get("isSave");
List<String> valueList = redisUtil.execRedisCommand(jedis, "get", key1);
System.out.println("value-->" + valueList.get(0));
} else {
System.out.println("value-->" + s);
}
}
String key2 = "type";
String value2 = "alamr";
List<String> keyList = redisUtil.execCommandRedisLine(jedis, "set", key2, value2);
for (String s : keyList) {
List<String> valueList = redisUtil.execRedisCommand(jedis, "get", key2);
System.out.println("value-->" + valueList.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
// 注意这里不是关闭连接,在 JedisPool 模式下,Jedis 会被归还给资源池。
if (jedis != null)
jedis.close();
}
}
}
执行mian方法
Connected to the target VM, address: '127.0.0.1:57410', transport: 'socket'
value-->王八
value-->王八蛋
Disconnected from the target VM, address: '127.0.0.1:57410', transport: 'socket'
Process finished with exit code 0
pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
这篇博客记录了在接到一个特殊需求后,如何在Java中使用jedis执行运维封装后的Redis自定义命令。文章详细介绍了定义Java封装类及执行命令的方法,并提及相关资源的稀缺性。

898

被折叠的 条评论
为什么被折叠?



