Build a RabbitMQ Consumer with Spring Cloud Stream in Spring Boot
This tutorial walks through creating a Spring Boot microservice that uses Spring Cloud Stream to bind to RabbitMQ, covering project setup, required dependencies, a simple consumer implementation, application startup, log verification, core annotations, and a unit test for message production.
Spring Cloud Stream is a lightweight framework that integrates Spring Boot and Spring Integration to provide message‑driven capabilities for microservices, automatically configuring supported brokers such as RabbitMQ and Kafka.
Quick‑start example
The example builds a Spring Boot application that consumes messages from RabbitMQ and logs them. Ensure RabbitMQ is running locally before starting.
Project setup
Create a basic Spring Boot project (e.g., stream-hello) and add the following Maven dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Consumer implementation
Create a class that receives messages from the input channel:
@EnableBinding(Sink.class)
public class SinkReceiver {
private static final Logger logger = LoggerFactory.getLogger(SinkReceiver.class);
@StreamListener(Sink.INPUT)
public void receive(Object payload) {
logger.info("Received: " + payload);
}
}Application entry point
@SpringBootApplication
public class SinkApplication {
public static void main(String[] args) {
SpringApplication.run(SinkApplication.class, args);
}
}Sink interface
public interface Sink {
String INPUT = "input";
@Input(Sink.INPUT)
SubscribableChannel input();
}Running and verifying
Start RabbitMQ, then run the Spring Boot application. The startup logs show the binder declaring an anonymous queue (e.g., input.anonymous.Y8VsFILmSC27eS5StsXp6A) and creating a connection to [email protected]:5672. Publishing a message to that queue via the RabbitMQ management console triggers the receive method, which logs the payload (e.g., Received: B@7cba610e).
The same queue can be inspected in the RabbitMQ UI, and messages can be sent using the "Publish Message" feature.
Core Spring Cloud Stream annotations
@EnableBinding– binds one or more interfaces that declare @Input or @Output channels. @StreamListener – registers a method as a listener for a specific input channel. @Input / @Output – declare channel names on an interface.
These annotations together allow the application to automatically create the necessary RabbitMQ bindings and handle message flow without manual boilerplate.
Unit test for producing messages
A test class demonstrates how to send a message to the same channel, verifying the consumer works end‑to‑end:
@RunWith(SpringRunner.class)
@EnableBinding(value = {SinkApplicationTests.SinkSender.class})
public class SinkApplicationTests {
@Autowired
private SinkSender sinkSender;
@Test
public void sinkSenderTester() {
sinkSender.output().send(
MessageBuilder.withPayload("produce a message :http://blog.didispace.com").build()
);
}
public interface SinkSender {
String OUTPUT = "input";
@Output(SinkSender.OUTPUT)
MessageChannel output();
}
}Running this test results in the consumer logging the sent payload, confirming the producer‑consumer pair works correctly.
Conclusion
The tutorial provides a complete, runnable example of using Spring Cloud Stream with RabbitMQ to build a message‑driven microservice. By following the steps—adding the starter dependency, defining a sink interface, implementing a consumer, and optionally writing a producer test—developers can quickly adopt Spring Cloud Stream for reliable, declarative messaging in Spring Boot applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
