1. 程式人生 > 實用技巧 >RabbitMQ (一) springboot+rabbitmq配置與簡單佇列

RabbitMQ (一) springboot+rabbitmq配置與簡單佇列

rabbitmq訊息中介軟體,按我的理解就是解決同時大量請求造成伺服器壓力大掛掉,可以作為中間商儲存請求,再慢慢讓伺服器返回。

首先是配置環境,預設已經安裝了本地rabbitmq,可以通過localhost:15672 進入管理頁面

pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

匯入依賴後,在RabbitMq Manager頁面按照下面步驟設定好登入使用者,host和許可權,

然後你就可以在ide中寫測試程式碼了。

首先寫個連線工具類:

package com.yaimer.d.rabbit;


import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.impl.AMQConnection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/** * @program: SpringSecurityTestProject * @description: * @author: yaimer * @create: 2020-09-07 23:56 **/ public class test { /** * 獲取MQ的連線 */ public static Connection getConnection() throws IOException, TimeoutException { //定義一個連線工廠 ConnectionFactory factory = new ConnectionFactory();
//設定服務地址 factory.setHost("127.0.0.1"); //設定埠 AMQ 5672 factory.setPort(5672); //vhost factory.setVirtualHost("/vhost"); //使用者名稱 factory.setUsername("zard"); //密碼 factory.setPassword("admin"); return factory.newConnection(); } }

簡單佇列就是一個生產者Producer和一個消費者Consumer

寫一個生產者:

package com.yaimer.d.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: SpringSecurityTestProject
 * @description:
 * @author: yaimer
 * @create: 2020-09-08 00:19
 **/

public class produce {
    private static final String QUEUE_NAME="testQueue";

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

        Connection connection = test.getConnection();

        //從連線中獲取一個通道
        Channel channel = connection.createChannel();

        //建立佇列宣告
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        String str ="hello_zard_test______";

        channel.basicPublish("",QUEUE_NAME,null,str.getBytes());

        System.out.println("她走過——————"+str);

        channel.close();

        connection.close();
    }
}

,接著一個消費者:

package com.yaimer.d.rabbit;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: SpringSecurityTestProject
 * @description:
 * @author: yaimer
 * @create: 2020-09-08 00:29
 **/

public class consumer {
    private static final String QUEUE_NAME="testQueue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = test.getConnection();

        Channel channel =connection.createChannel();

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

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String str = new String(body,"utf-8");

                System.out.println("consumer____"+str);
            }
        };

        channel.basicConsume(QUEUE_NAME,true,consumer);


    }
}

寫完就可以運行了,可以先執行消費者,再執行生產者,執行幾次,消費者接受幾次。

也可不執行消費者,執行幾次生產者,最後執行消費者,會一次性接收。

這是簡單佇列的實現。