Geek Culture
Published in

Geek Culture

How to define event-driven architectures using AsyncAPI

Source: www.asyncapi.com

Event-driven applications are inherently distributed and loosely coupled, which potentially leads to having many self-contained components in your architecture, managed by multiple teams.

AsyncAPI provides specification that allows you to define message-driven APIs in a machine-readable format.

This article will describe how you can define your event-driven architectures using the AsyncAPI definition.

What are the various parts of AsyncAPI specifications?

  • API Version: This defines the version of AsycnAPI.
  • Info: In this section, you define some metadata about the API.
  • Servers: This section describes servers or brokers where you want to connect.
  • Channels: This section contains the messaging channel information.
  • Components: This section defines the message definitions you will refer to inside the channel.

Example of AsyncAPI minimal definition

Below is an example API definition that connects to Kafka;

asyncapi: 2.0info:title: Shopping processversion: 1.0servers:development:url: localhost:{port}protocol: kafkavariables:port:default: '9092'channels:shopping.events.01:description: |-This contains events related to shopping.publish:operationId: consumeShoppingEventmessage:$ref: '#/components/messages/shoppingEvent'bindings:kafka:groupId: myGroupIdcomponents:messages:shoppingEvent:name: shoppingEventcontentType: 'application/json'payload:$ref: '#/components/schemas/ShoppingEventPayload'schemas:ShoppingEventPayload:type: objectproperties:ShoppingId:description: Shopping Idtype: stringamount:type: integer

How to Read the AsyncAPI Definition

With asynchronous communication, an application can consume an event and also emit an event.

A channel section from the above example looks as follows;

channels:shopping.events.01:description: |-This contains events related to shopping.publish:operationId: consumeShoppingEventmessage:$ref: '#/components/messages/shoppingEvent'bindings:kafka:groupId: myGroupId

The publish section in the channel section contains an operation Id, message, and binding.

When you read the definition, you have to read it from the perspective of a viewer looking at the API definition that is already implemented by a provider application.

If you want to interact with the provider’s application (who has implemented the API definition), you have to consume events from the shopping.events.01 topic.

Code generation

AsyncAPI provides different templates to create code from the API definition. There are quite a few code generation templates for languages such as Java, Nodejs, HTML, and python.

To generate the code from a template, you would need to use an async generator command, which you can install by command-line or use a docker image to run the command.

To install the command-line utility, you will need to run the following command;

npm install -g @asyncapi/generator

In the example given above, you would be using the @asyncapi/java-spring-template to generate the API providers and viewer’s code.

However, if you use the docker way of creating the code, It might take some time to generate because it downloads the template every time you run the command.

Conclusion

This article looks at how an AsyncAPI definition looks like and generates code using a code template. Even though it may look complicated for a beginner to define and generate code using AsyncAPI, its documentation is available to aid you through.

References

https://www.asyncapi.com/docs/specifications/v2.0.0

https://github.com/asyncapi/spec

https://www.avenga.com/magazine/async-api/

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Illuminate your career 💡

Don’t let Zombie EC2 instances run wild!

Terraform Tutorials: Frequently Asked Questions

How to migrate your app to Kubernetes?

Feature development for Zomato- Incremental and iterative model

The Standard of Code Review at Sixfold — Effective Communication of Feedback

Building truly cross-functional teams

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Michael J. Garbade

Dr. Michael J. Garbade

Founder & CEO of Education Ecosystem. Serial entrepreneur with experience from Amazon, GE & Rebate Networks, https://www.education-ecosystem.com/

More from Medium

Domain driven design eCommerce

How to use event storming to achieve domain-driven design

BCRS (Bounded Context Read Segregation)

Domain Driven Design | Deep dive into context mapping