一篇比较AsyncAPI与CloudEvents的文章,很不错,原文连接:https://www.asyncapi.com/blog/asyncapi-cloud-events/

I’ve been receiving the same question for a long time now: Should I use CloudEvents or AsyncAPI? — And my response has always been the same: it depends!

There is the belief by many people that AsyncAPI and CloudEvents are competing for the same thing. This can’t be less true, and I’d like to explain you why. Read on!

What is CloudEvents?

From cloudevents.io:

Enter CloudEvents, a specification for describing event data in a common way. CloudEvents seeks to ease event declaration and delivery across services, platforms and beyond!

The purpose of CloudEvents is to establish a common format for event data description. And it makes a lot of sense when you realize they are part of the CNCF’s Serverless Working Group.

If you are doing serverless or FaaS (Function as a Service), then CloudEvents is your best friend because the event is the only information you will have in your function during runtime. No topics or channels, no servers, no subscribers. Just the event and some extra information you may need to make your function work.

CloudEvents is focused on the event and defines an envelope for your application’s data. See an example from their repo:

{
"specversion" : "0.2",
"type" : "com.github.pull.create",
"source" : "https://github.com/cloudevents/spec/pull/123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"comexampleextension1" : "value",
"comexampleextension2" : {
"othervalue": 5
},
"contenttype" : "text/xml",
"data" : "<much wow=\"xml\"/>"
}

Here your event is actually <much wow=\"xml\"/> and the rest is meta information about your event. This envelope is what CloudEvents defines with the purpose of making event declaration reusable across services and platforms.

What is AsyncAPI?

From the AsyncAPI repo:

Create machine-readable definitions of your event-driven APIs.

The purpose of AsyncAPI is to provide a way for you to define how your event-driven applications (or APIs) communicate with the rest of the world. AsyncAPI is focused on the application and the channels it uses to communicate. Similar to what OpenAPI and RAML do for REST APIs. Unlike CloudEvents —who focuses on the message— AsyncAPI does not impose how your event must look like but, instead, allows you to strictly define its shape. See an example:

asyncapi: 2.0.0-rc1
id: urn:com.asyncapi.examples.user
info:
title: User service
version: 1.6.3
channels:
user/signedup:
publish:
message:
payload:
type: object
properties:
fullName:
type: string
email:
type: string
format: email

Looking at the example above, one can rapidly say this is the AsyncAPI definition of a User service, which its API version is 1.6.3 and it publishes to the user/signedup channel an event that is an object containing two properties: fullName and email.

We can define the event payload but its structure is totally free and user-defined. And that’s what makes AsyncAPI so powerful! Since our event payload can be anything, it can also be a CloudEvents event.

AsyncAPI + CloudEvents

Let’s see an example of the two combined:

asyncapi: 2.0.0-rc1
id: urn:com.asyncapi.examples.user
info:
title: User service
version: 1.6.3
channels:
user/signedup:
publish:
message:
payload:
type: object
properties:
specversion:
type: string
enum: ['0.2']
type:
type: string
example: com.github.pull.create
source:
type: string
format: uri
example: urn:com.asyncapi.examples.user
id:
type: string
example: 'A234-1234-1234'
time:
type: string
format: date-time
example: 2018-04-05T17:31:00Z
contenttype:
type: string
example: 'application/json'
data:
type: object
properties:
fullName:
type: string
email:
type: string
format: email

Looking at the example above, one can say this is the AsyncAPI definition of a User service, which its API version is 1.6.3 and it publishes to the user/signedup channel a CloudEvents event whose data is a JSON object containing two properties: fullName and email.

Leveraging AsyncAPI Custom Schema Formats

There’s only one concern with the approach above: every single CloudEvents definition is going to be exactly the same from line 11 to 33 — except for the examples that were added in this blog for clarity.

The default format for defining events (messages) in AsyncAPI 2.0 is JSON Schema. Thankfully, AsyncAPI provides a way to define events in your own custom format —like Avro and Protobuf — or a hypothetical CloudEvents one in this case. See example:

asyncapi: 2.0.0-rc1
id: urn:com.asyncapi.examples.user
info:
title: User service
version: 1.6.3
channels:
user/signedup:
publish:
message:
schemaFormat: 'application/cloudevents+json; version=0.2; charset=utf-8'
payload:
type: object
properties:
fullName:
type: string
email:
type: string
format: email

This results in a much shorter and nicer way of defining the usage of CloudEvents inside an AsyncAPI document.

Ok, it’s possible but, does it makes sense?

It really depends on your use case but it makes sense in scenarios where some kind of FaaS is involved. Consider the following example:

Reading the diagram from the bottom up, we see an overly simplified diagram of a sign up process. The user/signedup event flows from the REST API to the monitoring service and the FaaS API through the broker. The event could have the CloudEvents format so that both, the FaaS API and the monitoring service, understand it. Obviously, one may argue that the Faas API could be wrapping the event data in CloudEvents format and leave the rest of the events untouched, in plain JSON. Fair.

