场景是这样的:需要利用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);
}
}
}
}
网友评论