RabbitMQ的五种模式:

简单模式

生产者

public class Product {
    public static void main(String[] args) throws  Exception {
        //创建连接工厂--配置连接信息
        ConnectionFactory factory =new ConnectionFactory();
        factory.setHost("虚拟机id号");
        //创建链接对象
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        /**
         * String queue,       队列名
         * boolean durable,    持久化
         * boolean exclusive,  独占队列
         * boolean autoDelete  自动删除队列
         *  Map<String, Object> arguments   队列的其他属性
         */
        channel.queueDeclare("ljc",true,false,false,null);
        String msg="123331333";
        channel.basicPublish("","ljc",null,msg.getBytes());
    }
}

消费者

public class Consumer {
    public static void main(String[] args) throws  Exception {
        //创建连接工厂--配置连接信息
        ConnectionFactory factory =new ConnectionFactory();
        factory.setHost("虚拟机id");
        //创建链接对象
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        //接受消息
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消息内容"+new String(body));

            }
        };
        channel.basicConsume("ljc",true,defaultConsumer);
    }

}

工作者模式
在这里插入图片描述
特点:
1. 一个生产者
2. 由多个消费。
3. 统一个队列。
4. 这些消费者之间存在竞争关系。

用处:
比如批量处理上. rabbitMQ里面积压了大量的消息。

public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        channel.queueDeclare("ljc",true,false,false,null);
        for(int i=0;i<10;i++) {
            String msg = "第"+i"条";
            channel.basicPublish("", "li", null, msg.getBytes());
        }
        channel.close();
        connection.close();

    }
}

消费者 消费者代码基本一致

public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                //休眠1秒
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("ljc",true,callback);

    }
}

发布订阅模式

在这里插入图片描述

  • 特点:
  • 一个生产者
  • 多个消费者
  • 多个队列。
  • 交换机 转发消息。
    生产者
public class Product {
    public static void main(String[] args) throws  Exception {
        //创建连接工厂--配置连接信息
        ConnectionFactory factory =new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建链接对象
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        channel.queueDeclare("l1",true,false,false,null);
        channel.queueDeclare("l2",true,false,false,null);
        //创建交换机
        channel.exchangeDeclare("ljc1", BuiltinExchangeType.FANOUT,true);
        //交换机和信道绑定
        channel.queueBind("l1","ljc1","");
        channel.queueBind("l2","ljc1","");
        for (int i = 0; i <10 ; i++) {
          String msg = "第"+i"条";
            channel.basicPublish("ljc1","",null,msg.getBytes());
        }
        channel.close();
        connection.close();

    }
}

多个消费者

public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("ljc1",true,callback);

    }
}

路由模式

在这里插入图片描述

特点:
1.一个生产者
2.多个消费者
3.多个队列。
4.交换机 转发消息。
5.routekey:路由key 只要routekey匹配的消息可以到达对应队列。
生产者

public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.213.188");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        channel.queueDeclare("l1",true,false,false,null);
        channel.queueDeclare("l2",true,false,false,null);

        //创建交换机
        /**
         * String exchange,交换机的名称
         * BuiltinExchangeType type, 交换机的类型
         * boolean durable:是否持久化
         */
        channel.exchangeDeclare("ljc1", BuiltinExchangeType.DIRECT,true);
        /**
         * String queue,  队列名
         * String exchange, 交换机的名称
         * String routingKey:路由key 如果交换机为fanout模式则不需要路由key
         */
        channel.queueBind("l1","ljc1","error");


        channel.queueBind("12","ljc1","info");
        channel.queueBind("12","ljc1","error");
        channel.queueBind("12","ljc1","warning");
        //发生消息
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        for(int i=0;i<10;i++) {
            String msg = "第"+i"条";
            channel.basicPublish("ljc1", "error", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}

多个消费者

public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("ljc1",true,callback);

    }
}

topic主体模式

在这里插入图片描述

  1. 绑定按照通配符的模式。
    *: 统配一个单词。
    #: 统配n个单词
    hello.orange.rabbit
    lazy.orange
    生产者
public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.213.188");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        channel.queueDeclare("l1",true,false,false,null);
        channel.queueDeclare("l2",true,false,false,null);
        channel.exchangeDeclare("ljc1", BuiltinExchangeType.DIRECT,true);
    channel.queueBind("l1","ljc1","*.orange.*");
        channel.queueBind("l2","ljc1","*.*.rabbit");
        channel.queueBind("l3","ljc1","lazy.#");
        for(int i=0;i<10;i++) {
          String msg = "第"+i"条";
            channel.basicPublish("ljc1", "lazy.orange.rabbit", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}

多个消费者

public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.31.230");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("ljc1",true,callback);

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值