我在ec2实例中运行了Rabbitmq。我有两个springboot应用程序,一个将消息推送到队列。我将connectionFactory用于第一个推送到队列中,并且工作正常
connectionFactory.setUri("amqp://guest:guest@ec2host:5672/%2F");
第二个尝试连接到rabbitmq stomp端口,类似这样
registry.setApplicationDestinationPrefixes("/app");
registry.enableStompBrokerRelay("/topic").setRelayHost("ec2host").setRelayPort(61613).setClientLogin("guest")
.setClientPasscode("guest");
当我运行代码时,它会运行,但会不断抛出此错误消息
Attempting to connect to: [localhost:5672]
Consumer raised exception, processing can restart if the connection factory supports it.
Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException:
Connection refused (Connection refused)
2020-06-06 22:34:10.030 INFO 19749 --- [tContainer#0-85]
o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@71f987d1: tags=[[]],
channel=null, acknowledgeMode=AUTO local queue size=0
2020-06-06 22:34:10.030 INFO 19749 --- [tContainer#0-86] o.s.a.r.c.CachingConnectionFactory
尽管我将主机集保存到ec2,但我不知道为什么它要尝试连接localhost:5672。
侦听队列的类
public class MessageRedirect {
@Autowired
private SimpMessageSendingOperations messagingTemplate;
@RabbitListener(queues = "upload-queue")
@SendTo("/{institution}/notification")
public String sendNotification(String notification) throws JsonMappingException, JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Notification not = objectMapper.readValue(notification, Notification.class);
//messagingTemplate.convertAndSend("/topic/"+not.getInstitution(), not.getMessage());
messagingTemplate.convertAndSend("/topic/"+not.getInstitution() + ".student", not.getMessage());
System.out.println("SENDING MESSAGE TO upload-queue" + "/"+ not.getInstitution()+"/notification");
return "NOTIFICATION SENT";
}
}
[@RabbitListener(queues = "upload-queue")
默认情况下尝试连接到本地主机。我刚刚配置了ec2host,它就可以工作了
@Bean
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config) throws Exception{
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.getRabbitConnectionFactory().setUri("amqp://guest:guest@ec2host/%2F");
return connectionFactory;