Java框架之springboot整合rabbitmq(exchanges采用fanout模式)

1、启动rabbitmq服务

service rabbitmq-server start    #启动服务
service rabbitmq-server status   #查看服务状态

          

2、在idea中构建基于maven的spring boot聚合工程

        2.1  启动idea,选择New Project

              

        2.2  选择maven,然后选择maven-archetype-site-simple,之后点击next,再点击finish

              

             

      2.3  修改父工程pom.xml文件,使聚合工程的父工程继承spring-boot-starter-parent,同时引入rabbitmq的相关依赖,这样可以不用在子工程中的每一个pol.xml中都添加该依赖。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>springboot-order-rabbitmq-producter</module>
        <module>springboot-order-rabbitmq-consumber</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>11</java.version>
    </properties>
    <groupId>org.example</groupId>
    <artifactId>rabbitmqParent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>


</project>

3、创建子工程生产者

        

选择maven-archetype-portlet,然后next->finish

       

创建完毕需要自己建项目结构目录

生产者创建交换机和绑定队列代码

package com.example.springbootorderrabbitmqproduct.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfiguration {

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout_exchange_order", true, false);
    }

    @Bean
    public Queue emailQueue() {
        return new Queue("email.fanout.queue", true);
    }

    @Bean
    public Queue weChatQueue() {
        return new Queue("wechat.fanout.queue", true);
    }

    @Bean
    public Binding emailBinding() {
        return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding weChatBinding() {
        return BindingBuilder.bind(weChatQueue()).to(fanoutExchange());
    }
}

生产者往队列里面发送消息

package com.example.springbootorderrabbitmqproduct.service;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * @param userID
     * @param productID
     * @param count
     * @Auther
     * @Description 模拟用户下单
     */
    public void makeDrder(String userID, String productID, int count) {
        String orderID = UUID.randomUUID().toString();
        String exchange = "fanout_exchange_order";
        String routingKey = "";
        String content = orderID;
        rabbitTemplate.convertAndSend(exchange, routingKey, content);
    }
}

springboot启动类,测试时可以通过测试类启动测试,可以不从该类启动

package com.example.springbootorderrabbitmqproduct;

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

@SpringBootApplication
public class SpringbootOrderRabbitmqProductApplication {

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

}

生产者测试类,往队列发送消息

package com.example.springbootorderrabbitmqproduct;

import com.example.springbootorderrabbitmqproduct.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootOrderRabbitmqProductApplicationTests {

    @Autowired
    private OrderService orderService;

    @Test
    void contextLoads() {
        orderService.makeDrder("01", "1", 16);
    }

}
server:
  port: 8081
spring:
  rabbitmq:
    host: 192.168.21.129
    port: 5672
    virtual-host: /
    username: test
    password: test

4、创建子工程消费者(同创建子工程生产者)

消费者目录结构

消费邮件队列代码

package som.example.springbootorderrabbitmqconsumber.service;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@RabbitListener(queues = {"email.fanout.queue"})
@Service
public class FanoutEmailConsumber {

    @RabbitHandler
    public void receiveMessage(String message) {
        System.out.println("邮件订单信息====" + message);
    }
}

消费微信队列代码

package som.example.springbootorderrabbitmqconsumber.service;


import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@RabbitListener(queues = {"wechat.fanout.queue"})
@Service
public class FanoutWechatConsumber {

    @RabbitHandler
    public void receiveMessage(String message) {
        System.out.println("微信订单信息====" + message);
    }
}

启动类

package som.example.springbootorderrabbitmqconsumber;


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

@SpringBootApplication
public class SpringbootOrderRabbitmqConsumberApplication {

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

环境配置

server:
  port: 8081
spring:
  rabbitmq:
    host: 192.168.21.129
    port: 5672
    virtual-host: /
    username: test
    password: test

生产者测试

通过管控平台查看队列消息http://192.168.21.129:15672/#/queues(IP根据个人机器填写)

消费者测试

通过管控平台查看队列消息http://192.168.21.129:15672/#/queues(IP根据个人机器填写)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值