Skip to main content

Installation and use of RabbtiMQ in Springboot! ! !

Installation and use of RabbtiMQ in Springboot! ! !

1. Install Erlang and Rabbitmq

[Installation tutorialThis tutorial was tested under centos8. In fact, Linux systems are similar. RabbitMQ official: Messaging that just works - RabbitMQ RabbitMQ is an open source AMQP implementation. The server side is written in Erlang language, Python, Ruby, NET, Java, JMS, c, PHP, action scrive AMQP (Advanced Message Queuing Protocol) and Advanced Message Queuing Protocol are open standards for APP application layer protocols and are designed for message-oriented middleware. Message middleware is mainly used for decoupling between components. The sender of the message does not need to know the existence of the message user. On the contrary, rabbitmq installationInstallation and use of RabbtiMQ in Springboot! ! !https://blog.csdn.net/weixin_44545251/article/details/128216395](https://blog.csdn.net/weixin_44545251/article/details/128216395 "Installation tutorial")

2. Using rabbitmq in springboot

1. Import dependencies

>     <dependency>
> <groupId>org.springframework.boot</groupId>
> <artifactId>spring-boot-starter-amqp</artifactId>
> </dependency>

2.Write configuration file

>     spring:
> rabbitmq:
> host: localhost
> port: 5672
> username: guest
> password: guest

3. Configure a queue

    
2.

3. import org.springframework.amqp.core.Queue;

4. import org.springframework.context.annotation.Bean;

5. import org.springframework.context.annotation.Configuration;

6.

7. @Configuration

8. public class QueueAndExchangeConfig {

9. @Bean("myFirstQueue")

10. public Queue getFirstQueue(){

11. return new Queue("my-first-queue");

12. }

13. }



4. Write a producer class

    
2.

3. import org.springframework.amqp.rabbit.core.RabbitTemplate;

4. import org.springframework.beans.factory.annotation.Autowired;

5. import org.springframework.stereotype.Controller;

6. import org.springframework.web.bind.annotation.GetMapping;

7. import org.springframework.web.bind.annotation.RequestMapping;

8. import org.springframework.web.bind.annotation.RestController;

9.

10.

11. @RestController

12. @RequestMapping("producer")

13. public class RabbitMqController {

14. @Autowired

15. private RabbitTemplate rabbitTemplate;

16.

17. @GetMapping("producerSendFirstQueue")

18. public String sendMsg(String msg){

19. rabbitTemplate.convertAndSend("my-first-queue",msg);

20. return msg;

21. }

22.

23. }




![Installation and use of RabbtiMQ in Springboot! ! !](6b44e99974d17195ee2722671aabdc1f.png)

5. Write a consumer to listen to messages in the queue

    
2.

3. import org.springframework.amqp.rabbit.annotation.RabbitListener;

4. import org.springframework.amqp.rabbit.core.RabbitTemplate;

5. import org.springframework.beans.factory.annotation.Autowired;

6. import org.springframework.stereotype.Component;

7. import org.springframework.web.bind.annotation.GetMapping;

8. import org.springframework.web.bind.annotation.RequestMapping;

9. import org.springframework.web.bind.annotation.RestController;

10.

11.

12. @Component

13. public class ConsumerHandler {

14. @RabbitListener(queues = "my-first-queue")

15. public void getFirstQueue(String msg){

16. System.out.println("消费者1:"+msg);

17. }

18. }




![Installation and use of RabbtiMQ in Springboot! ! !](6b44e99974d17195ee2722671aabdc1f.png)

6. Use interface tools to adjust the production interface

Installation and use of RabbtiMQ in Springboot! ! !

7. View the graphical interface

Installation and use of RabbtiMQ in Springboot! ! !