SpringBoot整合RabbitMQ消息中间件笔记

0.docker 安装RabbitMQ命令:

从docker仓库拉取镜像:docker pull rabbitmq:3.8-management
运行镜像文件:docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq:3.8-management
查看容器日志:docker logs 容器id

1.pom文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.SpringBoot启动类

/**
 * 自动配置
 *  1、RabbitAutoConfiguration
 *  2、有自动配置了连接工厂ConnectionFactory;
 *  3、RabbitProperties 封装了 RabbitMQ的配置
 *  4、 RabbitTemplate :给RabbitMQ发送和接受消息;
 *  5、 AmqpAdmin : RabbitMQ系统管理功能组件;
 *      AmqpAdmin:创建和删除 Queue,Exchange,Binding
 *  6、@EnableRabbit +  @RabbitListener 监听消息队列的内容
 */
@EnableRabbit
@SpringBootApplication
public class SpringbootAmqpApplication {

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

3.配置文件

spring
  rabbitmq:
    host: 129.211.77.216
    username: guest
    password: guest
    port: 5672
    virtual-host: /

4.RabbitMQ装换器类

@Configuration
public class MyAMQPConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}

5.测试练习

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootAmqpApplicationTests {
    
    @Autowired
    RabbitTemplate rabbitTemplate;
@Autowired AmqpAdmin amqpAdmin;
@Test
public void testAmqpAdmin(){ //创建交换器 // Exchange exchange = new DirectExchange("amqpadmin.exchange"); // amqpAdmin.declareExchange(exchange); //创建队列 //amqpAdmin.declareQueue(new Queue("amqpadmin.equeue")); amqpAdmin.declareBinding(new Binding("amqpadmin.equeue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null)); } /** * 单播(点对点) */ @Test void contextLoads() { //Message需要自己构造一个;定义消息体内容和消息头 //rabbitTemplate.send(exchage,routeKey,message); //object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitmq; //rabbitTemplate.convertAndSend(exchage,routeKey,object); HashMap<String, Object> map = new HashMap<>(); map.put("msg","这是第一个消息"); map.put("data", Arrays.asList("helloworld",123,true)); //对象被默认序列化以后发送出去 //rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map); rabbitTemplate.convertAndSend("exchange.direct","atguigu.emps",map); } //接受数据,如何将数据自动的转为json发送出去 @Test public void receive(){ Object o = rabbitTemplate.receiveAndConvert("atguigu.news"); System.out.println(o.getClass()); System.out.println(o); } /** * 广播 */ @Test public void sendMsg(){ rabbitTemplate.convertAndSend("exchange.fanout","",new Book("红楼梦","曹雪芹")); } }

6.监听接收消息

@Service
public class BookService {

    @RabbitListener(queues = "atguigu.emps")
    public void receive(Book book){
        System.out.println("收到消息:"+book);
    }

    @RabbitListener(queues = "atguigu")
    public void receive2(Message message){
        System.out.println("收到消息体:"+message.getBody().getClass());
        System.out.println("收到消息头:"+message.getMessageProperties());
    }
}

7.用到的尸体类

public class Book {
    private String bookName;
    private String author;

    public Book() {
    }

    public Book(String bookName, String author) {
        this.bookName = bookName;
        this.author = author;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

猜你喜欢

转载自www.cnblogs.com/amaocc/p/12348030.html