|
|
package com.sunyo.wlpt.message.bus.service.rabbit.utils;
|
|
|
|
|
|
import com.rabbitmq.http.client.Client;
|
|
|
import com.rabbitmq.http.client.domain.ExchangeInfo;
|
|
|
import com.rabbitmq.http.client.domain.ExchangeMessageStats;
|
|
|
import com.rabbitmq.http.client.domain.QueueInfo;
|
|
|
import com.rabbitmq.http.client.domain.UserPermissions;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.BusServer;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.view.TempExchangeInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.view.ViewExchangeInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.view.ViewQueueInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.utils.AESUtils;
|
|
|
import org.apache.ibatis.reflection.MetaObject;
|
|
|
import org.apache.ibatis.reflection.SystemMetaObject;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author 子诚
|
|
|
* Description:com.rabbitmq.http.client的封装类
|
|
|
* 时间:2020/8/13 17:57
|
|
|
*/
|
|
|
public class ClientUtils {
|
|
|
|
|
|
public static Client connectClient(BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
// 服务器的IP地址
|
|
|
String host = busServer.getServerIp();
|
|
|
// 该服务器超级用户的用户名称
|
|
|
String superUsername = busServer.getSuperUsername();
|
|
|
// 该服务器超级用户的用户密码
|
|
|
String superPassword = AESUtils.decrypt(busServer.getSuperPassword());
|
|
|
|
|
|
// 服务器的客户端端口号
|
|
|
String clientPort = busServer.getClientPort().toString();
|
|
|
// 访问客户端的url
|
|
|
String url = "http://" + host + ":" + clientPort + "/api";
|
|
|
Client client = new Client(url, superUsername, superPassword);
|
|
|
return client;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建MQ用户
|
|
|
*
|
|
|
* @param userInfo 用户信息
|
|
|
* @param busServer {@link BusServer}
|
|
|
* @param password 新增用户的密码
|
|
|
*/
|
|
|
public static void addRabbitUser(UserInfo userInfo, BusServer busServer, String password) throws IOException, URISyntaxException
|
|
|
{
|
|
|
// 新增用户的用户名称
|
|
|
String username = userInfo.getUsername();
|
|
|
|
|
|
// 与客户端建立连接
|
|
|
Client client = connectClient(busServer);
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
// 创建用户,权限为none
|
|
|
client.createUser(username, password.toCharArray(), list);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户与虚拟主机建立联系
|
|
|
*
|
|
|
* @param userInfo 用户信息
|
|
|
* @param busServer 服务器信息
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static void userRelation(UserInfo userInfo, BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
String username = userInfo.getUsername();
|
|
|
String virtualHostName = userInfo.getVirtualHostName();
|
|
|
Client client = connectClient(busServer);
|
|
|
|
|
|
UserPermissions p = new UserPermissions();
|
|
|
p.setConfigure(username + ".*");
|
|
|
p.setRead(username + ".*");
|
|
|
p.setWrite(username + ".*");
|
|
|
client.updatePermissions(virtualHostName, username, p);
|
|
|
|
|
|
// TopicPermissions topicPermissions = new TopicPermissions();
|
|
|
// topicPermissions.setVhost(virtualHostName);
|
|
|
// topicPermissions.setExchange("");
|
|
|
// topicPermissions.setRead(".*");
|
|
|
// topicPermissions.setWrite(".*");
|
|
|
// client.updateTopicPermissions(virtualHostName, username, topicPermissions);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除用户
|
|
|
*/
|
|
|
public static void deleteMQUser(UserInfo userInfo, BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
String username = userInfo.getUsername();
|
|
|
Client client = connectClient(busServer);
|
|
|
client.deleteUser(username);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除用户
|
|
|
*/
|
|
|
public static void deleteMQUser(String username, BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
client.deleteUser(username);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改MQ的用户密码
|
|
|
*/
|
|
|
public static void updatePassword(BusServer busServer, String username, String password) throws IOException, URISyntaxException
|
|
|
{
|
|
|
char[] newPassword = password.toCharArray();
|
|
|
Client client = connectClient(busServer);
|
|
|
ArrayList<String> tags = new ArrayList<>();
|
|
|
client.updateUser(username, newPassword, tags);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建虚拟主机
|
|
|
*/
|
|
|
public static void createVirtualHost(BusServer busServer, String vHost) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
client.createVhost(vHost);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建虚拟主机
|
|
|
*/
|
|
|
public static void createVirtualHost(BusServer busServer, VirtualHost vHost) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
client.createVhost(vHost.getVirtualHostName(), vHost.getDescription());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除虚拟主机
|
|
|
*/
|
|
|
public static void deleteVirtualHost(BusServer busServer, String vHost) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
client.deleteVhost(vHost);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清楚用户与虚拟主机之间的关系
|
|
|
*/
|
|
|
public static void clearPermissions(BusServer busServer, String vHost, String username) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
client.clearPermissions(vHost, username);
|
|
|
client.clearTopicPermissions(vHost, username);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的队列信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static List<ViewQueueInfo> getViewQueues(BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
List<QueueInfo> queues = client.getQueues();
|
|
|
return reformatQueueInfo(busServer.getServerName(), queues);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 队列信息,重新格式化
|
|
|
*
|
|
|
* @param serverName MQ服务器名称
|
|
|
* @param queues 队列信息 {@link QueueInfo}
|
|
|
* @return
|
|
|
*/
|
|
|
public static List<ViewQueueInfo> reformatQueueInfo(String serverName, List<QueueInfo> queues)
|
|
|
{
|
|
|
List<ViewQueueInfo> list = new ArrayList<>();
|
|
|
// 将获取到的队列信息,拼接一个属性,服务器名称
|
|
|
for (QueueInfo queueInfo : queues) {
|
|
|
ViewQueueInfo viewQueueInfo = ViewQueueInfo.builder().queueInfo(queueInfo).serverName(serverName).build();
|
|
|
list.add(viewQueueInfo);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的队列信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器
|
|
|
* @param vHostName 虚拟主机名称
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static List<ViewQueueInfo> getViewQueues(BusServer busServer, String vHostName) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
List<QueueInfo> queues = client.getQueues(vHostName);
|
|
|
return reformatQueueInfo(busServer.getServerName(), queues);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的队列信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器信息
|
|
|
* @param vHostName 虚拟主机
|
|
|
* @param queueName 队列名称
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public ViewQueueInfo getViewQueues(BusServer busServer, String vHostName, String queueName) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
QueueInfo queue = client.getQueue(vHostName, queueName);
|
|
|
ViewQueueInfo viewQueueInfo = ViewQueueInfo.builder().serverName(busServer.getServerName()).queueInfo(queue).build();
|
|
|
return viewQueueInfo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的交换机信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器信息
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static List<ViewExchangeInfo> getViewExchanges(BusServer busServer) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
List<ExchangeInfo> exchanges = client.getExchanges();
|
|
|
List<ViewExchangeInfo> viewExchangeInfos = reformatExchangeInfo(busServer.getServerName(), exchanges);
|
|
|
return viewExchangeInfos;
|
|
|
}
|
|
|
|
|
|
public static List<ViewExchangeInfo> reformatExchangeInfo(String serverName, List<ExchangeInfo> exchanges)
|
|
|
{
|
|
|
List<ViewExchangeInfo> list = new ArrayList<>();
|
|
|
// 将获取到的队列信息,拼接一个属性,服务器名称
|
|
|
for (ExchangeInfo exchangeInfo : exchanges) {
|
|
|
TempExchangeInfo tempExchangeInfo = exchangeInfoToTemp(exchangeInfo);
|
|
|
ViewExchangeInfo viewExchangeInfo = ViewExchangeInfo.builder().serverName(serverName).tempExchangeInfo(tempExchangeInfo).build();
|
|
|
list.add(viewExchangeInfo);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* mybatis封装的反射(根据属性名和对象,获取属性的值)
|
|
|
*
|
|
|
* @param fieldName 属性名
|
|
|
* @param object 对象
|
|
|
* @return
|
|
|
*/
|
|
|
private static Object getFieldValueByFieldName(String fieldName, Object object)
|
|
|
{
|
|
|
MetaObject metaObject = SystemMetaObject.forObject(object);
|
|
|
Object value = metaObject.getValue(fieldName);
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将ExchangeInfo转换成TempExchangeInfo
|
|
|
*
|
|
|
* @param exchangeInfo ExchangeInfo
|
|
|
* @return
|
|
|
*/
|
|
|
private static TempExchangeInfo exchangeInfoToTemp(ExchangeInfo exchangeInfo)
|
|
|
{
|
|
|
ExchangeMessageStats messageStats = (ExchangeMessageStats) getFieldValueByFieldName("messageStats", exchangeInfo);
|
|
|
TempExchangeInfo temp = new TempExchangeInfo();
|
|
|
temp.setVhost(exchangeInfo.getVhost());
|
|
|
temp.setName(exchangeInfo.getName());
|
|
|
temp.setType(exchangeInfo.getType());
|
|
|
temp.setDurable(exchangeInfo.isDurable());
|
|
|
temp.setAutoDelete(exchangeInfo.isAutoDelete());
|
|
|
temp.setInternal(exchangeInfo.isInternal());
|
|
|
temp.setArguments(exchangeInfo.getArguments());
|
|
|
temp.setMessageStats(messageStats);
|
|
|
return temp;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的交换机信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器信息
|
|
|
* @param vHostName 虚拟主机
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static List<ViewExchangeInfo> getViewExchanges(BusServer busServer, String vHostName) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
List<ExchangeInfo> exchanges = client.getExchanges(vHostName);
|
|
|
List<ViewExchangeInfo> viewExchangeInfos = reformatExchangeInfo(busServer.getServerName(), exchanges);
|
|
|
return viewExchangeInfos;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取MQ界面的交换机信息,重载
|
|
|
*
|
|
|
* @param busServer MQ服务器信息
|
|
|
* @param vHostName 虚拟主机
|
|
|
* @param exchangeName 交换机名称
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws URISyntaxException
|
|
|
*/
|
|
|
public static ViewExchangeInfo getViewExchanges(BusServer busServer, String vHostName, String exchangeName) throws IOException, URISyntaxException
|
|
|
{
|
|
|
Client client = connectClient(busServer);
|
|
|
ExchangeInfo exchangeInfo = client.getExchange(vHostName, exchangeName);
|
|
|
TempExchangeInfo tempExchangeInfo = exchangeInfoToTemp(exchangeInfo);
|
|
|
ViewExchangeInfo viewExchangeInfo = ViewExchangeInfo.builder().serverName(busServer.getServerName()).tempExchangeInfo(tempExchangeInfo).build();
|
|
|
return viewExchangeInfo;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|