SpringBoot 整合RabbitMq

2019-09-29  本文已影响0人  拉提娜的爸爸

一、准备环境

1、SpringBoot 导入RabbitMq依赖

        <!--rabbitmq依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

这里要导入web依赖,因为如果没有web依赖,会出现NoClassDefFoundError错误

2、配置RabbitMq连接

# rabbitmq配置
# 连接地址
spring.rabbitmq.host=127.0.0.1
# 端口号 (默认是5672,默认与实际相同,可以不用配置)
spring.rabbitmq.port=5672
# 用户名
spring.rabbitmq.username=guest
# 密码
spring.rabbitmq.password=guest
# 虚拟主机(默认是 / )
spring.rabbitmq.virtual-host=/

二、开始使用RabbitMq

1、生产者消息推送

SpringBoot整合RabbitMq主要用RabbitTemplate来实现对队列的操作

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendDirect() {
        Map<String,Object> map = new HashMap<>();
        map.put("imei","第一个mq消息");
        map.put("cmd","0D00");
        // convertAndSend(交换器,路由键,消息体)
        rabbitTemplate.convertAndSend("my_exchanges","news",map);
       // rabbitTemplate.convertAndSend("my_exchanges","news",new DeviceCmd("12345678","0D00"));
    }
默认以java序列化方式存储
@Configuration
public class MyAMQPConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

然后运行生产者推送消息的方法 结果:


以json格式存储
    // 测试广播模式推送消息
    @Test
    public void sendFanout(){
        // 广播模式推送消息,可以不用写 路由键,会默认发送到交换器绑定的所有队列里
        rabbitTemplate.convertAndSend("my_exchange_fanout","",new DeviceCmd("54321","0D01"));
    }

2、消费者接收消息

    @Test
    public void receive(){
        // 接收并转换消息
        // receiveAndConvert("queueName"); 从指定队列中取出消息
        Object o = rabbitTemplate.receiveAndConvert("my_queue_old");
        System.out.println(o);
//        DeviceCmd deviceCmd = (DeviceCmd) rabbitTemplate.receiveAndConvert("my_queue_old");
//        System.out.println(deviceCmd.getClass());
//        System.out.println(deviceCmd.getCmd() + " -- " + deviceCmd.getImei());
    }
@EnableRabbit
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2)在项目中添加@RabbitListener注解,来实现队列的监听
注意:监听方法所在类必须在Spring容器中

@Component
public class DeviceCmdService {

    @RabbitListener(queues = {"test.queue"})
    public void receive(byte[] body){
        String s = new String(body);
        System.out.println(s);
    }
}

监听队列也可以直接将消息转换为对象或自己想要的类型,如果消息无法转换,会出现消息转换异常

    @RabbitListener(queues = {"test.queue"})
    public void receive(DeviceCmd deviceCmd){
        System.out.println(deviceCmd);
    }

3、队列管理AmqpAdmin

SpringBoot整合RabbitMq根据AmqpAdmin类来实现对交换器,队列进行创建、删除和绑定操作

    @Autowired
    private AmqpAdmin amqpAdmin;
    @Test
    public void createExchange(){
        // 创建交换机 new FanoutExchange()、new TopicExchange() new CustomExchange()
        amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
        // 创建队列  new Queue("队列名称")
        amqpAdmin.declareQueue(new Queue("amqpAdmin.queue"));

        // 绑定队列与交换机  new Binding("目标名称",目标类行,"交换机名称","路由键",)
        amqpAdmin.declareBinding(
                new Binding("amqpAdmin.queue",Binding.DestinationType.QUEUE,"amqpAdmin.exchange","amqpAdmin.key",null));

    }
上一篇 下一篇

猜你喜欢

热点阅读