RabbitMQ与springboot整合

本文根据视频https://www.bilibili.com/video/BV1pa4y1x7Kc?p=13, https://www.bilibili.com/video/BV1pa4y1x7Kc?p=14和https://www.bilibili.com/video/BV1pa4y1x7Kc?p=15 创作。未经视频作者授权,如果作者感到侵权,请联系本人删除此文

1. 引入maven依赖, 包括web依赖

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-amqp
        

        
            org.springframework.amqp
            spring-rabbit-test
            test
        
    

2. 配置rabbitmq信息

server.port=8081
spring.application.name=rabbitmq-producer
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/

3. 创建rabbitmq配置(示例采用topic交换机)

 

@Configuration
public class RabbitMQConfig {

    /**
     * 创建交换机,指定交换机名称
     */
    @Bean
    public Exchange topicsEchange() {
        //durable(true) 持久化,mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(StaticConstant.EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    /**
     * 创建队列,指定队列名称
     */
    @Bean
    public Queue emailQueue() {
        return new Queue(StaticConstant.QUEUE_INFORM_MAIL);
    }

    /**
     * 创建队列,指定队列名称
     */
    @Bean
    public Queue smsQueue() {
        return new Queue(StaticConstant.QUEUE_INFORM_SMS);
    }

    //绑定交换机,指定routingKey
    @Bean
    public Binding emailBinding(@Autowired Queue emailQueue, @Autowired Exchange topicsEchange) {
        return BindingBuilder.bind(emailQueue).to(topicsEchange).with(StaticConstant.TOPICS_ROUTINGKEY_EMAIL).noargs();
    }

    //绑定交换机,指定routingKey
    @Bean
    public Binding smsBinding(@Autowired Queue smsQueue, @Autowired Exchange topicsEchange) {
        return BindingBuilder.bind(smsQueue).to(topicsEchange).with(StaticConstant.TOPICS_ROUTINGKEY_SMS).noargs();
    }
}

4.  生产者发送消息

@Service
public class ProducerTopicsServiceImpl implements ProducerTopicsService {

    private static final String EMAIL_ROUTING_NAME = "inform.mail";

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendEmail() {
        String message = "send email to user";
        User user = new User();
        user.setName("庆成");
        user.setAge("28");

        /**
         * 参数:
         * 1.交换机名称
         * 2. routingKey
         * 3. 消息内容
         */
        rabbitTemplate.convertAndSend(StaticConstant.EXCHANGE_TOPICS_INFORM, EMAIL_ROUTING_NAME, user);
    }
}

5. 消费者接收消息

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @RabbitListener(queues = {StaticConstant.QUEUE_INFORM_MAIL})
    @Override
    public void receiveEmail(Message message, Channel channel) throws IOException {
//        User messages = null;
//        ObjectMapper om = new ObjectMapper();
//        messages = om.readValue(message.getBody(), User.class);
        System.out.println(new String(message.getBody(), Charset.defaultCharset()));
    }
}

 

你可能感兴趣的:(RabbitMQ)