SpringBoot整合RabbitMQ

在这里插入图片描述

使用RabbitMQ

  • 1、引入amqp场景;RabbitAutoConfiguration就会自动生效
  • 2、给容器中自动配置了RabbitTemplate、AmqpAdmin、CachingConnectionFactory、RabbitNessagingTemplate
  • 3、@EnabLeRabbit: @EnableXxXxx、cachingconnectionFactory

1.导入依赖

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

2.启动MQ(启动类添加@EnableRabbit)

/**
 *使用RabbitMQ
 *1、引入amqp场景;RabbitAutoConfiguration就会自动生效*
 *2、给容器中自动配置了*
 RabbitTemplate、AmqpAdmin、CachingConnectionFactory、RabbitMessagingTemplate;
 *
 所有的属性都是spring.rabbitmq
 *
 @configurationProperties(prefix = "spring.rabbitmq")
  *
 pubLic class RabbitProperties
  *
  *3、给配置文件中配置spring.rabbitmq信息*4J @EnableRabbit : @EnableXxxXx;开启功能
 */
@EnableRabbit
@EnableDiscoveryClient // 注册
@SpringBootApplication
public class GulimallOrderApplication {
   
    public static void main(String[] args) {
   
        SpringApplication.run(GulimallOrderApplication.class, args);
    }
}

3.配置application.properties

spring.rabbitmq.host=192.168.56.10
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/

4.测试类,创建交换机、队列、绑定

/**
     *1、如何创建Exchange、Queue、 Binding
     1)、使用AmqpAdmin进行创建
     *2、如何收发消息
     */
    @Autowired
    AmqpAdmin amqpAdmin;

    @Test
    void contextLoads() {
   
        // 创建交换机
        DirectExchange exchange = new DirectExchange("hello-java-exchange",true,false);
        amqpAdmin.declareExchange(exchange);
        log.info("Exchange[{}]创建成功","hello-java-exchange");
    }

    @Test
    void createQueue() {
   
        // 创建队列
        Queue queue = new Queue("hello-java-queue",true,false,false);
        amqpAdmin.declareQueue(queue);
        log.info("Queue[{}]创建成功","hello-java-queue");
    }

    @Test
    void createBinding() {
   
        // 交换机绑定队列
        // (String destination【目的地-队列】,
        //DestinationType destinationType【目的地类型】,/l /String exchange【交换机】,
        //String routingKey【路由键】,
        // Map<String, object> arguments【自定义参数】)
        //将exchange指定的交换机和destination目的地进行绑定,使用routingKey作为指定的路由键
        Binding binding = new Binding(
                "hello-java-queue",
                Binding.DestinationType.QUEUE,
                "hello-java-exchange",
                "hello.java",
                null);
        amqpAdmin.declareBinding(binding);
        log.info("[{}]绑定成功","hello.java");
    }

}

5.测试如何向mq中发消息

5.1 发送字符串
@Autowired
RabbitTemplate rabbitTemplate;

@Test
void sendMessageTest() {
   
    // 发消息给交换机
    String msg = "hello rabbitmq";
    rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",msg);
    log.info("消息发送完成{}",msg);
}

在这里插入图片描述

5.2 发送对象

1、发送消息,如果发送的消息是个对象,我们会使用序列化机制,将对象写出去。对象必须实现serializable

@Autowired
RabbitTemplate rabbitTemplate;

@Test
void sendMessageTest() {
   
    // 发消息给交换机
    OrderEntity orderEntity = new OrderEntity();
    orderEntity.setCommentTime(new Date());
    orderEntity.setDeliveryCompany("阿里巴巴");
    orderEntity.setId(1L);
    
    String msg = "hello rabbitmq";
    rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderEntity);
    log.info("消息发送完成{}",msg);
}

在这里插入图片描述

2、发送的对象消息,可以是json

  • 自定义消息转换策略
package com.atguigu.gulimall.order.config;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRabbitConfig {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值