我的问题是我有两个容器:
我想让我的API自动重新连接到Mongo容器
当我的API启用而Mongo禁用时,我希望我的API重试,但是猫鼬选项均无效,重试事件重新连接均无效
我的API给我发送错误消息“错误:getaddrinfo ENOTFOUND mongo”,(mongo是我的图像的名称,因此这是逻辑,因为docker无法使用mongo解析它,因为mongo尚未启动)。
我无法获得猫鼬创建连接的重试事件。
某些代码:
连接事件监听器
mongoose.connection.on('connecting', () => {
statusConnection = 'connecting'
logger.info({
message: 'Database status',
statusConnection: 'Connecting'
})
})
mongoose.connection.on('connected', () => {
statusConnection = 'connected'
logger.info({
message: 'Database status',
statusConnection: 'connected'
})
})
mongoose.connection.on('disconnected', () => {
statusConnection = 'disconnected'
logger.warn({
message: 'Database status',
statusConnection: 'disconnected'
})
})
mongoose.connection.on('reconnected', () => {
statusConnection = 'reconnected'
logger.warn({
message: 'Database status',
statusConnection: 'reconnected'
})
})
mongoose.connection.on('reconnect', () => {
statusConnection = 'reconnect'
logger.warn({
message: 'Database status',
statusConnection: 'reconnect'
})
})
mongoose.connection.on('reconnectFailed', () => {
console.log('-----> reconnectFailed')
statusConnection = 'reconnectFailed'
logger.warn({
message: 'Database status',
statusConnection: 'reconnectFailed'
})
})
mongoose.connection.on('reconnectTries', () => {
console.log('-----> reconnectTries')
statusConnection = 'reconnectTries'
logger.warn({
message: 'Database status',
statusConnection: 'reconnectTries'
})
})
mongoose.connection.on('error', (error) => {
statusConnection = 'disconnected'
logger.error({
message: 'Database status',
statusConnection: 'disconnected',
error
})
// process.exit()
})
连接代码
let opts = {
username: configService.database.username,
password: configService.database.password,
host: configService.database.host,
port: configService.database.port,
database: configService.database.database,
autoReconnect: true,
reconnectTries: 10,
reconnectInterval: 500
}
await mongoose.connect(opts)
任何帮助
如果您在巴黎,为您提供啤酒:)