RabbitMQ

RabbitMQ

1. 下載和安裝

  1. 去RabbitMQ官網下載對應的安裝包
    1. RabbitMQ,下載地址http://www.rabbitmq.com/install-windows.html
    2. 對應版本(必須是與mq版本適應)的Erlang,下載地址http://www.erlang.org/downloads/20.2
    3. 傻瓜式安裝直接下一步就好(前提是提前安裝Erlang,在安裝RabbitMQ)
    4. 安裝完成打開安裝目錄輸入rabbitmqctl status,查看是否安裝成功
    5. 如果安裝成功,重新開CMD輸入命令rabbitmq-plugins enable rabbitmq_management,這樣就可以添加可視化插件了。
    6. 在web瀏覽器中輸入地址:http://127.0.0.1:15672/;輸入默認賬號: guest 密碼: guest

2. 基礎使用

  1. 連接工具類
    public class ConnectionUtil {
    
        public static Connection getConnection() throws IOException, TimeoutException {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setPort(5672);
            connectionFactory.setHost("127.0.0.1");
            connectionFactory.setUsername("guest");
            connectionFactory.setPassword("guest");
            return connectionFactory.newConnection();
        }
    }
    
    1. simple模式:最簡單模式
      1. img
      2. 消息生產者
       public class Producter {
           //定義隊列名稱
           private final static String queue_name="simple";
           public static void main(String[] args)throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               /**
               	參數一:隊列名稱
               	參數二:是夠持久化
               	參數三:是否排他
               	參數四:是否自動刪除
               	參數五:其他參數
               */
               channel.queueDeclare(queue_name,false,false,false,null);
               //發送消息
               String msg = "Hello RabbitMQ";
               /**
               	參數一:交換機
               	參數二:隊列名稱
               	參數三:其他配置
               	參數四:發送內容
               */
               channel.basicPublish("",queue_name,null,msg.getBytes());
               //關閉信道
               channel.close();
               //關閉連接
               connection.close();
           }
       }
      
      1. 消息接收者
       public class Consumers {
           //定義隊列名稱
           private final static String queue_name = "simple";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
                 /**
               	參數一:隊其他參數列名稱
               	參數二:是否持久化
               	參數三:是否排他
               	參數四:是否自動刪除
               	參數五:其他參數
               */
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明消費者
              Consumer consumer  = 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(queue_name, true,consumer);
           }
       }
      
    2. fanout模式:生產者發送給交換機,交換機發送給所有的隊列,在發送給相應的消費者
      1. img
      2. 生產者
       public class Producter {
           //定義交換機名稱
           private final static String exchange_name="fanout_exchange";
           public static void main(String[] args)throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"fanout");
               //發送消息
               String msg = "Hello RabbitMQ";
               channel.basicPublish(exchange_name,"",null,msg.getBytes());
               //關閉信道
               channel.close();
               //關閉連接
               connection.close();
           }
       }
      
      1. 消費者1
       public class Consumers1 {
           //定義隊列名稱
           private final static String queue_name = "fanout1";
           //定義交換機名稱
           private final static String exchange_name="fanout_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"fanout");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
      
      1. 消費者2
       public class Consumers2 {
           //定義隊列名稱
           private final static String queue_name = "fanout2";
           //定義交換機名稱
           private final static String exchange_name="fanout_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"fanout");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
       
      
    3. direct模式:只有路由鍵完全匹配纔會被路由到相應的隊列
      1. img
      2. 生產者
       public class Producter {
           //定義交換機名稱
           private final static String exchange_name="direct_exchange";
           public static void main(String[] args)throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"direct");
               //發送消息
               String msg = "Hello RabbitMQ";
               channel.basicPublish(exchange_name,"info",null,msg.getBytes());
               channel.basicPublish(exchange_name,"warning",null,msg.getBytes());
               channel.basicPublish(exchange_name,"error",null,msg.getBytes());
               //關閉信道
               channel.close();
               //關閉連接
               connection.close();
           }
       }
      
      1. 消費者1
       public class Consumers1 {
           //定義隊列名稱
           private final static String queue_name = "direct1";
           //定義交換機名稱
           private final static String exchange_name="direct_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"direct");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"error");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
       
      
      1. 消費者2
       public class Consumers2 {
           //定義隊列名稱
           private final static String queue_name = "direct2";
           //定義交換機名稱
           private final static String exchange_name="direct_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"direct");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"info");
               channel.queueBind(queue_name,exchange_name,"warning");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
       
      
      
    4. topic模式:路由鍵相應匹配纔會被路由到相應的隊列
      1. img
      2. 生產者

       public class Producter {
           //定義交換機名稱
           private final static String exchange_name="topic_exchange";
           public static void main(String[] args)throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"topic");
               //發送消息
               String msg = "Hello RabbitMQ";
               channel.basicPublish(exchange_name,"info.info",null,msg.getBytes());
               channel.basicPublish(exchange_name,"warning.info",null,msg.getBytes());
               channel.basicPublish(exchange_name,"info.error",null,msg.getBytes());
               //關閉信道
               channel.close();
               //關閉連接
               connection.close();
           }
       }
      
      
      1. 消費者1
       public class Consumers1 {
           //定義隊列名稱
           private final static String queue_name = "topic1";
           //定義交換機名稱
           private final static String exchange_name="topic_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"topic");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"info.#");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
      
      
      1. 消費者2
       public class Consumers2 {
           //定義隊列名稱
           private final static String queue_name = "topic2";
           //定義交換機名稱
           private final static String exchange_name="topic_exchange";
       
           public static void main(String[] args) throws IOException, TimeoutException {
               //獲取連接
               Connection connection = ConnectionUtil.getConnection();
               //聲明信道
               Channel channel = connection.createChannel();
               //聲明隊列
               channel.queueDeclare(queue_name, false, false, false, null);
               //聲明交換機
               channel.exchangeDeclare(exchange_name,"topic");
               //交換機與隊列綁定
               channel.queueBind(queue_name,exchange_name,"#.info");
               //聲明消費者
              Consumer consumer  = 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.basicAck(envelope.getDeliveryTag(), false);
                   }
               };
               channel.basicConsume(queue_name, false,consumer);
           }
       }
       
      
      

3.高級進階

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章