Introducing Spring Cloud Streaming

Spring Cloud Stream is a framework to build message-driven microservice applications. It abstracts away the message producer and consumer code from message-broker-specific implementations. Spring Cloud Stream provides input and output channels to service communications to the outside world. Spring Cloud Stream is created on top of Spring Boot, it can create a standalone and production-grade applications.Spring Integration provides the message broker's connectivity to the Spring Cloud Stream. Message brokers, such as Kafka and RabbitMQ, can be added easily by just injecting a binding dependency to the code of your application.

Let's see the Maven dependency for Spring Cloud Stream:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-reactive</artifactId>
</dependency>

In the preceding Maven dependency, we have added the Spring Cloud Stream dependency reactive model. Let's see how to enable the application to connect with the message broker:

@EnableBinding(NotificationStreams.class)
public class StreamsConfig {
}

In the preceding code, the @EnableBinding annotation is used to enable connectivity between the application and message broker. This annotation takes one or more interfaces as parameters, in our case, we have passed the NotificationStreams interface as a parameter, let's see this interface:

public interface NotificationStreams {
String INPUT = "notification-in";
String OUTPUT = "notification-out";
@Input(INPUT)
SubscribableChannel subscribe();
@Output(OUTPUT)
MessageChannel notifyTo();
}

As you can see, the interface declares input and/or output channels. This is our custom interface in this example but you can also use interfaces such as Source, Sink, and Processor, provided by the Spring Cloud Stream:

  • Source: This interface can be used for an application that has a single outbound channel
  • Sink: This interface can be used for an application that has a single inbound channel
  • Processor: This interface can be used for an application that has both an inbound and an outbound channel

And also in the preceding code, the @Input annotation is used to identify an input channel by using this identifier it receives message which enter to the application. Similarly, the @Output annotation is used to identify an output channel, by using this identifier, published messages leave the application.

The @Input and @Output annotations take the name parameter as a channel name, if name is not provided, then by default name of the annotated method will be used. In this application, we have used Kafka as a message broker. Let's learn more about Kafka.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.138