AsyncAPI 提供了类似openapi的代码生成,以下demo,来自官方,只是目前官方的generator有些问题以下
同时说明运行中的一些问题

环境准备

主要是安装依赖组件

npm install -g asyncapi-generator mqtt

定义AsyncAPI

稍有改动,主要是channel(mqtt 的topic)

  • yaml 文件
asyncapi: '2.0.0'
info:
  title: Streetlights API
  version: '1.0.0'
  description: |
    The Smartylighting Streetlights API allows you
    to remotely manage the city lights.
  license:
    name: Apache 2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0'
servers:
  mosquitto:
    url: test.mosquitto.org
    protocol: mqtt
channels:
  dalong/measured:
    publish:
      summary: Inform about environmental lighting conditions for a particular streetlight.
      message:
        payload:
          type: object
          properties:
            id:
              type: integer
              minimum: 0
              description: Id of the streetlight.
            lumens:
              type: integer
              minimum: 0
              description: Light intensity measured in lumens.
            sentAt:
              type: string
              format: date-time
              description: Date and time when the message was sent.
  • 生成代码命令
ag asyncapi.yaml nodejs -p server=mosquitto
  • 简单说明
    如果直接使用url test.mosquitto.org 会有解析的问题,如下:
 
Something went wrong:
Template render error: (unknown path) [Line 29, Column 36]
  TypeError [ERR_INVALID_URL]: Invalid URL: test.mosquitto.org
    at Object._prettifyError (/usr/local/lib/node_modules/asyncapi-generator/node_modules/nunjucks/src/lib.js:36:11)
    at /usr/local/lib/node_modules/asyncapi-generator/node_modules/nunjucks/src/environment.js:567:19
    at Template.root [as rootRenderFunc] (eval at _compile (/usr/local/lib/node_modules/asyncapi-generator/node_modules/nunjucks/src/environmen
t.js:637:18), <anonymous>:83:3)
    at Template.render (/usr/local/lib/node_modules/asyncapi-generator/node_modules/nunjucks/src/environment.js:556:10)
    at Environment.renderString (/usr/local/lib/node_modules/asyncapi-generator/node_modules/nunjucks/src/environment.js:380:17)
    at Promise (/usr/local/lib/node_modules/asyncapi-generator/lib/generator.js:507:21)
    at new Promise (<anonymous>)
    at Generator.renderString (/usr/local/lib/node_modules/asyncapi-generator/lib/generator.js:506:12)
    at Generator.renderFile (/usr/local/lib/node_modules/asyncapi-generator/lib/generator.js:531:19)
    at <anonymous>
 

解决方法:临时修改为一个url 格式的比如test.mosquitto.org:1883 生成代码之后再修改回来

  • 生成效果
Done! ✨
Check out your shiny new generated files at /Users/dalong/mylearning/asyncapi-learning/first/streetlights.
 

  • 修改几处生成的代码
    配置文件:
    config/common.yml:
    如下:
 
default:
  app:
    name: Streetlights API
    version: 1.0.0
  broker:
    mqtt:
      url: mqtt://test.mosquitto.org
      topics: ["dalong/measured"]
      qos:
      protocol: mqtt
      retain:
      subscribe: true
development:
test:
staging:
production:
 
 

路由以及handler修改:
路由:

随便些一个名字,注意后边handler 需要和这个一样

启动&&测试

  • 安装npm依赖
 
yarn
  • 启动
yarn start
  • 模拟消息发送
mqtt pub -t 'dalong/measured' -h 'test.mosquitto.org' -m '{"id": 1111, "lumens": 3, "sentAt": "2019-10-06T12:34:32.000Z"}'
  • 效果

说明

以上是一个简单的试用,以及在使用中碰到的问题,AsyncAPI 是一个很不错的编写事件驱动应用的指南

参考资料

https://www.asyncapi.com/docs/tutorials/streetlights/
https://github.com/asyncapi/generator
http://test.mosquitto.org/
https://github.com/rongfengliang/asyncapi-streetlights-demo

