場景是這樣的:需要利用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);
}
}
}
}