使用 Redisson 提供的 Redis 分布式锁时,编译没有问题,运行时报错 (-READONLY You can’t write against a read only replica):

10月 11, 2020 2:18:18 上午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springMVC] in context with path [] threw exception [Request processing failed; nested exception is org.redisson.client.RedisException: ERR Error running script (call to f_9401052d872adfd0179ef8c8e8c028512707629a): @user_script:1: @user_script: 1: -READONLY You can't write against a read only replica.   . channel: [id: 0xe42668b2, L:/192.168.186.1:52025 - R:/192.168.186.128:6379] command: (EVAL), params: [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pe..., 1, upload-lock-redis, 30000, d3df9050-1988-4447-afd0-f9114c20f69b:33]] with root cause
org.redisson.client.RedisException: ERR Error running script (call to f_9401052d872adfd0179ef8c8e8c028512707629a): @user_script:1: @user_script: 1: -READONLY You can't write against a read only replica.   . channel: [id: 0xe42668b2, L:/192.168.186.1:52025 - R:/192.168.186.128:6379] command: (EVAL), params: [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pe..., 1, upload-lock-redis, 30000, d3df9050-1988-4447-afd0-f9114c20f69b:33]

然后执行 Linux 服务器的 redis-cli,然后执行 set 命令,发现报错:

(error) READONLY You can't write against a read only replica.

由此可见没有写的权限,然后执行 info replicatio 发现:

role:slave
...

所以,此时它是一个从库并且主库并没有上线,只有一个 Redis 库,而且 Redis 配置里的 replica-read-only 为 yes,导致从库没有写的权限。

只用一个 redis 库的情况下,把它设置为主库即可:

slaveof no one

再次运行程序,没有报错,问题解决。

想了解更多,欢迎关注我的微信公众号:Renda_Zhang

Logo

更多推荐