[原创]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);
	        }
		}
	}
}

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java技术干货

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值