这些工作函数是 RabbitMQ 消费者,用于处理传入请求并回复响应。
我正在寻找有关如何在部署 Lambda 函数后保持这些辅助函数始终处于活动状态的指导。哪些配置或实践可以帮助实现这一目标?
// app.js
import e from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import 'dotenv/config.js';
import mongoose from 'mongoose';
import SwaggerRouter from './swagger.js';
import logger from './logger.js';
import StartUpRouter from './routes/user_founder.js'
import VCRouter from './routes/user_vc.js';
import SuperAdminRouter from './routes/superadmin.js';
import { handleVCListRequest, startTokenValidationWorker, startUserValidationWorker, startVCDataWorker } from './workers/messageQueueWorker.js';
import { startWorker } from './services/rabbitMQService.js';
import { processMessage } from './workers/emailWorker.js';
const app = e();
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(SwaggerRouter);
// ...other routes and middleware...
mongoose.connect(process.env.MONGO_URL, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {
app.listen(process.env.PORT || 4001);
logger.info({listening: `Server is Processing on ${process.env.PORT}.`});
logger.info({success: "Connected to AuthenticationService Database."});
console.log({listening: `Server is Processing on ${process.env.PORT}.`});
console.log({success: "Connected to Database."});
startWorker(processMessage).catch((error) => {
console.error('Error while sending email:', error.message);
});
startTokenValidationWorker().catch((error) => {
logger.error('Error starting token validation worker:', error.message);
console.error('Error starting token validation worker:', error.message);
});
// ...other worker functions...
handleVCListRequest().catch((error) => {
logger.error('Error starting vc list worker:', error.message)
console.error('Error starting vc list worker:', error.message);
});
})
.catch((err) => {
logger.error({error: err.message});
console.error({error: err});
});
export default app;
// lambda.js
import ServerlessHttp from "serverless-http";
import app from "./app.js";
export const handler = ServerlessHttp(app);
AWS Lambda 作为无服务器计算服务并不适合这里。
虽然您可以让 Lambda 函数保持温暖,但无服务器架构的本质和目的是不让服务保持 24/7 运行。
RabbitMQ 消费者应该始终监听消息,应该在 ECS、EC2 等使用服务器且可以 24/7 可用的服务上运行。