RedisTemplate执行lua脚本,集群模式下报错解决

2020-04-12 20:45:26 浏览数 (1)

redis集群配置:

在使用spring的RedisTemplate执行lua脚本时,报错EvalSha is not supported in cluster environment,不支持cluster。

代码:

代码语言:javascript复制
 @Test
     public void test4() throws Exception {
         DefaultRedisScript script = new DefaultRedisScript();
         script.setScriptSource(new StaticScriptSource("redis.call('SET', KEYS[1], ARGV[1]); return ARGV[1]"));
         script.setResultType(String.class);
         Object result = redisTemplate.execute(script, Arrays.asList("test_key1"), Arrays.asList("hello,key1"));
         System.out.println(result);
     }
 报错信息:
org.springframework.dao.InvalidDataAccessApiUsageException: EvalSha is not supported in cluster environment.
 
     at org.springframework.data.redis.connection.jedis.JedisClusterConnection.evalSha(JedisClusterConnection.java:3561)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:498)
     at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:57)
     at com.sun.proxy.$Proxy136.evalSha(Unknown Source)
     at org.springframework.data.redis.core.script.DefaultScriptExecutor.eval(DefaultScriptExecutor.java:81)
     at org.springframework.data.redis.core.script.DefaultScriptExecutor$1.doInRedis(DefaultScriptExecutor.java:71)
     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207)
     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)
     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:157)
     at org.springframework.data.redis.core.script.DefaultScriptExecutor.execute(DefaultScriptExecutor.java:60)
     at org.springframework.data.redis.core.script.DefaultScriptExecutor.execute(DefaultScriptExecutor.java:54)
     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:300)
     at com.wwwarehouse.xdw.contractcenter.zzz.RedisTest.test4(RedisTest.java:70)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:498)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
     at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
 

但是redis是支持lua脚本的,只要拿到原redis的connection对象,通过connection去执行即可:

代码语言:javascript复制
 @Test
     public void test5() throws Exception {
         List<String> keys = new ArrayList<String>();
         keys.add("test_key1");
         List<String> args = new ArrayList<String>();
         args.add("hello,key1");
         String LUA = "redis.call('SET', KEYS[1], ARGV[1]); return ARGV[1]";
         //spring自带的执行脚本方法中,集群模式直接抛出不支持执行脚本异常,此处拿到原redis的connection执行脚本
         String result = (String)redisTemplate.execute(new RedisCallback<String>() {
             public String doInRedis(RedisConnection connection) throws DataAccessException {
                 Object nativeConnection = connection.getNativeConnection();
                 // 集群模式和单点模式虽然执行脚本的方法一样,但是没有共同的接口,所以只能分开执行
                 // 集群
                 if (nativeConnection instanceof JedisCluster) {
                     return (String) ((JedisCluster) nativeConnection).eval(LUA, keys, args);
                 }
 
                 // 单点
                 else if (nativeConnection instanceof Jedis) {
                     return (String) ((Jedis) nativeConnection).eval(LUA, keys, args);
                 }
                 return null;
             }
         });
         System.out.println(result);
     }
 输出: hello,key1

0 人点赞