spring 使用RabbitMQ tut1教程时无法获取rabbitmq发送器以打印出消息

uqjltbpv  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(208)

我有一个链接到我的github仓库为这个项目在这里:https://github.com/luiz-miotto/rabbitmqTut1v4
我一直在尝试使用RabbitMQ提供的教程学习如何在Spring AMQP中使用RabbitMQ:https://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html
我遇到的问题是,在运行项目时,我没有得到打印输出,也没有从发送方到接收方类的通信。
为了运行这个命令,我在终端中运行两个jar命令:java -Dserver.port=8889 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,sender

java -Dserver.port=8888 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,receiver
运行这些jar告诉我,它们已准备就绪并正在运行,并且The following 2 profiles are active: "hello-world", "receiver"
然而,我得到打印输出到我的终端,我也没有看到任何在我的RabbitMQ UI(我有RabbitMQ运行,如果有人问)
我怀疑这个问题与@Profile有关,因为我无法打印出接收者或发送者类,但我不确定。
我试着在Tut 1Config类的sender方法中添加日志记录,看看是否调用了它,但我没有看到任何日志记录被注销。
包com.example.rabbitmq4.tut1;

  1. import org.springframework.amqp.core.Queue;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Profile;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. @Profile({"tut1","hello-world"})
  7. @Configuration
  8. public class Tut1Config {
  9. @Bean
  10. public Queue hello(){
  11. return new Queue("hello");
  12. }
  13. @Profile("receiver")
  14. @Bean
  15. public Tut1Receiver receiver(){
  16. return new Tut1Receiver();
  17. }
  18. @Profile("sender")
  19. @Bean
  20. public Tut1Sender sender(){
  21. System.out.println("this shit working for sender?");
  22. return new Tut1Sender();
  23. }
  24. }
  1. package com.example.rabbitmq4;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.context.ConfigurableApplicationContext;
  6. public class RabbitAmqpTutorialsRunner implements CommandLineRunner {
  7. @Value("${tutorial.client.duration:0}")
  8. private int duration;
  9. @Autowired
  10. private ConfigurableApplicationContext ctx;
  11. @Override
  12. public void run(String... arg0) throws Exception {
  13. System.out.println("Ready ... running for " + duration + "ms");
  14. Thread.sleep(duration);
  15. ctx.close();
  16. }
  17. }
  1. import org.springframework.amqp.core.Queue;
  2. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. import org.springframework.scheduling.annotation.Scheduled;
  6. import org.springframework.stereotype.Component;
  7. public class Tut1Sender {
  8. @Autowired
  9. private RabbitTemplate template;
  10. @Autowired
  11. private Queue queue;
  12. @Scheduled(fixedDelay = 1000, initialDelay = 500)
  13. public void send() {
  14. String message = "Hello World!";
  15. this.template.convertAndSend(queue.getName(), message);
  16. System.out.println(" [x] Sent '" + message + "'");
  17. }
  18. }
rsaldnfx

rsaldnfx1#

我已经运行了你的代码,正如Reveson所说,你在application.yml中遗漏了一些配置。像那个教程一样做:

  1. spring:
  2. profiles:
  3. active: usage_message
  4. logging:
  5. level:
  6. org: ERROR
  7. tutorial:
  8. client:
  9. duration: 10000

字符串
如果你在Rabbitmq4Application.java类中有错误的包名,请修改:

  1. package com.example.rabbitmq4.tut1;


对此:

  1. package com.example.rabbitmq4;


我使用docker-compose文件运行Rabbitmq如下:

  1. version: "3.2"
  2. services:
  3. rabbitmq:
  4. image: rabbitmq:3-management-alpine
  5. container_name: "rabbitmq"
  6. ports:
  7. - 5672:5672
  8. - 15672:15672
  9. volumes:
  10. - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
  11. - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
  12. networks:
  13. - rabbitmq
  14. restart: unless-stopped
  15. networks:
  16. rabbitmq:
  17. driver: bridge


我在终端和Rabbitmq managementUI中看到过消息。但是请注意,当应用程序从队列中读取消息时,它将从队列中删除。因为你在终端中获得了print,所以你有队列,但是如果你想在Rabbitmq managementUI中看到消息,你只能运行发送者。

展开查看全部

相关问题