RabbitMQ快速入門

RabbitMQ是一種消息中間件,它接收和轉發消息,能夠理解爲郵局。只是RabbitMQ接收,處理,轉發的是二進制的數據,郵局處理的通常爲紙。html

基本概念

  • Producer(生產者): 發送消息的程序
  • Consumer(消費者):接收消息的程序
  • Queue(隊列):像郵局的信箱,在RabbitMQ內部,同一個消息流只能存在一個Queue中,隊列只受主機內存,磁盤的大小限制。 生產者像Queue中發送消息,消費者從Queue中取出消息

安裝

  1. Mac OS
brew install rabbitmq
複製代碼
  1. CentOS7
cat << EOF > /etc/yum.repos.d/rabbit.repo
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq/rpm/erlang/21/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
EOF
yum install rabbitmq-server
複製代碼

Hello World

  1. 啓動RabbitMQ
rabbitmq-server
複製代碼
  1. 添加Java依賴庫
<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
      </dependency>
      <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>4.2.0</version>
      </dependency>
複製代碼
  1. 編寫生產者
/** * @author aihe 2018/9/6 */
public class Producer {
    private final static String QUEUE_NAME = "hello1";

    public static void main(String[] args) throws IOException, TimeoutException {


        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        String message = "Hello World";

        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("發送消息:" + message);

        try {
            channel.close();
            connection.close();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}

複製代碼

channel.queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)java

  • queue 要聲明的隊列名稱
  • durable 是否長久保存,true的話,服務器重啓的時候,queue仍然存在
  • exclusive 是否聲明一個排他的隊列, 也就是隻有當前的channel才能監聽這個queue
  • autoDelete 當服務器不在使用這個queue的時候,自動刪除它
  • arguments 其它的屬性

生產和消費消息都是經過channel的。channel指定具體爲那個queuepython

  1. 編寫消費者
/** * @author aihe 2018/9/6 */
public class Consumer {
    private final static String QUEUE_NAME = "hello1";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] 正在等待消息. 退出按 CTRL+C");

        com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" 接收消息:'" + message + "'");
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);

    }
}
複製代碼
  1. 運行

生產者 bash

image.png

消費者 服務器

image.png

額外

  1. 咱們能夠安裝rabbitMQ的圖形管理界面
// 查看有哪些插件
rabbitmq-plugins list
// 啓用管理界面
rabbitmq-plugins enable rabbitmq_management

複製代碼
  1. 進入管理界面 進入地址:http://127.0.0.1:15672/ 帳號和密碼:guest guest
    image.png

image.png

這些參數大部分都是能夠從rabbitmqctl命令得到的。ide

最後

學習下消息中間件學習

參考

相關文章
相關標籤/搜索