So, does it really makes sense? It certainly does in some situations. Do you have to use AsyncAPI and CloudEvents together? As always that’s up to you. You have the tools. Choose them wisely.

 

AsyncAPI and CloudEvents的更多相关文章

  1. ASYNCAPI

    https://www.asyncapi.com Introduction AsyncAPI provides a specification that allows you to define Me ...

  2. cloudevents js sdk 简单试用

    cloudevents 目前官方提供了不同语言的sdk,以下是js 的简单学习试用,从目前来说更新不是很好 clone 代码 git clone https://github.com/cloudeve ...

  3. AsyncAPI 几个方便的工具

    AsyncAPI 类似openapi 也提供了一些周边工具 editor 类似openapi 的,但是目前官方已经不推荐使用了,推荐使用playground,以下是构建一个容器镜像 clone 代码 ...

  4. AsyncAPI 试用

    AsyncAPI 提供了类似openapi的代码生成,以下demo,来自官方,只是目前官方的generator有些问题以下 同时说明运行中的一些问题 环境准备 主要是安装依赖组件 npm instal ...

  5. asyncapi 指南

    asyncapi 是可以用来创建异步机器可读定义api的指南,我们可以用来创建事件驱动的架构. 说明 asyncapi 的定义类似openapi,目前指南版本为2.0,很值得学习下 参考资料 http ...

  6. cloudevents 通用event 描述指南

    cloudevents 是由cncf 组织管理的一个通用event描述指南 特性: 一致性 可理解性 可移植性 说明 cloudevents 不仅提供了核心描述,同时也包含了不同协议的指南说明(htt ...

  7. js 模块化历程

    作者:吕大豹 网址:http://www.cnblogs.com/lvdabao/p/js-modules-develop.html 这是一篇关于js模块化历程的长长的流水账,记录js模块化思想的诞生 ...

  8. Salesforce Bulk API 基于.Net平台下的实施

    在最近的salesforce实施项目中应用到Bulk API来做数据接口.顺便把实际应用的例子写下来.希望对做salesforce接口的朋友有借鉴作用. 一 参考网络牛人写好的Demo. 下载地址:h ...

  9. Boost - 从Coroutine2 到Fiber

    Boost - 从Coroutine2 到Fiber 协程引子 我开始一直搞不懂协程是什么,网上搜一搜,(尤其是Golang的goroutine)感觉从概念上听起来有点像线程池,尤其是类似Java的E ...

随机推荐

  1. Debian 9 / Debian 10 / Ubuntu 18.04 / Ubuntu 18.10快速开启BBR加速 或 关闭BBR加速

    如果使用的是Debian 9.Debian 10.Ubuntu 18.04.Ubuntu 18.10等内核高于4.9版本的系统,均可以使用此方法开启BBR加速,若你使用了Ubuntu 19.04的系统 ...

  2. Google Chrome 浏览器JS无法更新解决办法

    JS无法更新原因: 浏览器为了加载快,默认是按照自定规则更新缓存,非实时更新. 我们在开发的时候,JS变动很快,需要即时让浏览器加载最新文件,也就是禁用浏览器缓存 (1)使用F12进入开发者模式,找到 ...

  3. 2019 思贝克java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.思贝克等公司offer,岗位是Java后端开发,因为发展原因最终选择去了思贝克,入职一年时间了,也成为了面试官 ...

  4. 2019 中至数据java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.中至数据等公司offer,岗位是Java后端开发,因为发展原因最终选择去了中至数据,入职一年时间了,也成为了面 ...

  5. 【开发笔记】- 修改tomcat默认的编码方式

    tomcat8以后默认编码格式是utf-8:7之前的都是iso8859-1 如果默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找 ...

  6. 【转载】 C#中ArrayList使用GetRange方法获取某一段集合数据

    在C#的编程开发中,ArrayList集合是一个常用的非泛型类集合,可以使用GetRange方法来获取集合中指定索引位置开始的一整段集合数据组成一个新的集合,GetRange方法的签名为virtual ...

  7. vue与webpack开发环境搭建:从无到有

    一个vue从无到有的搭建过程. 一.不论是webpack还是vue,最初的第一步就是安装node.js.它是基石. 从官网下载你需要的安装包:官网下载链接:http://nodejs.cn/downl ...

  8. 小tips:TCP的三次握手、长连接、 短连接、 SPDY 协议

    当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次握手的,而释放则需 ...

  9. FreePascal - CodeTyphon 如何让编译的程序更小!

    CodeTyphon 6.9 在菜单[project]-->[project option]的弹出界面中 选择[compiler option]-->[debugging] 1,去掉“Ge ...

  10. 记Html的初次接触

    第一次接触Html是在昨天的培训班体验课上,这一次课我明白了许多. 1.程序语言比我想象中还要多(原来除了C与Java还有这么多) 2.程序员毕业后掌握7种语言是很正常的事(难怪程序员会秃顶) 3.H ...