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. LOJ2392 JOISC2017 烟花棒 二分、贪心

    传送门 先二分一个最大速度\(v\). 分析移动的性质.很显然的事情是在火焰两边的所有人都会往火焰的方向以最快的速度运动,这样可以使当前位置更早获得火焰,同时当前拥有火焰的若干个人为了传递火焰自然也会 ...

  2. 补习系列(17)-springboot mongodb 内嵌数据库【华为云技术分享】

    目录 简介 一.使用 flapdoodle.embed.mongo A. 引入依赖 B. 准备测试类 C. 完善配置 D. 启动测试 细节 二.使用Fongo A. 引入框架 B. 准备测试类 C.业 ...

  3. Marshmallow详解

    目录 Marshmallow详解 1. Scheme 2. Serializing(序列化) 3. 过滤输出 4. Deserializing(反序列化) 5. 处理多个对象的集合 6. Valida ...

  4. Kafka学习笔记之Kafka自身操作日志的清理方法(非Topic数据)

    0x00 概述 本文主要讲Kafka自身操作日志的清理方法(非Topic数据),Topic数据自己有对应的删除策略,请看这里. Kafka长时间运行过程中,在kafka/logs目录下产生了大量的ka ...

  5. [Leetcode] Binary Tree Pruning

    題目是說,如果左右子樹都不存在又自已為0,就去掉那個子樹(設為null) recursive後序,左子樹,右子樹,然後是根 自已同時又是別人的子樹,所以要告訢根自已是不是存在 從a開始,左右子樹都不存 ...

  6. 异常---Day21(写得有错请指出,感谢)

    异常的概念 异常,就是不正常的意思.在生活中:医生说,你的身体某个部位有异常,该部位和正常相比有点不同,该部位的功能将受影响.在程序中的意思就是:指的是程序在执行过程中,出现的非正常的情况,终会导致J ...

  7. Java自学-接口与继承 super

    Java的super关键字 步骤 1 : 准备一个显式提供无参构造方法的父类 准备显式提供无参构造方法的父类 在实例化Hero对象的时候,其构造方法会打印 "Hero的构造方法 " ...

  8. WebStorm eslint插件报错解决 - TypeError: this.CliEngine is not a constructor

    将eslint更新版本后,出现TypeError: this.CliEngine is not a constructor的错误. 解决办法: 1.编辑 X:\WebStorm\plugins\Jav ...

  9. css3中的calc的使用

    最近在布局的时候遇到一个问题,在页面中的左侧是侧边栏,右边是内容区域,内容区域中有一个固定定位的标签页,在设置固定定位的标签设置宽度的时候应该是内容区域的宽度,而固定定位的时候相对于是窗口的宽度,所以 ...

  10. 单链表-Python实现-jupyter->markdown 格式测试

    单链表引入 顺序表 理解Python变量的本质: 变量存储的不是值,是值的地址 理解Python的 "="表示的是指向关系 案例: 交换a,b的值, a=10, b=20 a, b ...