- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 381字
- 2021-07-02 19:45:04
Reactive microservices using Spring Boot and RabbitMQ
In an ideal case, all microservice interactions are expected to happen asynchronously using publish subscribe semantics. Spring Boot provides a hassle-free mechanism to configure messaging solutions:

In this next example, we will create a Spring Boot application with a sender and receiver, both connected through an external queue.
The full source code of this example is available as the chapter3.bootmessaging project in the code files of this book under the following Git repository: https://github.com/rajeshrv/Spring5Microservice
Let us follow these steps to create a string boot reactive microservice using RabbitMQ:
- Create a new project using STS to demonstrate this capability. In this example, instead of selecting Web, select AMQP under I/O:

- RabbitMQ will also be needed for this example. Download and install the latest version of RabbitMQ from https://www.rabbitmq.com/download.html. RabbitMQ 3.5.6 is used in this book.
- Follow the installation steps documented on the site. Once ready, start the RabbitMQ server like this:
$./rabbitmq-server
- Make the configuration changes to the application.properties file to reflect the RabbitMQ configuration. The following configuration uses the default port, username, and password of RabbitMQ:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
- Add a message sender component and a queue named TestQ of the type org.springframework.amqp.core.Queue to Application.java under src/main/java. The RabbitMessagingTemplate template is a convenient way to send messages, and abstracts all the messaging semantics. Spring Boot provides all boilerplate configurations for sending messages:
@Component
class Sender {
@Autowired
RabbitMessagingTemplate template;
@Bean
Queue queue() {
return new Queue("TestQ", false);
}
public void send(String message){
template.convertAndSend("TestQ", message);
}
}
- For receiving a message, all that is needed is a @RabbitListener annotation. Spring Boot auto-configures all required boilerplate configurations:
@Component
class Receiver {
@RabbitListener(queues = "TestQ")
public void processMessage(String content) {
System.out.println(content);
}
}
- The last piece of this exercise is to wire the sender to our main application, and implement the CommandLineRunner interface's run method to initiate the message sending. When the application is initialized, it invokes the run method of the CommandLineRunner:
@SpringBootApplication
public class Application implements CommandLineRunner{
@Autowired
Sender sender;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
sender.send("Hello Messaging..!!!");
}
}
- Run the application as a Spring Boot application, and verify the output. The following message will be printed on the console:
Hello Messaging..!!!
推薦閱讀
- 觸·心:DT時代的大數(shù)據(jù)精準(zhǔn)營銷
- 深度學(xué)習(xí)經(jīng)典案例解析:基于MATLAB
- MySQL 8從入門到精通(視頻教學(xué)版)
- ASP.NET Core Essentials
- Learning Elixir
- Python機器學(xué)習(xí)實戰(zhàn)
- bbPress Complete
- 用戶體驗增長:數(shù)字化·智能化·綠色化
- 51單片機C語言開發(fā)教程
- Java Web從入門到精通(第3版)
- Node.js從入門到精通
- Java程序設(shè)計教程
- Visual Basic程序設(shè)計實驗指導(dǎo)及考試指南
- Vue.js 3.x高效前端開發(fā)(視頻教學(xué)版)
- Learning ROS for Robotics Programming