第一步:在spring boot项目中添加RabbitMq的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
第二步:在配置文件中添加RabbitMq的配置
#配置RabbitMQ
spring.rabbitmq.host=192.168.1.103
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisherConfirms=true
第三步:添加RabbitMq配置类
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
@Configurable
public class RabbitMqConfig {
@Bean
public Queue countPalletBatchQueue(){
return new Queue("count-pallet-batch-queue");
};
}
第四步:添加生产者类
@Component
public class producter {
@Autowired
private AmqpTemplate amqpTemplate;
void create() {
String message = "这是一条消息";
amqpTemplate.convertAndSend("count-pallet-batch-queue",message);
}
}
第五步:添加消费者类
@Component
@Service
@Transactional
public class customer {
@Autowired
private AmqpTemplate amqpTemplate;
@RabbitListener(queues = "count-pallet-batch-queue")
public String custom(String message){
return message;
}
}
本文作者:网友 来源:CSDN
CIO之家 www.ciozj.com 微信公众号:imciow