最近遇到一个很严肃的问题,什么样的 events 算是合格的 events 结构。为什么需要定义source,定义在设计 events 事件体时是否有可以直接遵守的规范。针对这个问题,我收集了大量资料,从基础接口设计规范到CloudEvents 调研。索性也薄浅的了解了一些,就分享下这块的心得吧。有接口设计方面诉求的同学可以参考下。不过大部分也是研发的字段定义诉求了。
首先,我们先来了解下什么是 Events。
Events(以下统称事件)在系统设计中其实已经变的无处不在,它主要是接口级的事件描述。理论来讲定义接口和接口产生的事件是一个比较简单的事儿,但是现在缺乏一种对事件的统一描述,服务使用方和提供方往往要花费大量的时间沟通字段定义,凭直觉设计事件属性,并在将来的使用过程中疲于新增或修改事件的属性。所以在云上,定义事件其实有个 CNCF(云原生计算基金会) 牵头的 CloudEvents 标准协议来制定所有的服务事件规范。这其实也算是业内比较惯常使用的了(当然不包括 AWS,google 原因大家都懂, Azure 支持很到位)。
好了,这个应该很好理解,大体就是服务通过怎样的格式投递使用。
概念篇
那么从事件源到触发,整个流程是怎样的呢?下图很好的解释了里面的关系:
云服务代码中通常使用事件来连接不同的系统,其中一个系统中状态的改变会导致代码在另一个系统中执行。例如,当 Source 源接收到外部信号(例如HTTP或RPC)或检测到系统变化的值,它便会生成事件。Source 生成一条消息并将其中事件封装在协议中,事件到达目的地,触发事件数据并调用。
事件本身其实可以通过各种行业标准协议(例如HTTP,AMQP,MQTT,SMTP),开放源代码协议(例如Kafka,NATS)来传递,当然云厂商产生的事件其实一般是由厂商自定义的,这类事件也是本次探讨的核心。
action 的定义其实往往是指事件所附带的特定的事件触发。通常情况下,源是某种托管服务,而操作通常是 Serverless(无服务器化)的能力(例如AWS Lambda 或云函数 SCF)中的自定义代码。
定义篇
大体的概念大致梳理清楚了,那下面我们来看看最重要的定义,搞清楚定义,我们才能更好的研究事件的范围是如何的。
首先,我们来看下必选参数的定义:
id
类型: String
描述:事件标识,生产者必须确保source+id 对于每个不同的事件都是唯一的。如果重复事件被重新发送(例如网络错误),允许具有相同的id。消费者可以假设事件具有相同source且id重复。
source
类型: URI-reference描述:标识事件发生的上下文,通常将包括诸如事件源的类型,发布事件的组件或产生事件的过程之类的信息。一般由事件生产者定义数据背后的确切语法和语义。生产者必须确保source+id对于每个不同的事件都是唯一的。应用程序可以使用UUID,URN,DNS 授权或特定于整体技术架构的方案来创建唯一 source 标识符。
一个来源可以包括多个生产者。
type
类型: String
说明:该参数包含一个描述与源事件相关的事件类型。通常用于路由,策略执行等。格式应该由生产者定义,并且可能包含诸如的版本之类的信息。
其次,是非必选参数定义:
datacontenttype
类型:String描述:data 值的内容类型,该参数允许 data 赋予任何类型的内容。例如,通常使用 JSON 格式事件可能也会通过XML返回data信息,此时可以通过将此属性设置为“ application/xml”来通知使用者。
time
类型: Timestamp
描述:发生该事件的时间戳,如果不能确定发生时间,则生产者可以将此属性设置为其他时间(例如当前时间),但是所有生产者source必须保持一致。换句话说就是要么全部使用实际事件发生时间,要么全部使用相同的时间获取方式来确定所使用的时间戳。
核心参数的定义大抵如此了,其次是一些 Event Data,Attributes,和相关扩展字段,这里就不一一阐述了。这里给放个例子,也是 cloudevents 官方用例,可以供大家参考:
{ "specversion" : "1.0", // cloudevent版本信息 选择性填写即可 "type" : "com.github.pull.create", "source" : "https://github.com/cloudevents/spec/pull", "subject" : "123", "id" : "A234-1234-1234", "time" : "2020-04-05T17:31:00Z", "comexampleextension1" : "value", "comexampleothervalue" : 5, "datacontenttype" : "text/xml", "data" : "<much wow=\"xml\"/>" }
总结篇
CloudEvents 的核心其实是定义了一组关于不同组件间传输事件的元数据,以及这些元数据应该如何出现在消息体中。
其主旨大抵如下:
- 事件规范化
- 降低平台集成难度
- 提高FaaS的可移植性
- 源事件可追踪
- 提升事件关联性
准确的事件体,事件信息才可以做出更稳定的系统架构,永远保持对事件的敬畏。
附 一些术语及定义:
Occurrence:发生,指事件逻辑上的发生,基于某种情况,事件出现了。
Event:事件,表示事件以及上下文的数据记录。可以根据事件中的信息决定路由,但事件本身并不包含路由信息。
Producer:生产者,真正创造事件的实例或组件
Source:源,事件发生的上下文,可以由多个producer组成。
Consumer:消费者,接收事件并对事件进行消费
Intermediary:中介,接收包含事件的消息(message),并转发给下一个接收方,类似路由器
Context:上下文,上下文元数据被封装到context attributes中。用来判断事件与其它系统的关系
Data:数据,也可以叫做 payload
EventFormat:事件格式,例如 json
Message:消息,封装事件并将其从source传递到destination。
Protocol:协议,可以是行业标准如 http,开源协议如 kafka 或者供应商协议如 AWS Kinesis
Protocol Binding:协议绑定,描述如何通过给定的协议收发事件,如何将事件放到消息里
Thanks for the tips you are discussing on this site. Another thing I’d like to say is always that getting hold of some copies of your credit profile in order to scrutinize accuracy of each and every detail would be the first measures you have to execute in credit improvement. You are looking to clean up your credit file from detrimental details mistakes that screw up your credit score.
What i do not understood is actually how you are not actually much more well-liked than you might be now. You’re very intelligent. You realize thus considerably relating to this subject, made me personally consider it from so many varied angles. Its like men and women aren’t fascinated unless it?s one thing to accomplish with Lady gaga! Your own stuffs nice. Always maintain it up!
Terrific work! That is the kind of info that should be shared across the internet. Shame on Google for not positioning this publish higher! Come on over and consult with my web site . Thanks =)
Along with every thing that seems to be building within this subject material, your points of view happen to be somewhat refreshing. Nevertheless, I appologize, because I do not give credence to your entire strategy, all be it refreshing none the less. It looks to us that your remarks are actually not completely rationalized and in actuality you are generally yourself not really fully confident of your point. In any event I did enjoy reading through it.
Thanks a ton for your post. I would really like to write my opinion that the price of car insurance differs from one insurance policy to another, given that there are so many different facets which give rise to the overall cost. As an example, the make and model of the auto will have a huge bearing on the fee. A reliable ancient family car will have a more economical premium when compared to a flashy sports car.
I think other site proprietors should take this web site as an model, very clean and wonderful user friendly style and design, let alone the content. You’re an expert in this topic!
It?s onerous to find knowledgeable folks on this subject, however you sound like you understand what you?re talking about! Thanks
I have viewed that intelligent real estate agents everywhere are Advertising. They are knowing that it’s not just placing a sign in the front yard. It’s really regarding building connections with these retailers who at some point will become buyers. So, after you give your time and energy to supporting these suppliers go it alone – the “Law connected with Reciprocity” kicks in. Interesting blog post.
Thanks a ton for your post. I’d like to comment that the cost of car insurance varies from one insurance policy to another, simply because there are so many different facets which contribute to the overall cost. Such as, the brand name of the vehicle will have an enormous bearing on the cost. A reliable older family motor vehicle will have a more affordable premium compared to a flashy performance car.
Pretty portion of content. I just stumbled upon your weblog and in accession capital to say that I get actually loved account your blog posts. Any way I will be subscribing on your augment and even I achievement you get right of entry to constantly rapidly.
hello!,I really like your writing so so much! percentage we keep up a correspondence extra about your article on AOL? I require a specialist in this area to unravel my problem. Maybe that is you! Taking a look ahead to look you.
Hey very cool site!! Guy .. Beautiful .. Amazing .. I’ll bookmark your blog and take the feeds additionally?I am satisfied to find numerous useful information here within the publish, we want work out more techniques on this regard, thank you for sharing. . . . . .
Thank you for solving my problem with your post, I really appreciate it.
I have fun with, result in I found just what I was having a look for. You have ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye
Thanks for expressing your ideas. I might also like to state that video games have been actually evolving. Today’s technology and innovations have aided create practical and fun games. All these entertainment games were not that sensible when the concept was first of all being tried out. Just like other kinds of electronics, video games way too have had to grow via many years. This itself is testimony towards fast development of video games.
Valuable information. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
Thanks for the interesting things you have revealed in your short article. One thing I’d like to comment on is that FSBO relationships are built eventually. By presenting yourself to the owners the first few days their FSBO can be announced, before the masses begin calling on Monday, you make a good network. By mailing them resources, educational materials, free reviews, and forms, you become a good ally. By taking a personal affinity for them and their circumstances, you build a solid network that, on many occasions, pays off as soon as the owners decide to go with a real estate agent they know along with trust — preferably you.
Hi, i feel that i saw you visited my site thus i came to ?return the desire?.I am attempting to find things to improve my website!I assume its ok to make use of some of your ideas!!
I do agree with all the concepts you have presented on your post. They’re really convincing and can certainly work. Still, the posts are very short for novices. May you please lengthen them a bit from next time? Thanks for the post.
Thank you for any other informative site. The place else may I am getting that kind of information written in such an ideal way? I have a mission that I’m simply now operating on, and I have been at the look out for such information.
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how could we communicate?
One thing is always that one of the most popular incentives for applying your cards is a cash-back or even rebate offer. Generally, you will get 1-5 back upon various purchases. Depending on the credit cards, you may get 1 back on most expenses, and 5 back on buying made going to convenience stores, gasoline stations, grocery stores along with ‘member merchants’.
I’ve been browsing on-line more than 3 hours as of late, yet I by no means discovered any fascinating article like yours. It is pretty worth sufficient for me. In my view, if all site owners and bloggers made just right content material as you probably did, the web will probably be a lot more useful than ever before.
I do trust all the ideas you’ve introduced for your post. They’re really convincing and will certainly work. Nonetheless, the posts are very short for starters. May you please extend them a bit from next time? Thank you for the post.
Thanks for making me to acquire new strategies about computers. I also hold the belief that one of the best ways to maintain your notebook computer in perfect condition has been a hard plastic-type material case, or even shell, that suits over the top of your computer. These kinds of protective gear are generally model distinct since they are made to fit perfectly within the natural housing. You can buy them directly from the owner, or via third party places if they are available for your laptop, however not all laptop may have a cover on the market. Yet again, thanks for your guidelines.
I should say also believe that mesothelioma is a scarce form of cancer malignancy that is often found in individuals previously familiar with asbestos. Cancerous cells form within the mesothelium, which is a safety lining that covers the vast majority of body’s internal organs. These cells generally form while in the lining of your lungs, belly, or the sac that encircles one’s heart. Thanks for expressing your ideas.
I?ve been exploring for a bit for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i am happy to convey that I have an incredibly good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to don?t forget this website and give it a look regularly.
Valuable information. Lucky me I found your website by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
What?s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I hope to contribute & assist other users like its aided me. Good job.
Oh my goodness! an incredible article dude. Thanks Nevertheless I’m experiencing concern with ur rss . Don?t know why Unable to subscribe to it. Is there anyone getting similar rss downside? Anyone who knows kindly respond. Thnkx
It’s best to take part in a contest for probably the greatest blogs on the web. I will suggest this site!
I have seen many useful issues on your site about computer systems. However, I’ve got the view that notebook computers are still more or less not powerful adequately to be a good choice if you typically do jobs that require loads of power, including video croping and editing. But for internet surfing, word processing, and quite a few other popular computer functions they are okay, provided you don’t mind the small screen size. Appreciate sharing your opinions.
Thank you for sharing these kind of wonderful threads. In addition, the optimal travel along with medical insurance system can often relieve those concerns that come with touring abroad. A new medical emergency can shortly become expensive and that’s bound to quickly decide to put a financial problem on the family’s finances. Putting in place the ideal travel insurance program prior to setting off is well worth the time and effort. Thanks a lot
One important thing is that if you are searching for a education loan you may find that you will need a co-signer. There are many conditions where this is correct because you may find that you do not possess a past credit history so the lender will require that you’ve got someone cosign the money for you. Interesting post.
magnificent points altogether, you simply gained a new reader. What would you suggest in regards to your post that you made some days ago? Any positive?
Wonderful goods from you, man. I have keep in mind your stuff previous to and you’re just extremely wonderful. I actually like what you’ve bought right here, really like what you’re saying and the way in which through which you say it. You make it entertaining and you continue to take care of to keep it sensible. I can not wait to read far more from you. That is really a terrific web site.
I have learned newer and more effective things via your blog site. One other thing I’d like to say is that often newer computer system operating systems often allow a lot more memory to be utilized, but they in addition demand more storage simply to operate. If someone’s computer can’t handle much more memory and also the newest software requires that memory space increase, it usually is the time to buy a new Computer. Thanks