0


简单示范RabbitMQ在Spring Cloud中的使用

介绍

在spring cloud的项目中使用RabbitMQ之前得先给自己的电脑配置RabbitMQ

超详细的RabbitMQ入门,看这篇就够了!-阿里云开发者社区

我是根据该篇文章配置的RabbitMQ。

下载RabbitMQ

先配置语言环境,一直next就行

Otp 20.0 - Erlang/OTP

编辑erl语言的环境变量

配置完后进入cmd查询语言配置是否成功。

版本记得对应上,erl和rabbitmq的版本兼容问题记得注意一下,或者跟着我的配也可以。

我选的RabbitMQ的版本是3.7.3。

之后一直next就行,然后找到安装路径后操作,打开对应栏的控制台然后输入

rabbitmq-plugins enable rabbitmq_management

安装插件

安装完插件后记得用命令启动(shell)

rabbitmqctl status

之后输入网址http://localhost:15672,账号密码默认是:guest/guest

SpringCloud简单实例

整体框架如下图,如果有什么不懂可以看看我之前的文章,从零开始搭建一个属于自己的SpringCloud项目

生产者和服务者的模块都得导入相关的RabbitMQ的依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>

在两个模块的

  1. application.yml

中配置 RabbitMQ 的连接信息:

  1. spring:
  2. rabbitmq:
  3. host: localhost # RabbitMQ 主机地址
  4. port: 5672
  5. username: guest
  6. password: guest
  7. queue-name: myQueue # 自定义队列名称

生产者模块(order80)

配置类(RabbitMQconfig)

  1. import org.springframework.amqp.core.Binding;
  2. import org.springframework.amqp.core.BindingBuilder;
  3. import org.springframework.amqp.core.Queue;
  4. import org.springframework.amqp.core.TopicExchange;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class RabbitMQConfig {
  9. // 队列名称
  10. private final String queueName = "myQueue";
  11. @Bean
  12. public Queue queue() {
  13. return new Queue(queueName, true); // 设置队列持久化
  14. }
  15. @Bean
  16. public TopicExchange exchange() {
  17. return new TopicExchange("myExchange");
  18. }
  19. @Bean
  20. public Binding binding(Queue queue, TopicExchange exchange) {
  21. return BindingBuilder.bind(queue).to(exchange).with("routing.key");
  22. }
  23. }

Service类

  1. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class MessageProducer {
  6. private final RabbitTemplate rabbitTemplate;
  7. @Value("${spring.queue-name}")
  8. private String queueName;
  9. public MessageProducer(RabbitTemplate rabbitTemplate) {
  10. this.rabbitTemplate = rabbitTemplate;
  11. }
  12. public void sendMessage(String message) {
  13. rabbitTemplate.convertAndSend(queueName, message);
  14. System.out.println("Sent message: " + message);
  15. }
  16. }

Controller类

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RequestParam;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class MessageController {
  6. private final MessageProducer messageProducer;
  7. public MessageController(MessageProducer messageProducer) {
  8. this.messageProducer = messageProducer;
  9. }
  10. @GetMapping("/send")
  11. public String sendMessage(@RequestParam String message) {
  12. messageProducer.sendMessage(message);
  13. return "Message sent: " + message;
  14. }
  15. }

消费者模块(consumer8001)

接收类

  1. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class MessageConsumer {
  5. @RabbitListener(queues = "${spring.queue-name}")
  6. public void receiveMessage(String message) {
  7. System.out.println("Received message: " + message);
  8. // 可以在这里进行消息处理逻辑
  9. }
  10. }

调试

我之后便在postman中进行调试该接口

可以看出来该接口是可以成功使用的,之后我们便去看看控制台和RabbitMQ的输出与反应,对应如图所示:

控制台的输出与接收:

RabbitMQ的使用:


本文转载自: https://blog.csdn.net/m0_73938805/article/details/143469876
版权归原作者 Demons_kirit 所有, 如有侵权,请联系我们删除。

“简单示范RabbitMQ在Spring Cloud中的使用”的评论:

还没有评论