spring-boot redisTemplate execute script error: EvalSha is not supported in cluster environment
spring-boot redisTemplate execute script error: EvalSha is not supported in cluster environment
尝试在 spring-boot 项目中使用 redisTemplate 执行 lua 脚本。看起来 jedis 不支持为 redis 集群执行 lua 脚本...还有其他选择吗?谢谢!
redis 配置:
spring:
redis:
cluster:
nodes:
- 192.168.0.111:6390
- 192.168.0.111:6391
- 192.168.0.111:6392
代码:
@Component
public class Example {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
RedisScript<Boolean> script;
public boolean checkAndSet(String expectedValue, String newValue) {
return (boolean) redisTemplate.execute(script, singletonList("key1"), asList(expectedValue, newValue));
}
}
错误日志:
org.springframework.dao.InvalidDataAccessApiUsageException: EvalSha is not supported in cluster environment.
at org.springframework.data.redis.connection.jedis.JedisClusterConnection.evalSha(JedisClusterConnection.java:3568)
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.$Proxy237.evalSha(Unknown Source)
at org.springframework.data.redis.core.script.DefaultScriptExecutor.eval(DefaultScriptExecutor.java:81)
at org.springframework.data.redis.core.script.DefaultScriptExecutor.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)
切换到 Lettuce 驱动程序以在集群环境中使用 EVALSHA
。
Spring 引导 1.5.x 的配置可能如下所示:
@Bean
LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
Cluster clusterProperties = redisProperties.getCluster();
RedisClusterConfiguration config = new RedisClusterConfiguration(
clusterProperties.getNodes());
if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects());
}
return new LettuceConnectionFactory(config);
}
尝试在 spring-boot 项目中使用 redisTemplate 执行 lua 脚本。看起来 jedis 不支持为 redis 集群执行 lua 脚本...还有其他选择吗?谢谢!
redis 配置:
spring:
redis:
cluster:
nodes:
- 192.168.0.111:6390
- 192.168.0.111:6391
- 192.168.0.111:6392
代码:
@Component
public class Example {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
RedisScript<Boolean> script;
public boolean checkAndSet(String expectedValue, String newValue) {
return (boolean) redisTemplate.execute(script, singletonList("key1"), asList(expectedValue, newValue));
}
}
错误日志:
org.springframework.dao.InvalidDataAccessApiUsageException: EvalSha is not supported in cluster environment.
at org.springframework.data.redis.connection.jedis.JedisClusterConnection.evalSha(JedisClusterConnection.java:3568)
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.$Proxy237.evalSha(Unknown Source)
at org.springframework.data.redis.core.script.DefaultScriptExecutor.eval(DefaultScriptExecutor.java:81)
at org.springframework.data.redis.core.script.DefaultScriptExecutor.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)
切换到 Lettuce 驱动程序以在集群环境中使用 EVALSHA
。
Spring 引导 1.5.x 的配置可能如下所示:
@Bean
LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
Cluster clusterProperties = redisProperties.getCluster();
RedisClusterConfiguration config = new RedisClusterConfiguration(
clusterProperties.getNodes());
if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects());
}
return new LettuceConnectionFactory(config);
}