I haven't written Spring Boot for a long time. I happened to be writing Spring Cloud Bus recently. Because the content will have some relevance, I will add a post about the integration of AMQP.
Introduction to Message Broker and AMQP
Message Broker is an architectural model for message verification, transmission and routing. Its design goals are mainly used in the following scenarios:
AMQP is the abbreviation of Advanced Message Queuing Protocol, an open standard application layer protocol for message middleware. AMQP defines these features:
RabbitMQ
The RabbitMQ that is to be introduced in this article is a middleware product implemented with the AMQP protocol. It can support multiple operating systems and multiple programming languages, and can cover almost all mainstream enterprise-level technology platforms.
Install
In the download page of RabbitMQ official website https://www.rabbitmq.com/download.html, we can obtain installation packages and instructions for various operating systems. Here, we will explain several commonly used platforms one by one.
The following are the version descriptions of the Erlang and RabbitMQ Server we use:
Windows Installation
Install Erland, get the exe installation package through the official download page http://www.erlang.org/downloads, open it directly and complete the installation.
Install RabbitMQ and get the exe installation package through the official download page https://www.rabbitmq.com/download.html.
After the download is completed, run the installer directly.
After the installation of RabbitMQ Server is completed, it will automatically register as a service and start with the default configuration.
Mac OS X installation
Using brew tool in Mac OS X can easily install RabbitMQ server, just follow the following command:
Through the above command, the RabbitMQ Server command will be installed in /usr/local/sbin and will not be automatically added to the user's environment variables, so we need to add the following content to the .bash_profile or .profile file:
PATH=$PATH:/usr/local/sbin
In this way, we can start the RabbitMQ server through the rabbitmq-server command.
Ubuntu installation
In Ubuntu, we can use the APT repository to install
Install Erlang and execute: apt-get install erlang
Execute the following command to add an APT repository to /etc/apt/sources.list.d
echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
Update the package list of the APT repository and execute the sudo apt-get update command
Install Rabbit Server and execute the sudo apt-get install rabbitmq-server command
Rabbit Management
We can manage it directly through configuration file access, or through web access. Below we will introduce how to manage through the web.
Execute the rabbitmq-plugins enable rabbitmq_management command to enable the web management plug-in so that we can manage it through the browser.
> rabbitmq-plugins enable rabbitmq_managementThe following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_managementApplying plugin configuration to rabbit@PC-201602152056... started 6 plugins.
Open the browser and visit: http://localhost:15672/, and log in with the default user guest, and the password is also guest. We can see the management page in the following figure:
From the figure, we can see some basic concepts mentioned in the previous chapter, such as: Connections, Channels, Exchanges, Queue, etc. Readers who use it for the first time can click on it to see what content they have and become familiar with the server side of RabbitMQ Server.
Click the Admin tab to manage users here.
Spring Boot Integration
Below, we have an intuitive feeling and understanding of RabbitMQ by integrating RabbitMQ in Spring Boot application and implementing a simple example of sending and receiving messages.
Integrating RabbitMQ in Spring Boot is very easy, because we have introduced Starter POMs before, and the AMQP module can support RabbitMQ well. Let's talk about the integration process in detail:
Create a new Spring Boot project, named "rabbitmq-hello".
In pom.xml, the following dependencies are introduced, where spring-boot-starter-amqp is used to support RabbitMQ.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>
Configure connection and user information about RabbitMQ in application.properties. Users can go back to the above installation content and create users in the management page.
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=springspring.rabbitmq.password=123456
Create message producer Sender. The AmqpTemplate interface defines a set of basic operations for the AMQP protocol by injecting an instance of the AmqpTemplate interface. In Spring Boot, its specific implementation will be injected according to the configuration. In this producer, we generate a string and send it to a queue called hello.
@Componentpublic class Sender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hello " + new Date(); System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("hello", context); }} Create message consumer Receiver. The @RabbitListener annotation defines the listening of the hello queue of this class, and uses the @RabbitHandler annotation to specify the processing method for the message. Therefore, the consumer realizes consumption of the hello queue, and the consumption operation is the string content of the output message.
@Component@RabbitListener(queues = "hello")public class Receiver { @RabbitHandler public void process(String hello) { System.out.println("Receiver : " + hello); }} Create RabbitMQ configuration class RabbitConfig, which is used to configure advanced information such as queues, switches, and routing. Here we focus on getting started and first define it with a minimizing configuration to complete a basic production and consumption process.
@Configurationpublic class RabbitConfig { @Bean public Queue helloQueue() { return new Queue("hello"); }} Create the main application class:
@SpringBootApplicationpublic class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); }} Create a unit test class to call message production:
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = HelloApplication.class)public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); }}After completing the program writing, try to run it. First make sure that the RabbitMQ Server has started, and then perform the following operations:
Start the application main class, from the console, we see the following content, the program creates a connection to access springcloud in 127.0.0.1:5672.
Copy the code as follows: osarcCachingConnectionFactory: Created new connection: SimpleConnection@29836d32 [delegate=amqp://[email protected]:5672/]
At the same time, through the RabbitMQ control panel, we can see that the entries containing the current connection in Connection and Channels.
Run the unit test class and we can see the following output in the console, and the message is sent to the hello queue of the RabbitMQ Server.
Sender : hello Sun Sep 25 11:06:11 CST 2016
Switch to the console of the main class of the application, we can see the following output, the consumer executes the listener program of the hello queue and outputs the received message information.
Receiver : hello Sun Sep 25 11:06:11 CST 2016
Through the above example, we introduced the spring-boot-starter-amqp module in Spring Boot application, and simply configured to complete the development content of RabbitMQ message production and consumption. However, in actual applications, we still have a lot of content that has not been demonstrated, so we will not give more explanations here. Readers can check the official RabbitMQ tutorials on their own to gain a more comprehensive understanding.
Complete example: Chapter 5-2-1
Open Source China: http://git.oschina.net/diidispace/SpringBoot-Learning/tree/master/Chapter5-2-1
GitHub: https://github.com/dyc87112/SpringBoot-Learning/tree/master/Chapter5-2-1
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.