Moleculer, a modern microservices framework for NodeJS
source link: https://ppzz.github.io/posts/6122/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Moleculer, a modern microservices framework for NodeJS
译自:Moleculer Blog,作者:Icebob,发表时间:Jun 7, 2017
Moleculer, Nodejs 上现代化的微服务框架
我宣布经过半年的努力,我为 Node.js 创造了一个全新的微服务框架。
What‘s Moleculer
Moleculer 是一个开源的、快速的、可扩展的框架,基于 MIT 协议。它支持很多重要的微服务相关功能(服务注册,自动发现,负载均衡,断路器等)。
Key features
- 基于 Promise 的解决方案(Bluebird)
- 请求 - 响应的理念
- 事件驱动架构,支持负载均衡
- 支持中间件
- 内置缓存解决方案(内存,Redis)
- 支持多种传输器(TCP,NATS,MQTT,Redis,AMQP,NATS Streaming,Kafka)
- 支持多种序列化器(JSON,Avro,MsgPack,Protocol Buffer)
- 请求的负载均衡(round-robin,random)
- 自动发现服务
- 健康监测,指标和统计数据
Install
通过 npm 包管理器可以安装 Moleculer,你可以使用 npm 或 yarn。
npm install moleculer |
Usage
下面的这个小例子展示了使用 Moleculer 创建和调用服务有多简单。
const { ServiceBroker } = require("moleculer"); |
正如所见,我们创建了一个 math
服务,有一个 action
叫 add
。 这个 action
的作用是对两个参数求和。 创建后,我们可以使用 broker.call
方法调用它。broker.call
第一个参数是 path(ervice name + action name)
,第二个参数是调用 path
指定的 action
需要的参数(需要包装成对象再传递)。
在 Runkit 网站上,你可以在浏览器中运行上述代码。
Create a project
使用 Molecular CLI 工具可以创建一个基于 Molecular 的微服务。
# 1. 全局安装 moleculer-cli |
安装过程中,遇到提问全部选 Yes
然后我们得到了一个 service(greeter),其中中包含基于 Jest 的测试,还有集成了 API 网关功能。
进入 moleculer-demo
的目录,启动
cd moleculer-demo |
打开: http://localhost:3000/api/greeter/hello 链接。它将会调用 greeter 服务的 helloaction.
打开: http://localhost:3000/api/greeter/welcome?name=world 链接将会调用 welcomeaction.
Congratulations!你成功创建了你的第一个 Moleculer 的微服务。欢迎打开微服务的大门。
如果你想了解更多关于 Moleculer 框架的知识,请查阅我们的网站 https://moleculer.services 或者 github:https://github.com/moleculerjs/moleculer.
你也可以在 Twitter 上关注我们 @MoleculerJS 或者加入 Gitter 同我们交流.
如果你喜欢这篇文章,可以在下方给我们点赞和留言。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK