• 07_整合spring boot


    整合spring boot-生产者-Fanout模式

    在这里插入图片描述

    1.新建springboot项目 选择 springweb和rabbitmq依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.springframework.amqpgroupId>
                <artifactId>spring-rabbit-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.1.0version>
                plugin>
            plugins>
        build>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2.service层

    package com.tian.demo.service;
    
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.UUID;
    
    @Service
    public class OrderService {
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        public void makeOrder(String userid,String productid,int num){
            //1.根据商品id查询库存是否充足
            //2.保存订单
            String orderId= UUID.randomUUID().toString();
            System.out.println("订单生产成功:"+orderId);
    
            //3.通过MQ来完成消息的分发
            //参数1:交换机 参数2:路由key/queue队列名称 参数3:消息内容
            String exchangeName="fanout_order_exchange";
            String routingKey="";
            rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3.config层

    package com.tian.demo.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 {
        //1.声明注册fanout模式的交换机
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("fanout_order_exchange",true,false);
        }
        //2.声明队列
        @Bean
        public Queue smsQueue(){
            return new Queue("sms.fanout.queue",true);
        }
        @Bean
        public Queue duanxinQueue(){
            return new Queue("duanxin.fanout.queue",true);
        }
        @Bean
        public Queue emailQueue(){
            return new Queue("email.fanout.queue",true);
        }
        //3.完成绑定关系 (队列和交换机完成绑定关系)
        @Bean
        public Binding smsBingding(){
            return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
        }
        @Bean
        public Binding emailBingding(){
            return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
        }
        @Bean
        public Binding duanxinBingding(){
            return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    4.application.yml

    server:
      port: 8080
    
    #配置rabbitmq
    spring:
      rabbitmq:
        username: admin
        password: admin
        virtual-host: /
        host: 192.168.1.150
        port: 5672
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.测试运行

    @SpringBootTest
    class DemoApplicationTests {
        @Autowired
        private OrderService orderService;
        @Test
        void contextLoads() {
            orderService.makeOrder("1","1",12);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6.查看网页

    整合spring boot-消费者-Fanout模式

    1.新建springboot 勾选上springweb和rabbitmq依赖 同上

    2.application.yml同上

    3.service层

    • FanoutDuanxinConsumer
    @Service
    @RabbitListener(queues = {"duanxin.fanout.queue"})
    public class FanoutDuanxinConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("duanxin fanout--->接收到了订单信息是->"+message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • FanoutEmailConsumer
    @Service
    @RabbitListener(queues = {"email.fanout.queue"})
    public class FanoutEmailConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("email fanout--->接收到了订单信息是->"+message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • FanoutSMSConsumer
    @Service
    @RabbitListener(queues = {"sms.fanout.queue"})
    public class FanoutSMSConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("sms fanout--->接收到了订单信息是->"+message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.主启动类运行 控制台收到消息

    sms fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
    email fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
    duanxin fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
    
    • 1
    • 2
    • 3

    整合spring boot-Direct模式

    • 配置文件写在消费端更好 启动不会报错

    consumer config层

    package com.tian.demo.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class DirectRabbitMqConfiguration {
        //1.声明注册fanout模式的交换机
        @Bean
        public DirectExchange directExchange(){
            return new DirectExchange("direct_order_exchange",true,false);
        }
        //2.声明队列
        @Bean
        public Queue directsmsQueue(){
            return new Queue("sms.direct.queue",true);
        }
        @Bean
        public Queue directduanxinQueue(){
            return new Queue("duanxin.direct.queue",true);
        }
        @Bean
        public Queue directemailQueue(){
            return new Queue("email.direct.queue",true);
        }
        //3.完成绑定关系 (队列和交换机完成绑定关系)
        @Bean
        public Binding directsmsBingding(){
            return BindingBuilder.bind(directsmsQueue()).to(directExchange()).with("sms");
        }
        @Bean
        public Binding directemailBingding(){
            return BindingBuilder.bind(directemailQueue()).to(directExchange()).with("email");
        }
        @Bean
        public Binding directduanxinBingding(){
            return BindingBuilder.bind(directduanxinQueue()).to(directExchange()).with("duanxin");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    service层

    @Service
    @RabbitListener(queues = {"duanxin.direct.queue"})
    public class DirectDuanxinConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("duanxin direct--->接收到了订单信息是->"+message);
        }
    }
    
    @Service
    @RabbitListener(queues = {"email.direct.queue"})
    public class DirectEmailConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("email direct--->接收到了订单信息是->"+message);
        }
    }
    
    
    @Service
    @RabbitListener(queues = {"sms.direct.queue"})
    public class DirectSMSConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("sms direct--->接收到了订单信息是->"+message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    生产者 service层

     public void makeOrderDirect(String userid,String productid,int num){
            //1.根据商品id查询库存是否充足
            //2.保存订单
            String orderId= UUID.randomUUID().toString();
            System.out.println("订单生产成功:"+orderId);
    
            //3.通过MQ来完成消息的分发
            //参数1:交换机 参数2:路由key/queue队列名称 参数3:消息内容
            String exchangeName="direct_order_exchange";
            rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
            rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试访问

    @Autowired
    private OrderService orderService;
    @Test
    void contextLoads1() {
        orderService.makeOrderDirect("1","1",12);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    只有 邮件和短信收到服务

    整合spring boot-Topic模式

    • 注解模式
    • consumer端service层
    @Service
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "duanxin.topic.queue",durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
            key = "#.duanxin.#"
    ))
    public class TopicDuanxinConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("duanxin topic--->接收到了订单信息是->"+message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @Service
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "email.topic.queue",durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
            key = "*.email.#"
    ))
    public class TopicEmailConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("email topic--->接收到了订单信息是->"+message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @Service
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "sms.topic.queue",durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
            key = "com.#"
    ))
    public class TopicSMSConsumer {
        @RabbitHandler
        public void receiveMessage(String message){
            System.out.println("sms topic--->接收到了订单信息是->"+message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    producer端 service层

       public void makeOrderTopic(String userid,String productid,int num){
            //1.根据商品id查询库存是否充足
            //2.保存订单
            String orderId= UUID.randomUUID().toString();
            System.out.println("订单生产成功:"+orderId);
    
            //3.通过MQ来完成消息的分发
            //参数1:交换机 参数2:路由key/queue队列名称 参数3:消息内容
            String exchangeName="topic_order_exchange";
            String routingKey="com.duanxin";
            // #.duanxin.#   duanxin
            // *.email.#   email
            // com.#     sms
            rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试

    @Autowired
    private OrderService orderService;
    @Test
    void contextLoads2() {
        orderService.makeOrderTopic("1","1",12);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    只有sms 和 短信接收到服务

  • 相关阅读:
    mac常见问题(五) Mac 无法开机
    linux CentOs 安装docker 推荐生产环境使用
    RabbitMQ实现延迟队列
    图像增强算法的安卓移植
    高等数学教程【单变量微积分】内容目录
    vue的双向绑定的原理,和angular的对比
    Java JVM——12. 垃圾回收理论概述
    minikube 快速使用入门 - pod - 外传
    oracle 同一张表同时insert多条数据 mysql 同一张表同时insert多条数据
    FPGA HLS stream与dataflow
  • 原文地址:https://blog.csdn.net/xixihaha_coder/article/details/126867904