RabbitMQ(四)项目中使用

1.pom

<!--RabbitMQ-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-amqp</artifactId>
 </dependency>

2.在spring下添加RabbitMQ的配置

 rabbitmq:
   host: localhost
   port: 5672
   username: guest
   password: guest

3.消息的发送者,在service中,

@Autowired
private AmqpTemplate amqpTemplate;

//发送MQ消息
amqpTemplate.convertAndSend("productInfo",list.toString() );

4.消息的接收者

@Component
@Slf4j
public class ProductInfoReceiver {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RabbitListener(queuesToDeclare = @Queue("productInfo"))
    private void process(String message){
        //message > ProductInfoOutput

        log.info("接收到MQ:{}",message);

        //存储到redis
        stringRedisTemplate.opsForValue().set("product",message);
    }
}

你可能感兴趣的:(spring,cloud)