Open In App

Spring Cloud Stream – Demystified and Simplified

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Spring framework was introduced for ease of Java application development. Later spring boot gained popularity for its easy-to-configure nature. To make the framework survive in the industry with demand for event-driver streaming architecture, Spring introduced a new framework named Spring Cloud Stream. People started calling it a lightweight Spring integration, but it is not and never was.

In this article, we will be exploring the Spring Cloud Stream demystifies the purpose of Spring Cloud Stream and will try to simplify the understanding of this framework.

Spring Cloud Stream

In an abstraction, we can say that Spring Cloud Stream is a binding and activation framework. It is a combination of two popular spring frameworks, Spring Boot and Spring integration. Spring cloud stream has spring boot features to create enterprise-grade, stand-alone microservice applications and Spring integration for event-driver message broker connectivity. It binds code to the source or target of data exposed by the binder and activates such code according to the binder’s implementation. Spring Cloud Stream provides a way to build message-driven microservices that are connected by a common messaging infrastructure. In short, we can say that Spring Cloud Stream is a framework for building event-driver stream processing microservices.

Demystifying the Key Concepts of Spring Cloud Stream

1. Binders:

Binders work like a bridge between Spring Cloud Stream applications and messaging middleware like Apache Kafka or RabbitMQ. Binders handle the integration details, allowing developers to focus on the business logic rather spending time on configurations. Spring cloud stream supports multiple binder implementation which helps in providing flexibility for choosing the messaging system that suits better.

2. Channels:

Channels are the communication endpoints within the Spring Cloud Stream application. Channels are also of two types, one is input channel where messages are received, another one is the output channel where messages are sent. Channels are automatically configured or bound to the destination in a messaging system. This significantly helps in simplifying the communication configuration.

3. Messages:

Messages are the data or better to say, unit of data which is exchanged between Spring Cloud Stream Applications. Each message is a combination of actual data or payload and headers(optional) or the metadata. Messages are passed through channels. This enables communication between microservices.

Simplifying Spring Cloud Stream

In Spring framework, we always talk about reducing the boilerplate code. In case of Spring Cloud Stream applications, there has been a shift towards a function-based programming architecture as opposed to the traditional annotation-based approach. This shift is aligned with Spring Boot principles and simplifies the development.

Annotation based approach:

Java




@SpringBootApplication
@EnableBinding(Processor.class)
public class MyApplication {
      
    @StreamListener(Processor.INPUT)
    @SendTo(Processor.OUTPUT)
    public String uppercase(String value) {
        return value.toUpperCase();
    }
  
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}


In this annotation-based approach, the @EnableBinding annotation is used to specify the messaging channels. @StreamListener annotation is used to make a method as listener for messages arriving on the input channels. @SendTo is used to specify the output channel where the processed message should be sent.

Function based approach:

Java




@SpringBootApplication
public class MyApplication {
  
    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }
  
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}


Here as we can see we are not using any of the annotations like EnableBinding or StreamListener, instead functions are defined as Java beans, where each function acts as a processor that take an input and provides output. Here we have taken upperCase() method as an example which tales a string input and returns its uppercase version as output.

Both the techniques used here are correct and provides same output for processed message. The function-based programming model here simplifies the development process by reducing explicit annotations which improves development experience.

Conclusion

Spring cloud stream is a powerful framework for developing applications following microservices architecture. As we have discussed in the article, you can use annotation-based functionalities or go with function-based approach. The function-based approach in Spring Cloud Stream simplifies the development of event-driven microservices and aligns better with the framework’s goal of not getting involved in configuration and developing business logic.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads