我有redis replication
与sentinel
架构师:
5台服务器:.1
,.2
,.3
,.4
和.5
.1
:nodejs app,redis master,redis sentinel .2
:nodejs app,redis slave .3
:nodejs app,redis slave .4
:redis sentinel .5
:redis sentinel Sentinel处理故障转移按预期方式。
redis-cli -h x.y.z.5 -p 26379 sentinel get-master-addr-by-name mymaster
始终返回.1
服务器。
我的代码连接到此复制(我使用ioredis):
let sentinels = [
{ host: process.env.REDIS_SENTINEL_1, port: process.env.REDIS_PORT },
{ host: process.env.REDIS_SENTINEL_2, port: process.env.REDIS_PORT },
{ host: process.env.REDIS_SENTINEL_3, port: process.env.REDIS_PORT }
]
store = new Redis({
name: 'mymaster',
sentinels: sentinels
})
store.on('ready', () => {
store.set('foo', new Date())
store.get('foo', function (err, result) {
console.log('redis: get foo: ', result)
});
});
然后,.1
nodejs app运行正常,但在.2
和.3
中,此日志发生错误:
events.js:161
throw er; // Unhandled 'error' event
^
Error: READONLY You can't write against a read only slave.
P / s:我的redis服务器在服务器IP和127.0.0.1
都绑定。因为,在没有127.0.0.1
的情况下,我遇到了这个错误:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
所以,我想,我的应用没有通过redis server
连接到sentinel
,但它直接与localhost
连接。
任何帮助都很合适!
更新:我打开DEBUG=ioredis:*
时的日志:
2|MyApp | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis status[x.y.z.5:26379]: connecting -> connect
2|MyApp | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis status[x.y.z.5:26379]: connect -> ready
2|MyApp | Wed, 15 Mar 2017 12:58:42 GMT ioredis:connection send 1 commands in offline queue
2|MyApp | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis write command[0] -> sentinel(get-master-addr-by-name,mymaster)
2|MyApp | events.js:161
2|MyApp | throw er; // Unhandled 'error' event
2|MyApp | ^
2|MyApp | Error: READONLY You can't write against a read only slave.
2|MyApp | at JavascriptReplyParser._parseResult (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:43:16)
2|MyApp | at JavascriptReplyParser.try_parsing (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:114:21)
2|MyApp | at JavascriptReplyParser.run (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:126:22)
2|MyApp | at JavascriptReplyParser.execute (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:107:10)
2|MyApp | at Socket.<anonymous> (/home/demo/demo_api/source/node_modules/redis/index.js:131:27)
2|MyApp | at emitOne (events.js:96:13)
2|MyApp | at Socket.emit (events.js:189:7)
2|MyApp | at readableAddChunk (_stream_readable.js:176:18)
2|MyApp | at Socket.Readable.push (_stream_readable.js:134:10)
2|MyApp | at TCP.onread (net.js:551:20)
2|MyApp | [nodemon] app crashed - waiting for file changes before starting...
2|MyApp | sentinel production
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[localhost:6379]: [empty] -> connecting
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[x.y.z.5:26379]: [empty] -> connecting
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis queue command[0] -> sentinel(get-master-addr-by-name,mymaster)
2|MyApp | sentinel production
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[localhost:6379]: [empty] -> connecting
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[x.y.z.5:26379]: [empty] -> connecting
2|MyApp | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis queue command[0] -> sentinel(get-master-addr-by-name,mymaster)
更新2:还有一件事,我在我的本地机器上使用production
环境运行应用程序,一切都像魅力一样!
答案 0 :(得分:0)
通过连接到&#34; localhost&#34;,。2和.3正在与他们的本地只读保存进行通信。 你没有通过哨兵和#34;连接。您连接到sentinel,获取主IP,然后连接到该IP和端口。出于某种原因,您尝试连接到localhost,而不是由sentinel报告的IP。至于为什么,我不能说,因为您的问题中没有这些信息。
Sentinel返回一个IP地址,而不是&#34; localhost&#34;,所以你没有使用来自sentinel的返回,而是在某处编码使用&#34; localhost&#34;,这将产生你的行为报告。您需要找到并修复它。
答案 1 :(得分:0)
是的,终于找到了。
当旧代码使用redis模块并尝试直接连接到localhost时,我的错误。
将其替换为ioredis实例,它就像魅力一样:)