事件:原本lua脚本读的还好好的,下午改了读取方式后就报错:
Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException:
ERR Error compiling script (new function): user_script:1: 'end' expected near '<eof>'
解决过程:反复试了好几次,用测试类测试本地redis的服务是否正常后,发现redis服务是好的
根据报的错,可以看出是lua脚本出的问题,于是把读取到的lua脚本打印出来后发现:
[2020-04-08 21:27:35,788]-[INFO]-[main]-[com.util.ReadConfigsPathUtil:57]-luaScript:local function get_next_seq() local key = tostring(KEYS[1]) local incr_amoutt = tonumber(KEYS[2]) local seq = tonumber(KEYS[3]) local month_in_seconds = 24 * 60 * 60 * 30 if (1 == redis.call(‘setnx’, key, seq)) then redis.call(‘expire’, key, month_in_seconds) return seq else local nextSeq = redis.call(‘incrby’, key, incr_amoutt) return nextSeq endendreturn get_next_seq()
解决方法:可以发现最后一行出现了”endendreturn “相连,所以自然是错误的,于是修改后:在每读一行之后都回车换行,完美解决问题!!!
类似问题:出现问题之后我去查了同僚们也有类似的问题,他们是出在配置文件配置redis时,超时时间写的是0,可以改成1000或更多毫秒
spring.redis.pool.timeout= 2000ms
另外还有是由于脚本中有中文的问题,删掉后就好了!!!
还有:在项目中配置文件中,连接redis,配置了密码,密码错误,或者配置文件中没有写密码,就会报错,可以考虑取消密码或者修改配置文件,添加密码。
在springboot项目中原本用的ClassPathResource 读取的配置文件,但是考虑到在生产环境时配置文件或者脚本文件需要单独拿出来,但是放在resouce目录下打包的时候就会打进去,无法解耦,所以在根目录下建了config目录,把脚本放在跟目录下:
读取lua脚本的方法:
package com.test.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.DefaultScriptExecutor;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.List;
@Component
public class RedisUtil {
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
private static StringRedisTemplate redisStringTemplate;
private static RedisScript<Long> redisScript;
private static DefaultScriptExecutor<String> scriptExecutor;
private RedisUtil(StringRedisTemplate template) throws IOException {
RedisUtil.redisStringTemplate = template;
// 初始化lua脚本调用 的redisScript 和 scriptExecutor
//第一种办法:通过ClassPathResource读取resource下的lua脚本
// ClassPathResource luaResource = new ClassPathResource("luaScript/genSeq.lua");
// EncodedResource encRes = new EncodedResource(luaResource, "UTF-8");
// String luaString = FileCopyUtils.copyToString(encRes.getReader());
//第二种办法,读取跟目录下的config目录后再按行读取
String luaString= ReadConfigsPathUtil.readFileContent("luaScript/genSeq.lua");
// log.info("luaString:"+luaString);
redisScript = new DefaultRedisScript<>(luaString, Long.class);
scriptExecutor = new DefaultScriptExecutor<>(redisStringTemplate);
}
public static Long getBusiSeq(List<String> Busilist) {
Long seqFromRedis = scriptExecutor.execute(redisScript, Busilist);
return seqFromRedis;
}
}
方法:ReadConfigsPathUtil.readFileContent(“luaScript/genSeq.lua”)
package com.test.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class ReadConfigsPathUtil {
private static final Logger log = LoggerFactory.getLogger(ReadConfigsPathUtil.class);
private ReadConfigsPathUtil() {}
private static Properties properties = null;
public static String getPropertiesPath(String luaScriptPath) {
String sysPath=getrelativePath();
String osType=System.getProperties().getProperty("os.name").toLowerCase();
log.info("sysPath:"+sysPath);
String filepath="";
if(osType.startsWith("windows")) {
filepath=new StringBuffer(sysPath).append(File.separator)
.append("config").append(File.separator).append(luaScriptPath).toString();
log.info("filepath:"+filepath);
}else if(osType.startsWith("linux")) {//启动脚本在哪个路径,sysPath的值就是当前路径的pwd的值
int len=sysPath.length();
int lenBin=("/bin").length();//我的启动脚本在bin目录下
sysPath=sysPath.substring(0,(len-lenBin));
filepath=new StringBuffer(sysPath).append(File.separator)
.append("config").append(File.separator).append(luaScriptPath).toString();
log.info("filepath:"+filepath);
}
return filepath;
}
public static String getrelativePath() {
return System.getProperty("user.dir");
}
public static String readFileContent(String luaScriptPath) {
String filename=getPropertiesPath(luaScriptPath);
File file = new File(filename);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
sbf.append("\r\n");//此处必须换行,这是出现RedisCommandExecutionException异常的原因
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return sbf.toString();
}
}
与君共勉!!!也欢迎留言交流哦~~~