spingboot使用rabbitmq

1.引入依赖

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

2.yml配置

spring:
  rabbitmq:
    host: 47.115.14.7
    port: 9903
    username: guest
    password: guest

3.生产者

@Component
public class ProduceSchedule {
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Scheduled(fixedRate = 1000)
    public void send() {
        String msg = "MY_QUEUE send ..." + System.currentTimeMillis();
        amqpTemplate.convertAndSend(QueueNames.MY_QUEUE, msg);
    }
}

4.消费者

@Component
@Slf4j
public class Receiver {

    @RabbitListener(queuesToDeclare = @Queue(QueueNames.MY_QUEUE))
    public void receive(String msg) {
        log.info("MY_QUEUE receive:{}", msg);
    }
}

 

上一篇:Tars | TarsJava SpingBoot启动与负载均衡源码初探


下一篇:Vue +Spingboot + MySQL制作搜索引擎(一)项目概述