网站的建设步骤,wordpress多语言包,潍坊建设网站公司电话,淄博市网站建设介绍
Yunfly 一款高性能 Node.js WEB 框架, 使用 Typescript 构建我们的应用。 使用 Koa2 做为 HTTP 底层框架, 使用 routing-controllers 、 typedi 来高效构建我们的 Node 应用。
Yunfly 在 Koa 框架之上提升了一个抽象级别, 但仍然支持 Koa 中间件。在此基础之上, 提供了一…介绍
Yunfly 一款高性能 Node.js WEB 框架, 使用 Typescript 构建我们的应用。 使用 Koa2 做为 HTTP 底层框架, 使用 routing-controllers 、 typedi 来高效构建我们的 Node 应用。
Yunfly 在 Koa 框架之上提升了一个抽象级别, 但仍然支持 Koa 中间件。在此基础之上, 提供了一套强大的插件系统, 给开发者提供更强大更灵活的能力。
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 微软开发的自由和开源的编程语言, 它是JavaScript的一个超集, 添加了可选的静态类型和基于类的面向对象编程 参考文档routing-controllers 使用装饰器的方式来进行路由的开发 参考文档typedi: 依赖注入插件工具 参考文档grpc: 一个高性能、开源和通用的 RPC 框架 参考文档winston: javascript 的 log 日志插件 参考文档 与社区框架差异
能力yunflyeggjsnestjsTypescript✅❌[支持但不友好]✅cluster✅✅❌openapi✅❌✅框架约束部分约束约束自由扩展模型插件插件模块
性能
yunfly 框架底层 web 库为 koa, 路由开发模型库为 routing-controllers, 路由命中库为 find-my-way。
koa 对于写业务来说性能是足够优异的routing-controllers 使用装饰器的方式来进行路由的开发对于开发者来说是很提效的。
框架剔除了低效的 koa-router 更换为高效的 find-my-way。框架未内插件开发者可以根据自己的需求定制插件。
性能压测
以下性能测试为同一台机器同样的容器场景下压测3分钟得出的结果。
容器环境
1G1核 Docker 容器
hello world 场景
web框架qps备注yunfly6400使用 koa 为底层库eggjs3950使用 koa 为底层库nestjs2900使用 express 为底层库nestjs7200使用 fastify 为底层库
1000 个路由场景
web框架qps备注yunfly6100使用 koa 为底层库eggjs1680使用 koa 为底层库nestjs2050使用 express为底层库nestjs6550使用 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?namexxx当前支持的一些特性
支持多进程模型
若应用需要开启node多进程只需要在 config 中配置启用即可单多进程模型随意切换
Cluster 配置
// src/config/config.default.ts
/*** cluster config*/
config.cluster {enable: true,
};自定义启动进程数
// src/config/config.default.ts
config.cluster {enable: true,count: 4,
};备注在 docker 容器场景下会优先获取容器分配的cpu核数, 优先级容器核数 config.cluster.count
随意定制你的框架
yunfly web框架是由基础包一个个插件组合而成框架自身提供了很多插件支持开发者自定义插件。 备注yunfly 的插件部分理念实现参考了eggjs的插件模型 开发者可以把常规插件自定义插件打包成一个集合组装成一个新的框架。 此处能力可以参考Yunfly 框架开发
支持生成openapi
框架提供了辅助插件 routing-controllers-to-openapi, 能把所有路由与Typescript代码转换为openapi, 进而你可以通过openapi生成接口文档信息。
支持 typescript 生成 jsonschema支持注释行内注释代码块上方注释块级注释ts 类型描述的越全接口生成的越详细支持所有的 routing-controllers api方法
关于ts生成openapi更详细的文档请参考框架生成OpenAPI数据
支持生成前端request代码
框架提供辅助插件openapiv3-gen-typescript, 能通过openapi 生成前端request代码
因此可以通过routing-controllers-to-openapi 生成openapi, 再通过openapi生成前端request代码
关于openapi生成request代码详细文档openapi 生成前端 request 代码
限流插件
为了防止流量洪峰时应用的崩溃我们可以采取限流的方式来保护我们的应用限流有多种规则
限流规则
Node.js应用 整体限流即 应用在某一段时间内所有接口的总流量限制具体 path 路径限流 即 应用在某一段时间内某个具体的 path 路径的流量限制具体 path具体用户限流 即 应用在某一段时间内某个 path 单个用户的流量限制
支持规则动态变更实时生效
配置化的限流规则是不够灵活的对业务来说不能实时生效基于此插件提供动态更新的 api
import { updateRateLimiterRules } from yunflyjs/yunfly-plugin-rate-limiter// 例如EtchChange为规则变更监听函数当规则变更时通过 updateRateLimiterRules api 实时更新限流规则
EtchChange().then((data: NeedRateLimiterOption){updateRateLimiterRules(data)
})限流插件使用文档请参考https://yunke-yunfly.github.io/doc.github.io/document/secruity/rate-limiter
node 性能排查v8profiler插件
实时获取 cpuprofile 插件, 用于性能瓶颈分析。 当应用出现性能瓶颈时排查是一件比较复杂的事情框架提供了yunke/yunfly-plugin-v8-profiler 用于cpu性能排查。 性能瓶颈插件详细使用文档https://yunke-yunfly.github.io/doc.github.io/document/plugin/cpuprofile
数据库操作插件prisma
对于数据库的操作框架提供了prisma插件它是新一代 orm 工具, 支持 MySql SQLite SQL Server MongoDB PostgreSQL。
prisma插件详细使用文档https://yunke-yunfly.github.io/doc.github.io/document/technology/prisma
Redis 插件
redis 是BFF服务或服务端开发经常用到的内存数据库框架提供了redis插件 yunflyjs/yunfly-plugin-redis
redis插件详细使用文档https://yunke-yunfly.github.io/doc.github.io/document/technology/redis
其他插件
框架还提供了一下常用的其他插件例如
socket插件https://yunke-yunfly.github.io/doc.github.io/document/technology/socketprometheus插件https://yunke-yunfly.github.io/doc.github.io/document/technology/prometheusjwt插件https://yunke-yunfly.github.io/doc.github.io/document/secruity/jwtapollo插件https://yunke-yunfly.github.io/doc.github.io/document/plugin/apollo安全插件https://yunke-yunfly.github.io/doc.github.io/document/plugin/secruity内存检查插件https://yunke-yunfly.github.io/doc.github.io/document/plugin/memory-checketcd插件: https://yunke-yunfly.github.io/doc.github.io/document/plugin/etcdalinode插件https://yunke-yunfly.github.io/doc.github.io/document/plugin/alinode熔断https://yunke-yunfly.github.io/doc.github.io/document/secruity/fusinggrpc: https://yunke-yunfly.github.io/doc.github.io/document/technology/grpc