Routing TO Consumer
Routing may be accomplished by using the RoutingFunction, which is provided in Spring Cloud Function 3.0. All you have to do is activate it using the application parameter –spring.cloud.stream.function.routing.enabled=true or give the spring.cloud.function.routing-expression.
Message headers
Message header means passing some message or value to the header. By setting the spring.cloud.function.routing-expression header to value “even” and “odd” will end up routing request to either odd or even functions.
Java
@SpringBootApplication public class GFGApplication { public static void main(String[] args) { // Start the Spring Boot application with Cloud Stream function routing enabled SpringApplication.run(GFGApplication. class , "--spring.cloud.stream.function.routing.enabled=true" ); } // Bean definition for handling even numbers @Bean public Consumer<String> evenConsumer() { return value -> { // Print a message indicating that the value is even System.out.println( "EVEN: " + value); }; } // Bean definition for handling odd numbers @Bean public Consumer<String> oddConsumer() { return value -> { // Print a message indicating that the value is odd System.out.println( "ODD: " + value); }; } } |
Application properties
It is possible to send the spring.cloud.function.definition or spring.cloud.function.routing-expression as application properties spring.cloud.function.routing-expression=headers[‘type’] is one example.
Java
@SpringBootApplication public class RoutingStreamApplication { public static void main(String[] args) { // Start the Spring Boot application with a routing expression for determining even or odd SpringApplication.run(RoutingStreamApplication. class , "--spring.cloud.function.routing-expression=" + "T(java.lang.System).nanoTime() % 2 == 0 ? 'even' : 'odd'" ); } // Bean definition for handling even numbers @Bean public Consumer<Integer> evenConsumer() { return value -> { // Print a message indicating that the value is even System.out.println( "EVEN: " + value); }; } // Bean definition for handling odd numbers @Bean public Consumer<Integer> oddConsumer() { return value -> { // Print a message indicating that the value is odd System.out.println( "ODD: " + value); }; } } |
Spring Cloud Stream – Event Routing
Spring Cloud Stream Event Routing is the ability to route events to a specific event subscriber or a specific destination. Routes ‘TO’ and ‘FROM’ will be used here. Using the Spring Cloud Stream architecture, developers may create extremely scalable event-driven microservices that are integrated with common messaging platforms. With support for persistent pub/sub semantics, consumer groups, and stateful partitions, among other features, the framework offers a versatile programming paradigm that is based on well-known and established Spring idioms and best practices.
Dependencies to Spring Cloud Stream
First, we must add the appropriate binder implementation library in our application before we can activate Spring Cloud Stream. The spring-cloud-stream-binder-rabbit artifact is required as we are connecting with RabbitMQ. There are no further dependencies that need to be included because it references all other necessary libraries.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
Contact Us