带有 express.js 和 rabbitmq 的微服务

您所在的位置:网站首页 端口9005 带有 express.js 和 rabbitmq 的微服务

带有 express.js 和 rabbitmq 的微服务

#带有 express.js 和 rabbitmq 的微服务| 来源: 网络整理| 查看: 265

这篇文章是为了简化并做一步一步的教程来制作一个微服务应用

在本教程中,我将使用

Node.js 作为运行环境。

Typescript 代替 Javascript 以获得更好的开发体验。

Express.js 为后端。

Rabbitmq 作为消息代理。

要找到最终代码,可以在Github上找到

在输入代码之前,为什么是微服务?

假设我们有服务,在一个_单体架构_中,您将只有一个存储库,并且您的所有服务都通过同一个实例进行交互,如果任何服务导致实例崩溃,整个应用程序都会崩溃。

虽然在_微服务架构_中,每个都将工作一个完整的单元并与其他服务交互,它使用像 Rabbitmq 这样的消息代理向其他服务发送消息。

现在考虑一个真正依赖于订单服务(发布者)的仓库服务(消费者),每当提交订单时,仓库都会收到通知并开始发货流程,在这个架构中,如果订单服务导致崩溃,仓库会获胜'不受影响并且会继续工作,即使在提交订单时仓库服务已经关闭,消息代理也会保留该消息,并且在仓库服务启动时,它会断言in知道该消息。

怎么实现?

首先,文件夹结构,考虑做一个叫online-ordering-microservices的目录,在[order,warehouse]里面有两个文件夹

[文件夹结构](https://res.cloudinary.com/practicaldev/image/fetch/s--P0zoRmBf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to- uploads.s3.amazonaws.com/uploads/articles/tbunkhwqoqezq2vrzm05.png)

.gitignore 文件忽略每个服务中的 node_modules

二,设置rabbitmq为消息代理

运行docker container run --name rabbitmq --detach -p 5672:5672 rabbitmq

这将从图像 rabbitmq 以分离模式运行一个容器,并公开端口 5672,这是 rabbitmq 的默认端口,并为该容器命名 rabbitmq

第三,设置各项服务。

运行npm install express @types/express amqplib @types/amqplib以安装必要的依赖项。

订单/index.ts

import amqplib, { Channel, Connection } from 'amqplib' import express, { Request, Response } from 'express' const app = express() // parse the request body app.use(express.json()) // port where the service will run const PORT = 9005 // rabbitmq to be global variables let channel: Channel, connection: Connection connect() // connect to rabbitmq async function connect() { try { // rabbitmq default port is 5672 const amqpServer = 'amqp://localhost:5672' connection = await amqplib.connect(amqpServer) channel = await connection.createChannel() // make sure that the order channel is created, if not this statement will create it await channel.assertQueue('order') } catch (error) { console.log(error) } } app.post('/orders', (req: Request, res: Response) => { const data = req.body // send a message to all the services connected to 'order' queue, add the date to differentiate between them channel.sendToQueue( 'order', Buffer.from( JSON.stringify({ ...data, date: new Date(), }), ), ) res.send('Order submitted') }) app.get('*', (req: Request, res: Response) => { res.status(404).send('Not found') }) app.listen(PORT, () => { console.log(`Server running on ${PORT}`) })

进入全屏模式 退出全屏模式

仓库/index.ts

import amqplib, { Channel, Connection } from 'amqplib' import express, { Request, Response } from 'express' const app = express() // parse the request body app.use(express.json()) // port where the service will run const PORT = 9005 // rabbitmq to be global variables let channel: Channel, connection: Connection connect() async function connect() { try { const amqpServer = 'amqp://localhost:5672' connection = await amqplib.connect(amqpServer) channel = await connection.createChannel() // consume all the orders that are not acknowledged await channel.consume('order', (data) => { console.log(`Received ${Buffer.from(data!.content)}`) channel.ack(data!); }) } catch (error) { console.log(error) } } app.get('*', (req: Request, res: Response) => { res.status(404).send('Not found') }) app.listen(PORT, () => { console.log(`Server running on ${PORT}`) })

进入全屏模式 退出全屏模式

现在,如果您运行这两个应用程序并向http://localhost:9005/orders发出 post 请求,您将在仓库服务中收到一条消息,更重要的是,如果您在仓库服务未运行时发出请求运行并启动仓库服务,它将接收该消息,并且实际上会一直接收它,直到它确认它为止。

我希望你喜欢这个教程,并在另一个教程中见到你。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3