• AsyncAPI 试用


    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

  • 相关阅读:
    安卓-登陆页面的实现
    异常
    实用类
    Hashset
    Map
    LinkedList
    arraylist
    继承
    字符串相关代码
    数组代码
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11627307.html
Copyright © 2020-2023  润新知