AsyncAPI 试用的更多相关文章

  1. SharePoint Online 申请试用链接地址

    SharePoint Online 申请试用链接地址: https://products.office.com/en-us/business/compare-office-365-for-busine ...

  2. 活动助手Beta用户试用报告

    用户试用报告 1.面向参与者用户 1.1 日常参加各类学习(水综测)活动中,有没有遇到以下问题: (1) 信息来源混乱,不知道靠不靠谱 (2) 每次报名都要重新填写自己的学号手机号,有时候填错了就没综 ...

  3. 创建WP8试用应用

    参考资料: 创建 Windows Phone 的试用应用 如何在 Windows Phone 应用中实现试用体验 Windows Phone 7 开发 31 日谈——第23日:提供试用版应用程序 对资 ...

  4. 达梦7的试用 与SQLSERVER的简单技术对比

    达梦7的试用 与SQLSERVER的简单技术对比 达梦数据库公司推出了他们的数据库服务管理平台,可以在该平台使用达梦数据库而无须安装达梦7数据库 地址:http://online.dameng.com ...

  5. Beta版本——用户试用与调研报告

    1 引言 1.1 系统概述 ​ 毕设导师智能分配系统是一个用来简化传统手工匹配繁琐操作的系统.本系统将学生报志愿.系负责人收集整理数据.相关人员进行手工分配.反馈选择结果等繁琐的操作转移到线上.把毕设 ...

  6. 【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能

    [背景] 折腾: [记录]给Android中添加log日志输出到文件 期间,已经试了: [记录]尝试用android中microlog4android实现log输出到文件的功能 但是不好用. 然后就是 ...

  7. 代码阅读分析工具Understand 2.0试用

    Understand 2.0是一款源代码阅读分析软件,功能强大.试用过一段时间后,感觉相当不错,确实可以大大提高代码阅读效率.由于Understand功能十分强大,本文不可能详尽地介绍它的所有功能,所 ...

  8. 如何申请国际版Office365和Azure的试用账号

    关键字:国际版.Office365.Azure.试用账号.1美元.信用卡 待续

  9. Android N preview 试用

    一.下载更新包 下载地址:https://developer.android.com/intl/zh-cn/preview/download.html 注意下载适合你手机的安装包哦 二.把你手机的oe ...

随机推荐

  1. Replication:事务复制 Subscriber的主键列是只读的

    在使用Transactional Replication时,Subscriber 被认为是“Read-Only”的 , All data at the Subscriber is “read-only ...

  2. 卸载webpack,降低版本

    卸载:npm uninstall webpack -g 重新安装:npm install webpack@3.7.1 -g

  3. [shell] while read line 与for循环的区别

    [shell] while read line 与for循环的区别 while read line 与for循环的区别---转载整理 while read line 是一次性将文件信息读入并赋值给变量 ...

  4. 基于C#实现的单点登录

    了解或想探索单点登录的朋友应该对单点登录有一个大致的了解,在这里我不在过多的阐述单点登录的概念. 单点登录说的通俗一点,就是一处登录(统一认证中心--Server),处处通行(Client). 一.第 ...

  5. 【转】socket通信-C#实现udp通讯

    在日常碰到的项目中,有些场景下不适合使用tcp常连接,而需要靠UDP无连接的数据收发.那么如何使用SharpSocket完成UDP收发数据呢?其中要掌握的关键点是什么呢? 点击查看原博文内容

  6. APS.NET MVC + EF (08)---数据注解和验证

    对于Web开发人员来说,用户输入验证一直是一个挑战.不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.如果觉得验证是令人望而生畏的繁杂琐事,ASP.NET MVC框架提供了数据注解的方式帮助 ...

  7. 拷贝 vs 赋值

    其实我只要关注两个动作就够了: 拷贝.赋值=            (而构造.析构的步骤都是没疑问的.) ——>   赋值=  都是显式调用的, 而拷贝构造可以显示调,也可能隐式被调. 下图错误 ...

  8. (转)二步实现 远程连接 阿里云SqlServer 2012 数据库服务器

    前言:在使用 阿里云 上的一些产品时,遇到不少坑. 安装IIS 时,遇到 因买的配置过低,虚拟内存不足,而导致 IIS 总是安装失败: 现在 在上面安装了个 Sql Sever 2012,远程老是 不 ...

  9. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  10. 工具sublime安装

    默认安装后是英文版 view-show console 安装packagecontrol https://packagecontrol.io/installation ctrl+`打开控制台,输入代码 ...