[原創]RabbitMQ之業務場景(五):采用httpApi動態操作RabbitMQ(JAVA版)

場景是這樣的:需要利用RabbitMQ提供的http api,采用java發送http請求來獲取到queues相關信息, 并動態刪除某些隊列。
首先, 我們來看一下RabbitMQ提供http api是什么樣子, 大概有哪些api可以使用, 或者符合我們的場景需求?

在瀏覽器上打開并登陸RabbitMQ后,在頁面的最下方我們就可以看到介紹“HTTP API”的鏈接入口,


image.png

image.png

下面是api列表, 都有相關介紹說明,其中可以找到我們需要獲取所有的queues列表api接口"/api/queues"


部分api.png

然后我們發現curl又是什么鬼, 然后又開始百度了一通, 發現在windows環境下是沒有curl 這個工具, 結果又得安裝, 如果項目部署上線, 還得依賴于它, 顯然是不現實的。所以, 就采用java借助于httpClient工具類來發送請求。


標紅需要RabbitMQ的賬號和密碼.png

但是,在實踐期間還是出現了問題,發現curl 在發送請求的時候 需要用戶名和密碼進行權限校驗,這是我第一次遇到, 在發送http請求的時候, 也沒有這么干過, 于是又開始了百度搜索之路。

現總結并分享如下:
首先需要httpClient工具包, 所以在pom.xml引入一下相關jar包

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>

封裝http請求工具包:HttpKit.java

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * http請求工具包
 * @author rayson517
 *
 */
public class HttpKit {
    
    public static String Get(String url, String username, String password) throws IOException{
        // 發送http請求數據
        // 創建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 設置BasicAuth
        CredentialsProvider provider = new BasicCredentialsProvider();
        // Create the authentication scope
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        // Create credential pair,在此處填寫用戶名和密碼
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        // Inject the credentials
        provider.setCredentials(scope, credentials);
        // Set the default credentials provider
        httpClientBuilder.setDefaultCredentialsProvider(provider);
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        
        String result = "";
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        HttpEntity entity = null;
        httpGet = new HttpGet(url);
        try {
            httpResponse = closeableHttpClient.execute(httpGet);
            entity = httpResponse.getEntity();
            if( entity != null ){
                result = EntityUtils.toString(entity);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // 關閉連接
        closeableHttpClient.close();
        
        return result;
    }
    
}

調用RabbitMQ http api接口示例

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 調用rabbitMQ http api demo示例
 * @author rayson517
 *
 */
@Component
public class RabbitMQDemo {
    
    @Autowired
    RabbitProperties rabbitProperties;
    
    public void sendApi(){
        
        
        // 獲取隊列名稱
        String host = rabbitProperties.getHost();
        String username = rabbitProperties.getUsername();
        String password = rabbitProperties.getPassword();
        // 根據RabbitMQ提供的隊列信息, 每天定時刪除動態創建的隊列。
        String url = "http://"+host+":15672/api/queues";
        String result = null;
        try {
            result = HttpKit.Get(url, username, password);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(result)){
            JSONArray jsonArray = JSON.parseArray(result);
            for(int i = 0; i < jsonArray.size(); i++){
                JSONObject jsonObject = JSONObject.parseObject(jsonArray.getString(i));
                String name = jsonObject.getString("name");
                System.out.println("隊列名稱:"+name);
            }
        }
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,537評論 1 92
  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981閱讀 16,004評論 2 11
  • 今天休息,帶女兒出去遛了一圈回來,大概因為新鞋子的緣故,女兒說腳疼。一回到家,女兒就大呼小叫的喊我幫忙脫鞋子。 我...
    從容自由閱讀 457評論 2 5
  • 朋友圈的孝心太多, 媽媽不懂得網絡。
    百葉行閱讀 201評論 0 0