要将RabbitMQ与Spring Boot整合使用,可以按照以下步骤操作:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring,rabbitmq.username=guest
spring.rabbitmq.password=guest
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQProducer {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String message) {
amqpTemplate.convertAndSend("myQueue", message);
System.out.println("Message sent: " + message);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQConsumer {
@RabbitListener(queues = "myQueue")
public void receiveMessage(String message) {
System.out.println("Message received: " + message);
}
}
通过以上步骤,就可以实现RabbitMQ与Spring Boot的整合使用。