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

uqjltbpv  于 9个月前  发布在  Spring
关注(0)|答案(1)|浏览(154)

我有一个链接到我的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;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;

@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {

    @Bean
    public Queue hello(){
        return new Queue("hello");
    }

    @Profile("receiver")
    @Bean
    public Tut1Receiver receiver(){
        return new Tut1Receiver();
    }

    @Profile("sender")
    @Bean
    public Tut1Sender sender(){
        System.out.println("this shit working for sender?");
        return new Tut1Sender();
    }
}
package com.example.rabbitmq4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;

public class RabbitAmqpTutorialsRunner implements CommandLineRunner {

    @Value("${tutorial.client.duration:0}")
    private int duration;

    @Autowired
    private ConfigurableApplicationContext ctx;

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Ready ... running for " + duration + "ms");
        Thread.sleep(duration);
        ctx.close();
    }
}
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


public class Tut1Sender {
    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Queue queue;

    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() {
        String message = "Hello World!";
        this.template.convertAndSend(queue.getName(), message);
        System.out.println(" [x] Sent '" + message + "'");
    }
}
rsaldnfx

rsaldnfx1#

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

spring:
  profiles:
    active: usage_message

logging:
  level:
    org: ERROR

tutorial:
  client:
    duration: 10000

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

package com.example.rabbitmq4.tut1;


对此:

package com.example.rabbitmq4;


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

version: "3.2"
services:
rabbitmq:
  image: rabbitmq:3-management-alpine
  container_name: "rabbitmq"
  ports:
    - 5672:5672
    - 15672:15672
  volumes:
    - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
    - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
  networks:
    - rabbitmq
  restart: unless-stopped

networks:
  rabbitmq:
    driver: bridge


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

相关问题