Dapr+NestJs編寫Pub及Sub裝飾器實戰(zhàn)示例

Dapr 是一個可移植的、事件驅(qū)動的運行時,它使任何開發(fā)人員能夠輕松構(gòu)建出彈性的、無狀態(tài)和有狀態(tài)的應(yīng)用程序,并可運行在云平臺或邊緣計算中,它同時也支持多種編程語言和開發(fā)框架。Dapr 確保開發(fā)人員專注于編寫業(yè)務(wù)邏輯,不必分神解決分布式系統(tǒng)難題,從而顯著提高了生產(chǎn)力。Dapr 降低了構(gòu)建微服務(wù)架構(gòu)類現(xiàn)代云原生應(yīng)用的門檻。
系列
本地使用 Docker Compose 與 Nestjs 快速構(gòu)建基于 Dapr 的 Redis 發(fā)布/訂閱分布式應(yīng)用
NodeJS 基于 Dapr 構(gòu)建云原生微服務(wù)應(yīng)用,從 0 到 1 快速上手指南
Dapr JavaScript SDK
用于在 JavaScript 和 TypeScript 中構(gòu)建 Dapr 應(yīng)用程序的客戶端庫。 該客戶端抽象了公共 Dapr API,例如服務(wù)到服務(wù)調(diào)用、狀態(tài)管理、發(fā)布/訂閱、Secret 等,并為構(gòu)建應(yīng)用程序提供了一個簡單、直觀的 API。
安裝
要開始使用 Javascript SDK,請從 NPM 安裝 Dapr JavaScript SDK 包:
npm install --save @dapr/dapr
?? dapr-client 現(xiàn)在已棄用。 請參閱#259 了解更多信息。
結(jié)構(gòu)
Dapr Javascript SDK 包含兩個主要組件:
- DaprServer: 管理所有 Dapr sidecar 到應(yīng)用程序的通信。
- DaprClient: 管理所有應(yīng)用程序到 Dapr sidecar 的通信。
上述通信可以配置為使用 gRPC 或 HTTP 協(xié)議。
實戰(zhàn)
創(chuàng)建一個小應(yīng)用程序來生成有關(guān)網(wǎng)站中用戶行為的統(tǒng)計信息。
Demo 源碼
準(zhǔn)備環(huán)境和項目結(jié)構(gòu)
npm install -g @nestjs/cli nest new api mv api nest-dapr cd nest-dapr nest generate app page-view npm install dapr-client wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
創(chuàng)建一個 decorators.ts 文件(apps/shared/decorators.ts),這樣所有微服務(wù)都可以從我們即將編寫的基礎(chǔ)架構(gòu)中受益。
注入 Dapr 賴項
注入 DaprClient 和 DaprServer,我們需要提供它們到 nest.js。
在 app.module.ts 中讓我們注冊 DaprClient:
providers: [
...
{
provide: DaprClient,
useValue: new DaprClient()
}
]
在 page-view.module.ts 中以同樣的方式添加 DaprServer:
providers: [
...
{
provide: DaprServer,
useValue: new DaprServer()
}
]
配置 Dapr 組件(rabbitMQ)
用 docker compose 啟動 rabbitmq:
version: '3.9'
services:
pubsub:
image: rabbitmq:3-management-alpine
container_name: 'pubsub'
ports:
- 5674:5672
- 15674:15672 #web port
我們還需要配置 Dapr 組件。在根文件夾中創(chuàng)建一個 component/pubsub.yml:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
namespace: default
spec:
type: pubsub.rabbitmq
version: v1
metadata:
- name: host
value: 'amqp://guest:guest@localhost:5674'
每個 Dapr 微服務(wù)都需要自己的 config。
api/dapr/config.yml:
apiVersion: dapr.io/v1alpha1 kind: Configuration metadata: name: api namespace: default
page-view/dapr/config.yml:
apiVersion: dapr.io/v1alpha1 kind: Configuration metadata: name: page-view namespace: default
API/Gateway 服務(wù)
在 app.controller.ts 中,我們將公開一個簡單的 API — /add-page-view。
@Post('/add-page-view')
async prderAdd(@Body() pageViewDto: PageViewDto): Promise<void> {
try {
console.log(pageViewDto);
await this.daprClient.pubsub.publish('pubsub', 'page-view-add', pageViewDto);
} catch (e) {
console.log(e);
}
}
內(nèi)部監(jiān)聽微服務(wù)
在我們將數(shù)據(jù)發(fā)布到隊列之后,我們需要監(jiān)聽它并調(diào)用它:
在 page-view.controller.ts 添加:
@DaprPubSubSubscribe('pubsub', 'add')
addPageView(data: PageViewDto) {
console.log(`addPageView executed with data: ${JSON.stringify(data)}`);
this.data.push(data);
}
注意我們現(xiàn)在需要創(chuàng)建的新裝飾器:@DaprPubSubscribe。
@DaprPubSubscribe 裝飾器
在 shared/decorators.ts 中:
import { INestApplication } from '@nestjs/common';
import { DaprServer } from 'dapr-client';
export type PubsubMap = {
[pubSubName: string]: {
topic: string;
target: any;
descriptor: PropertyDescriptor;
};
};
export const DAPR_PUB_SUB_MAP: PubsubMap = {};
export const DaprPubSubSubscribe = (
pubSubName: string,
topic: string,
): MethodDecorator => {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
DAPR_PUB_SUB_MAP[pubSubName] = {
topic,
target,
descriptor,
};
return descriptor;
};
};
export const useDaprPubSubListener = async (app: INestApplication) => {
const daprServer = app.get(DaprServer);
for (const pubSubName in DAPR_PUB_SUB_MAP) {
const item = DAPR_PUB_SUB_MAP[pubSubName];
console.log(
`Listening to the pubsub name - "${pubSubName}" on topic "${item.topic}"`,
);
await daprServer.pubsub.subscribe(
pubSubName,
item.topic,
async (data: any) => {
const targetClassImpl = app.get(item.target.constructor);
await targetClassImpl[item.descriptor.value.name](data);
},
);
}
};
運行應(yīng)用程序
運行:
docker-compose up -d # 啟動 rabbitmq npm run dapr:api:dev # 啟動 api/gateway npm run page-view:dev # 啟動內(nèi)部微服務(wù)監(jiān)聽 dapr rabbitmq 隊列
執(zhí)行請求:
curl --location --request POST 'http://localhost:7001/v1.0/invoke/api/method/statistics/add-page-view' \
--header 'Content-Type: application/json' \
--data-raw '{
"pageUrl" : "https://test.com/some-page",
"userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
}'以上就是Dapr+NestJs編寫Pub及Sub裝飾器實戰(zhàn)示例的詳細(xì)內(nèi)容,更多關(guān)于Dapr NestJs編寫Pub及Sub的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
nodejs連接mysql數(shù)據(jù)庫簡單封裝示例-mysql模塊
本篇文章主要介紹了nodejs連接mysql數(shù)據(jù)庫簡單封裝(mysql模塊),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Node.js中Mongodb數(shù)據(jù)庫操作方法(最新推薦)
MongoDB是一種基于分布式文件存儲的NoSQL數(shù)據(jù)庫,它允許存儲和檢索大量結(jié)構(gòu)化數(shù)據(jù),MongoDB的核心概念包括數(shù)據(jù)庫、集合和文檔,每個集合可以包含多個文檔,每個文檔是一個鍵值對的集合,本文介紹Node.js Mongodb數(shù)據(jù)庫操作方法,感興趣的朋友一起看看吧2024-12-12
Node.js使用MongoDB的ObjectId作為查詢條件的方法
這篇文章主要介紹了Node.js使用MongoDB的ObjectId作為查詢條件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
提升node.js中使用redis的性能遇到的問題及解決方法
本文中提到的node redis client采用的基于node-redis封裝的二方包,因此問題排查也基于node-redis這個模塊。接下來通過本文給大家分享提升node.js中使用redis的性能2018-10-10
node管理統(tǒng)計文件大小并顯示目錄磁盤空間狀態(tài)從零實現(xiàn)
這篇文章主要為大家介紹了node管理統(tǒng)計文件大小并顯示目錄磁盤空間狀態(tài)的從零實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Node.js?中使用fetch?按JSON格式發(fā)post請求的問題解析
最近在測試一個api,可以用curl命令直接訪問,指定header相關(guān)配置,request?body(JSON),成功后返回一個JSON,這篇文章主要介紹了Node.js?中使用fetch?按JSON格式發(fā)post請求,需要的朋友可以參考下2023-04-04
Node.js中的EventEmitter類使用小結(jié)
EventEmitter 是 Node.js 中的一個核心模塊,它提供了一種實現(xiàn)事件驅(qū)動編程的機(jī)制,它是一個基于觀察者模式的類,用于在應(yīng)用程序中處理事件和觸發(fā)事件,這篇文章主要介紹了Node.js中的EventEmitter類介紹,需要的朋友可以參考下2023-12-12

