DEV Community

Priyanshi Sharma for Decipher Zone

Posted on

Event-Driven Microservices using Spring Boot

With IT systems producing, gathering, and analyzing gallons of data, developers are expected to create complex applications that are highly scalable, resilient, and available more cheaply and quickly.

Different architectures and programming paradigms like DevOps, microservices, and event-driven architecture get used to achieving these goals. Moreover, developers are now combining microservices with event-driven architecture to make project delivery exceptional by making applications highly concurrent, scalable, fault-tolerant, and easy to maintain.

Although event-driven microservices architecture might sound easy to implement, understanding different technologies and methods to make it successful can be challenging.

So, in this blog, we will discuss the basic concepts of event-driven microservices and how you can implement them using Spring Cloud Stream.

An Overview of Microservices & Event-Driven Architecture

Microservices is not a layer in a monolithic architecture. Microservices are self-contained, distributed, clear-interfaced, de-coupled, independent services that perform well-defined tasks. Therefore, microservices can be built, tested, and deployed independently.

Microservices have been widely adopted by development teams for monolithic projects that need to be broken down into self-contained, independent services. Put simply, microservice is the architectural style to develop an application as a small services suite where each service has processes of its own. APIs allow these services to communicate with one another.

Event-Driven Architecture has existed for a long time. In an event-driven architecture, the application’s decoupled services are triggered by events to communicate. An event refers to the changes in the state by actions performed by users.

As the components that send notifications don’t know about receiving services’ identities while compiling, EDA is more loosely coupled than the client and server paradigm.

An event-driven architecture consists of event producers and event consumers. While event producers create streams of events, event consumers listen for the events. This architecture might work using either event streaming or the pub/sub model.

In the Publish/Subscribe model, the messaging infrastructure keeps track of subscriptions that helps in sending the event to the subscriber once it’s published. However, once the event is received and read by the subscriber, it cannot be replayed so the new subscribers will not see the previous events.

In the Event Streaming model, events are written to a log and are strictly ordered as well as durable. It helps clients to read from any part of the stream instead of subscribing to it.

To make it easier to understand how Microservices and Event-Driven Architectures compliment each other:

Microservices Event-Driven Architecture
Bounded context which provides separation of concerns. Loose coupling between components/services.
Supports polyglot programming. Processing components that can be developed independently.
Services can be deployed and scaled independently. Allows independent scalability of components.
Microservices are cloud native. EDA has a high cloud relationship.
Has elastic scalability. Has asynchronous nature and ability to throttle workload.
Better observability to detect failures. Better resilience and fault tolerance.
Provides a repository of reusable implementation patterns. Provides proven enterprise integration patterns.

By combining event-driven architecture with microservices, developers can develop highly scalable, distributed, fault-tolerant, available, and extensible systems.

What are Event-Driven Microservices?

In the event-driven architecture, a microservice/service publishes an event for communication when something crucial happens. Once the event is published by a microservice, other microservices also subscribe to those events. After receiving an event, the microservice can update its business entities leading to more events being published. Now, these events can be used to implement transactions to span different services.

A transaction in event-driven microservices consists of multiple steps where each step includes microservices updating the business entities for publishing events to trigger consecutive steps. Topics and queues are used for the exchange of events between microservices. Since microservices subscribe to queues and topics, at the time an event is published on the messaging service, the targeted microservice gets notified immediately to perform the requested tasks without external invocations.

The event-driven microservice architecture enables developers to implement transactions that traverse multiple services and deliver consistency. With event-driven microservices, material views can be maintained in an application.

Although event-driven microservice architecture is great, without a proper framework to write the source that helps in working with event messaging platforms, it can become messy.

For instance, if you want to write the logic for the event-driven microservices application, the boilerplate messaging code can make the work more difficult.

So, which framework can bring you out of the mess? Let’s check that out in the next section.

Spring Cloud Stream: The Solution

Spring Cloud Stream is a framework that is used by Java/Kotlin developers to build highly scalable event-driven microservices that are connected to the shared messaging system. It is a flexible model built on previously established Spring Boot best practices and idioms.

It offers flexible messaging abstraction that will handle the complex integration of messaging platforms and allow you to focus on the clear logic of a business. It brings a plethora of messaging platforms like Apache Kafka, RabbitMQ, Google Pub/Sub, Azure Event Hubs, Amazon Kinesis, and Apache RoketMQ behind one easy-to-use API.

Spring Cloud Stream also removes any minor differences in features or approaches between these platforms, so that you can create innovative event-driven applications.

Put simply, Spring Cloud Stream is a framework by Spring that abstracts boilerplate code and setup messaging services to enable developers to focus on the business logic and put it on the topic/queue without writing lengthy codes for setting up these messaging systems.

The core building blocks of SCS are Destination Binders, Destination Bindings, and Messages. While destination binders provide integration with external messaging systems, destination bindings bridge the gap between application code and external messaging systems. Finally, the message block is the canonical data structure that is used to communicate with Destination Binders by producers and users.

Running The Event-Driven Microservices Demo Application

Before running the event-driven microservices application, you need to install the latest versions of Java, Docker (to locally run Kafka or RabbitMQ), Git (optional), and Bash/Powershell on your PC.

Start with cloning the code repository from GitHub by opening a new terminal window and writing the following command:

git clone https://github.com/benwilcock/spring-cloud-stream-demo.git
Enter fullscreen mode Exit fullscreen mode

This repository by Spring will contain two microservices: Loansouce that will act as an event message source and Loancheck acts as a loan processor.

Now, in a fresh terminal, move to the project’s root folder and write the command as follows to start Kafka:

./start-servers.sh
Enter fullscreen mode Exit fullscreen mode

Then write -Pkafka to choose Kafka as a messaging platform.

Open a new terminal window, make /loansource current directory through cd and write the command as follows to start the microservice:

./mvnw clean package spring-boot:run -DskipTests=true -Pkafka
Enter fullscreen mode Exit fullscreen mode

In a new terminal window, make /loancheck the current directory and write the following command to process the events of loan application:

./mvnw clean package spring-boot:run -DskipTests=true -Pkafka
Enter fullscreen mode Exit fullscreen mode

Once this app starts, you will see a message stating Pending before it gets approved or declined.

Now that you have processed both microservices of the loan app, press Ctrl-C in each terminal window to stop the demo.

Conclusion

So that was all for event-driven microservices, and we hope you enjoyed the blog.

To conclude, developers can combine microservices and event-driven architecture to create a web application that is distributed, fault-tolerant, highly scalable, highly available, and provides high throughput. These event-driven microservices applications will be able to process a vast amount of data.

Source: Decipher Zone

Top comments (0)