springboot整合rabbitmq

1.先引入Rabbitmq的starter

2.在Application.properties文件中配置rabbitmq

#rabbitmq主机地址
spring.rabbitmq.host=192.168.144.143
#rabbitmq端口号 spring.rabbitmq.port=5672
#rabbitmq用户名 spring.rabbitmq.username=guest
#rabbitmq密码 spring.rabbitmq.password=guest
#rabbitmq虚拟主机 spring.rabbitmq.virtual-host=/

3.因为springboot直接引入rabbitmq默认的序列化方式是jdk的即存入rabbitmq队列中的数据是字节数据。通常我们为了开发方便需要将其转换成json格式的存入rabbitmq服务器,所以我们需要编写一个配置类将其转换成json

package com.atguigu.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 MyAMQPConfig {
    /**
     * 将存入rabbitmq队列中的序列化方式换成json序列化方式
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

4.RabbitMQ常用方法

package com.atguigu;

import com.atguigu.vo.Book;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringbootRabbitmqApplicationTests {
    @Autowired
    RabbitTemplate rabbitTemplate;//用来发送和接收数据
    @Autowired
    AmqpAdmin amqpAdmin;//用来自动创建或删除 交换器,队列,绑定

    @Test
    void send() {
        Map<String,Object> map=new HashMap<>();
        map.put("msg","这是第一个消息");
        map.put("data", Arrays.asList("hello world",123,true));
        rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);//发送数据
    }
    @Test
    public void receive(){
        Object o = rabbitTemplate.receiveAndConvert("atguigu.news");//接收数据
        System.err.println(o);
    }
    @Test
    public void sendBook(){
        Book book=new Book("三国演义","罗贯中");
        rabbitTemplate.convertAndSend("exchange.fanout","hello",book);
    }
    @Test
    public void receiveBook(){
        Book book = (Book) rabbitTemplate.receiveAndConvert("atguigu.news");
        System.err.println(book);
        System.err.println(book.getBookName());
        System.err.println(book.getAuthor());
    }
    @Test
    public void createExchange(){
        Exchange exchange=new FanoutExchange("java-exchange");
        amqpAdmin.declareExchange(exchange);//以declare开头的方法都是创建,以delete或remove开头的方法都是删除
    }

}

5.开启RabbitMQ注解模式

@EnableRabbit//开启rabbitmq注解模式
@SpringBootApplication
public class SpringbootRabbitmqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
}

6.在要监听的方法上使用@RabbitListener注解,监听的方法是写在业务层的

package com.atguigu;

import com.atguigu.vo.Book;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Service;

@Service//监听的方法是写在业务层的
public class BookService {
    @RabbitListener(bindings = @QueueBinding(value = @Queue("atguigu.hello"),exchange = @Exchange("randout-exchange"),key = "atguigu"))//用来自动创建并监听RabbitMQ,value表示队列名称,exchange表示交换器,key表示路由键
    public void receive01(Book book){//这里的book就是一个实体类,表示只获取数据
        System.err.println("==================01===================");
        System.err.println(book);
    }
    @RabbitListener(queuesToDeclare = @Queue("atguigu.java"))//用来监听RabbitMQ
    public void receive02(Message message){//这里的Message是amqp包的,里面包括消息属性
        System.err.println("==================02===================");
        System.err.println(message.getBody().toString());
        //System.err.println(message.getMessageProperties());
    }
}

7.更多rabbitmq注解再百度

posted @ 2020-03-24 09:34  王兴龙123  阅读(187)  评论(0编辑  收藏  举报