天天看点

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

RabbitMQ整合springboot(一个例子)

1. 创建一个spring boot项目

web想加就加,第二个必选

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)
RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

2. 创建以下的目录结构

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

3. 父工程引入相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <!--子模块-->
    <modules>
        <module>product</module>
        <module>consumer</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.hong</groupId>
    <artifactId>rabbitmq-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rabbitmq-springboot</name>
    <description>springboot整合rabbitmq</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)
			引入rabbitMQ的依赖:启动类加载,读取配置文件
            springboot自动装配原理:引用starter启动依赖时,
            将对应的自动装配类加载进去,该自动装配类可以读取application.yml文件中的内容
        -->
        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

4. 相应的生产者和消费者的配置

生产者:

# 端口号
server:
  port: 8081

# rabbitmq的配置
spring:
  rabbitmq:
    host: 192.168.31.129
           

消费者:

# 端口号
server:
  port: 8082
  
# rabbitmq配置
spring:
  rabbitmq:
    host: 192.168.31.129
           

5.生产者

package com.hong.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author hongCheng
 * @Date 2021/4/20 19:21
 * @Version 1.0
 */
@RestController
public class ProductController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("product")
    public String product(){//业务层
        System.out.println("下单成功");
        /**
         * String exchange, 交换机的名称
         * String routingKey, 路由密钥
         * Object message 信息
         */
        rabbitTemplate.convertAndSend("hong_direct","error","橙子真好吃");
        return "下单成功";
    }
}
           

写一个测试类,测试结果

package com.hong;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author hongCheng
 * @Date 2021/4/20 20:21
 * @Version 1.0
 */
@SpringBootApplication
public class SpringBootRabbitMQ {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootRabbitMQ.class,args);
    }
}
           

运行结果:

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)
RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

6.消费者

6.1 传入String

package com.hong.consumer.listener;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author hongCheng
 * @Date 2021/4/20 20:47
 * @Version 1.0
 */
@Component//作用就是相当于bean的注入
public class MyRabbitListener {
    //队列中存在消息则即使回调该方法
    @RabbitListener(queues = {"hong_direct01"})
    public void  listener(String msg){
        System.out.println(msg);
    }
}
           

测试类监听生产者:

package com.hong.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author hongCheng
 * @Date 2021/4/20 20:42
 * @Version 1.0
 */
@SpringBootApplication
public class ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class,args);
    }
}
           

测试结果:

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

6.2 传入map

生产者:

package com.hong.consumer;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author hongCheng
 * @Date 2021/4/20 19:21
 * @Version 1.0
 */
@RestController
public class ProductController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("product")
    public String product(){//业务层
        System.out.println("下单成功");
        Map<String, Object> map = new HashMap<>();
        map.put("productId",1);
        map.put("num",10);
        map.put("sum",240);
        /**
         * String exchange, 交换机的名称
         * String routingKey, 路由密钥
         * Object message 信息
         */
        rabbitTemplate.convertAndSend("hong_direct","error",map);
        return "下单成功";
    }
}
           

消费者:

@Component//作用就是相当于bean的注入
public class MyRabbitListener {
    //队列中存在消息则即使回调该方法
    @RabbitListener(queues = {"hong_direct01"})
    public void  listener(Message msg){
        System.out.println(msg);
    }
}
           

结果:

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)

6.3 使用fastjson

  1. 引入依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.72</version>
</dependency>
           
  1. 生产者
package com.hong.consumer;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author hongCheng
 * @Date 2021/4/20 19:21
 * @Version 1.0
 */
@RestController
public class ProductController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("product")
    public String product(){//业务层
        System.out.println("下单成功");
        Map<String, Object> map = new HashMap<>();
        map.put("productId",1);
        map.put("num",10);
        map.put("sum",240);
        /**
         * String exchange, 交换机的名称
         * String routingKey, 路由密钥
         * Object message 信息
         */
        rabbitTemplate.convertAndSend("hong_direct","error", JSON.toJSONString(map));
        return "下单成功";
    }
}
           

消费者:

package com.hong.consumer.listener;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author hongCheng
 * @Date 2021/4/20 20:47
 * @Version 1.0
 */
@Component//作用就是相当于bean的注入
public class MyRabbitListener {
    //队列中存在消息则即使回调该方法
    @RabbitListener(queues = {"hong_direct01"})
    public void  listener(String msg){
        Map map = JSON.parseObject(msg, Map.class);
        System.out.println(map);
    }
}
           

测试结果:

Map;

* @Author hongCheng
 * @Date 2021/4/20 20:47
 * @Version 1.0
 */
@Component//作用就是相当于bean的注入
public class MyRabbitListener {
    //队列中存在消息则即使回调该方法
    @RabbitListener(queues = {"hong_direct01"})
    public void  listener(String msg){
        Map map = JSON.parseObject(msg, Map.class);
        System.out.println(map);
    }
}
           

测试结果:

RabbitMQ整合springbootRabbitMQ整合springboot(一个例子)