介紹
Yunfly
一款高性能 Node.js WEB 框架, 使用 Typescript
構建我們的應用。
使用 Koa2
做為 HTTP 底層框架, 使用 routing-controllers
、 typedi
來高效構建我們的 Node 應用。
Yunfly 在 Koa 框架之上提升了一個抽象級別, 但仍然支持 Koa 中間件。在此基礎之上, 提供了一套強大的插件系統(tǒng), 給開發(fā)者提供更強大更靈活的能力。
github地址:https://github.com/yunke-yunfly/yunflyjs
文檔地址:https://yunke-yunfly.github.io/doc.github.io/document/introduction/introduce
框架技術棧
Koa2
node.js http 框架, async await異步編程 參考文檔find-my-way
一款高性能的 http 路由器 參考文檔typescript
微軟開發(fā)的自由和開源的編程語言, 它是JavaScript的一個超集, 添加了可選的靜態(tài)類型和基于類的面向?qū)ο缶幊?參考文檔routing-controllers
使用裝飾器的方式來進行路由的開發(fā) 參考文檔typedi
: 依賴注入插件工具 參考文檔grpc
: 一個高性能、開源和通用的 RPC 框架 參考文檔winston
: javascript 的 log 日志插件 參考文檔
與社區(qū)框架差異
能力 | yunfly | eggjs | nestjs |
---|---|---|---|
Typescript | ? | ?[支持但不友好] | ? |
cluster | ? | ? | ? |
openapi | ? | ? | ? |
框架約束 | 部分約束 | 約束 | 自由 |
擴展模型 | 插件 | 插件 | 模塊 |
性能
yunfly 框架底層 web 庫為 koa, 路由開發(fā)模型庫為 routing-controllers, 路由命中庫為 find-my-way。
koa 對于寫業(yè)務來說性能是足夠優(yōu)異的,routing-controllers 使用裝飾器的方式來進行路由的開發(fā),對于開發(fā)者來說是很提效的。
框架剔除了低效的 koa-router 更換為高效的 find-my-way。框架未內(nèi)插件,開發(fā)者可以根據(jù)自己的需求定制插件。
性能壓測
以下性能測試為同一臺機器同樣的容器場景下壓測3分鐘得出的結果。
容器環(huán)境
1G1核 Docker 容器
hello world 場景
web框架 | qps | 備注 |
---|---|---|
yunfly | 6400 | 使用 koa 為底層庫 |
eggjs | 3950 | 使用 koa 為底層庫 |
nestjs | 2900 | 使用 express 為底層庫 |
nestjs | 7200 | 使用 fastify 為底層庫 |
1000 個路由場景
web框架 | qps | 備注 |
---|---|---|
yunfly | 6100 | 使用 koa 為底層庫 |
eggjs | 1680 | 使用 koa 為底層庫 |
nestjs | 2050 | 使用 express為底層庫 |
nestjs | 6550 | 使用 fastify為底層庫 |
以上壓測結果不同的機器得出的結果會略有不同。
開始使用
當前提供了2種快速上手模式
- 使用框架提供的腳手架快速初始化 詳細參考文檔
- 使用手動模式逐步搭建 詳細參考文檔
編寫一個簡單的Controller
import { Get, JsonController, BodyParam, Post, QueryParam } from '@yunflyjs/yunfly';
/**
* 測試案例controller
*
* @export
* @class TestController
*/
@JsonController('/example')
export default class ExampleController {
/**
* 簡單案例 - get
*
* @param {string} name 姓名
* @return {*} {string}
* @memberof ExampleController
*/
@Get('/simple/get')
simple(
@QueryParam('name') name: string,
): string {
return name || 'success';
}
/**
* 簡單案例 -post
*
* @param {string} name 姓名
* @return {*} {string}
* @memberof ExampleController
*/
@Post('/simple/post')
simple1(
@BodyParam('name') name: string,
): string {
return name || 'success';
}
}
- 訪問應用
http://127.0.0.1:3000/example/simple/get?name=xxx
當前支持的一些特性
支持多進程模型
若應用需要開啟node多進程,只需要在 config 中配置啟用即可,單多進程模型隨意切換
Cluster 配置
// src/config/config.default.ts
/**
* cluster config
*/
config.cluster = {
enable: true,
};
- 自定義啟動進程數(shù)
// src/config/config.default.ts
config.cluster = {
enable: true,
count: 4,
};
- 備注:在 docker 容器場景下,會優(yōu)先獲取容器分配的cpu核數(shù), 優(yōu)先級:
容器核數(shù) > config.cluster.count
隨意定制你的框架
yunfly web框架是由基礎包+一個個插件組合而成,框架自身提供了很多插件,支持開發(fā)者自定義插件。
備注:yunfly 的插件部分理念實現(xiàn)參考了eggjs的插件模型
開發(fā)者可以把常規(guī)插件+自定義插件打包成一個集合組裝成一個新的框架。
此處能力可以參考:Yunfly 框架開發(fā)
支持生成openapi
框架提供了輔助插件 routing-controllers-to-openapi
, 能把所有路由與Typescript代碼轉(zhuǎn)換為openapi, 進而你可以通過openapi生成接口文檔信息。
- 支持 typescript 生成 jsonschema
- 支持注釋(行內(nèi)注釋,代碼塊上方注釋,塊級注釋)
- ts 類型描述的越全,接口生成的越詳細
- 支持所有的 routing-controllers api方法
關于ts生成openapi更詳細的文檔請參考:框架生成OpenAPI數(shù)據(jù)
支持生成前端request代碼
框架提供輔助插件openapiv3-gen-typescript
, 能通過openapi 生成前端request代碼
- 因此可以通過
routing-controllers-to-openapi
生成openapi, 再通過openapi生成前端request代碼
關于openapi生成request代碼詳細文檔:openapi 生成前端 request 代碼
限流插件
為了防止流量洪峰時應用的崩潰,我們可以采取限流的方式來保護我們的應用,限流有多種規(guī)則
限流規(guī)則
- Node.js應用 整體限流,即: 應用在某一段時間內(nèi)所有接口的總流量限制
- 具體 path 路徑限流, 即: 應用在某一段時間內(nèi)某個具體的 path 路徑的流量限制
- 具體 path+具體用戶限流, 即: 應用在某一段時間內(nèi)某個 path 單個用戶的流量限制
支持規(guī)則動態(tài)變更實時生效
配置化的限流規(guī)則是不夠靈活的,對業(yè)務來說不能實時生效,基于此插件提供動態(tài)更新的 api
import { updateRateLimiterRules } from '@yunflyjs/yunfly-plugin-rate-limiter'
// 例如:EtchChange為規(guī)則變更監(jiān)聽函數(shù),當規(guī)則變更時通過 updateRateLimiterRules api 實時更新限流規(guī)則
EtchChange().then((data: NeedRateLimiterOption)=>{
updateRateLimiterRules(data)
})
限流插件使用文檔請參考:https://yunke-yunfly.github.io/doc.github.io/document/secruity/rate-limiter
node 性能排查,v8profiler插件
- 實時獲取 cpuprofile 插件, 用于性能瓶頸分析。
當應用出現(xiàn)性能瓶頸時,排查是一件比較復雜的事情,框架提供了@yunke/yunfly-plugin-v8-profiler
用于cpu性能排查。
性能瓶頸插件詳細使用文檔:https://yunke-yunfly.github.io/doc.github.io/document/plugin/cpuprofile
數(shù)據(jù)庫操作插件prisma
對于數(shù)據(jù)庫的操作,框架提供了prisma插件,它是新一代 orm 工具, 支持 MySql SQLite SQL Server MongoDB PostgreSQL。
- prisma插件詳細使用文檔:https://yunke-yunfly.github.io/doc.github.io/document/technology/prisma
Redis 插件
redis 是BFF服務或服務端開發(fā)經(jīng)常用到的內(nèi)存數(shù)據(jù)庫,框架提供了redis插件 @yunflyjs/yunfly-plugin-redis
文章來源:http://www.zghlxwxcb.cn/news/detail-609447.html
- redis插件詳細使用文檔:https://yunke-yunfly.github.io/doc.github.io/document/technology/redis
其他插件
框架還提供了一下常用的其他插件,例如:文章來源地址http://www.zghlxwxcb.cn/news/detail-609447.html
- socket插件:https://yunke-yunfly.github.io/doc.github.io/document/technology/socket
- prometheus插件:https://yunke-yunfly.github.io/doc.github.io/document/technology/prometheus
- jwt插件:https://yunke-yunfly.github.io/doc.github.io/document/secruity/jwt
- apollo插件:https://yunke-yunfly.github.io/doc.github.io/document/plugin/apollo
- 安全插件:https://yunke-yunfly.github.io/doc.github.io/document/plugin/secruity
- 內(nèi)存檢查插件:https://yunke-yunfly.github.io/doc.github.io/document/plugin/memory-check
- etcd插件: https://yunke-yunfly.github.io/doc.github.io/document/plugin/etcd
- alinode插件:https://yunke-yunfly.github.io/doc.github.io/document/plugin/alinode
- 熔斷:https://yunke-yunfly.github.io/doc.github.io/document/secruity/fusing
- grpc: https://yunke-yunfly.github.io/doc.github.io/document/technology/grpc
到了這里,關于Yunfly 一款高效、性能優(yōu)異的node.js企業(yè)級web框架的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!