作者 王勇

重新修改关于rabbitMQ的封装

  1 +package com.sunyo.wlpt.message.bus.service.rabbit.utils;
  2 +
  3 +import com.rabbitmq.http.client.Client;
  4 +import com.rabbitmq.http.client.domain.TopicPermissions;
  5 +import com.rabbitmq.http.client.domain.UserPermissions;
  6 +import com.sunyo.wlpt.message.bus.service.domain.BusServer;
  7 +import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
  8 +import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
  9 +import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils;
  10 +
  11 +import java.io.IOException;
  12 +import java.net.URISyntaxException;
  13 +import java.util.ArrayList;
  14 +
  15 +/**
  16 + * @author 子诚
  17 + * Description:com.rabbitmq.http.client的封装类
  18 + * 时间:2020/8/13 17:57
  19 + */
  20 +public class ClientUtils {
  21 +
  22 + public static Client connectClient(BusServer busServer) throws IOException, URISyntaxException
  23 + {
  24 + // 服务器的IP地址
  25 + String host = busServer.getServerIp();
  26 + // 该服务器超级用户的用户名称
  27 + String superUsername = busServer.getSuperUsername();
  28 + // 该服务器超级用户的用户密码
  29 + String base = EncryptionUtils.decryptBase64(busServer.getSuperPassword());
  30 + String[] split = base.split("\\.");
  31 + String superPassword = split[split.length - 1];
  32 +
  33 + // 服务器的客户端端口号
  34 + String clientPort = busServer.getClientPort().toString();
  35 + // 访问客户端的url
  36 + String url = "http://" + host + ":" + clientPort + "/api";
  37 + Client client = new Client(url, superUsername, superPassword);
  38 + return client;
  39 + }
  40 +
  41 + /**
  42 + * 创建MQ用户
  43 + *
  44 + * @param userInfo 用户信息
  45 + * @param busServer {@link BusServer}
  46 + * @param password 新增用户的密码
  47 + */
  48 + public static void addRabbitUser(UserInfo userInfo, BusServer busServer, String password) throws IOException, URISyntaxException
  49 + {
  50 + // 新增用户的用户名称
  51 + String username = userInfo.getUsername();
  52 + // 虚拟主机名称
  53 + String virtualHostName = userInfo.getVirtualHostName();
  54 +
  55 + // 与客户端建立连接
  56 + Client client = connectClient(busServer);
  57 + ArrayList<String> list = new ArrayList<>();
  58 + // 创建用户,权限为none
  59 + client.createUser(username, password.toCharArray(), list);
  60 +
  61 + // 用户与虚拟主机建立联系
  62 + UserPermissions p = new UserPermissions();
  63 + p.setConfigure(username + ".*");
  64 + p.setRead(username + ".*");
  65 + p.setWrite(username + ".*");
  66 + client.updatePermissions(virtualHostName, username, p);
  67 +
  68 + TopicPermissions topicPermissions = new TopicPermissions();
  69 + topicPermissions.setVhost(virtualHostName);
  70 + topicPermissions.setExchange("");
  71 + topicPermissions.setRead(".*");
  72 + topicPermissions.setWrite(".*");
  73 + client.updateTopicPermissions(virtualHostName, username, topicPermissions);
  74 + }
  75 +
  76 + /**
  77 + * 删除用户
  78 + */
  79 + public static void deleteMQUser(UserInfo userInfo, BusServer busServer) throws IOException, URISyntaxException
  80 + {
  81 + String username = userInfo.getUsername();
  82 + Client client = connectClient(busServer);
  83 + client.deleteUser(username);
  84 + }
  85 +
  86 + /**
  87 + * 修改MQ的用户密码
  88 + */
  89 + public static void updatePassword(BusServer busServer, String username, String password) throws IOException, URISyntaxException
  90 + {
  91 + char[] newPassword = password.toCharArray();
  92 + Client client = connectClient(busServer);
  93 + ArrayList<String> tags = new ArrayList<>();
  94 + client.updateUser(username, newPassword, tags);
  95 + }
  96 +
  97 + /**
  98 + * 创建虚拟主机
  99 + */
  100 + public static void createVirtualHost(BusServer busServer, String vHost) throws IOException, URISyntaxException
  101 + {
  102 + Client client = connectClient(busServer);
  103 + client.createVhost(vHost);
  104 + }
  105 + /**
  106 + * 创建虚拟主机
  107 + */
  108 + public static void createVirtualHost(BusServer busServer, VirtualHost vHost) throws IOException, URISyntaxException
  109 + {
  110 + Client client = connectClient(busServer);
  111 + client.createVhost(vHost.getVirtualHostName(),vHost.getDescription());
  112 + }
  113 +
  114 + /**
  115 + * 删除虚拟主机
  116 + */
  117 + public static void deleteVirtualHost(BusServer busServer, String vHost) throws IOException, URISyntaxException
  118 + {
  119 + Client client = connectClient(busServer);
  120 + client.deleteVhost(vHost);
  121 + }
  122 +
  123 + /**
  124 + * 清楚用户与虚拟主机之间的关系
  125 + */
  126 + public static void clearPermissions(BusServer busServer, String vHost, String username) throws IOException, URISyntaxException
  127 + {
  128 + Client client = connectClient(busServer);
  129 + client.clearPermissions(vHost, username);
  130 + client.clearTopicPermissions(vHost, username);
  131 + }
  132 +}
@@ -4,6 +4,7 @@ import com.rabbitmq.client.*; @@ -4,6 +4,7 @@ import com.rabbitmq.client.*;
4 import com.sunyo.wlpt.message.bus.service.domain.XmlData; 4 import com.sunyo.wlpt.message.bus.service.domain.XmlData;
5 import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType; 5 import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
6 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 6 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
  7 +import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils;
7 import com.sunyo.wlpt.message.bus.service.utils.IdUtils; 8 import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
8 import lombok.extern.slf4j.Slf4j; 9 import lombok.extern.slf4j.Slf4j;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
@@ -95,6 +96,8 @@ public class DirectUtils { @@ -95,6 +96,8 @@ public class DirectUtils {
95 */ 96 */
96 public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws Exception 97 public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws Exception
97 { 98 {
  99 + String base = EncryptionUtils.decryptBase64(password);
  100 + String[] split = base.split("\\.");
98 //定义连接工厂 101 //定义连接工厂
99 ConnectionFactory factory = new ConnectionFactory(); 102 ConnectionFactory factory = new ConnectionFactory();
100 //设置服务地址 103 //设置服务地址
@@ -104,7 +107,7 @@ public class DirectUtils { @@ -104,7 +107,7 @@ public class DirectUtils {
104 //设置账号信息,用户名、密码、vhost 107 //设置账号信息,用户名、密码、vhost
105 factory.setVirtualHost(vHostName); 108 factory.setVirtualHost(vHostName);
106 factory.setUsername(userName); 109 factory.setUsername(userName);
107 - factory.setPassword(password); 110 + factory.setPassword(split[split.length - 1]);
108 // 通过工程获取连接 111 // 通过工程获取连接
109 return factory.newConnection(); 112 return factory.newConnection();
110 } 113 }
@@ -258,7 +261,7 @@ public class DirectUtils { @@ -258,7 +261,7 @@ public class DirectUtils {
258 { 261 {
259 // 1、创建Connection 262 // 1、创建Connection
260 Connection connection = getConnection(xmlData.getServerIp(), xmlData.getServerPort(), 263 Connection connection = getConnection(xmlData.getServerIp(), xmlData.getServerPort(),
261 - xmlData.getVirtualHostName()); 264 + xmlData.getVirtualHostName(),xmlData.getSuperUsername(), xmlData.getSuperPassword());
262 // 2、 通过Connection创建一个新的Channel 265 // 2、 通过Connection创建一个新的Channel
263 Channel channel = connection.createChannel(); 266 Channel channel = connection.createChannel();
264 // 3、开启消息的确认机制(confirm:保证消息能够发送到 exchange) 267 // 3、开启消息的确认机制(confirm:保证消息能够发送到 exchange)
@@ -6,6 +6,7 @@ import com.rabbitmq.client.ConnectionFactory; @@ -6,6 +6,7 @@ import com.rabbitmq.client.ConnectionFactory;
6 import com.sunyo.wlpt.message.bus.service.domain.*; 6 import com.sunyo.wlpt.message.bus.service.domain.*;
7 import com.sunyo.wlpt.message.bus.service.service.BusServerService; 7 import com.sunyo.wlpt.message.bus.service.service.BusServerService;
8 import com.sunyo.wlpt.message.bus.service.service.VirtualHostService; 8 import com.sunyo.wlpt.message.bus.service.service.VirtualHostService;
  9 +import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils;
9 import lombok.extern.slf4j.Slf4j; 10 import lombok.extern.slf4j.Slf4j;
10 import org.springframework.beans.factory.annotation.Value; 11 import org.springframework.beans.factory.annotation.Value;
11 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
@@ -87,64 +88,28 @@ public class RabbitUtils { @@ -87,64 +88,28 @@ public class RabbitUtils {
87 /** 88 /**
88 * 获取 rabbitMq 的连接,重载 89 * 获取 rabbitMq 的连接,重载
89 */ 90 */
90 - public Connection getConnection(String virtualHostName) throws IOException, TimeoutException  
91 - {  
92 - ConnectionFactory factory = new ConnectionFactory();  
93 - factory.setHost(host);  
94 - factory.setPort(port);  
95 - factory.setVirtualHost(virtualHostName);  
96 - factory.setUsername(username);  
97 - factory.setPassword(password);  
98 - Connection connection = factory.newConnection();  
99 - return connection;  
100 - }  
101 -  
102 - /**  
103 - * 获取 rabbitMq 的连接,重载  
104 - */  
105 - public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName) 91 + public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName, String superUsername, String superPassword)
106 throws IOException, TimeoutException 92 throws IOException, TimeoutException
107 { 93 {
  94 + String base = EncryptionUtils.decryptBase64(superPassword);
  95 + String[] split = base.split("\\.");
108 ConnectionFactory factory = new ConnectionFactory(); 96 ConnectionFactory factory = new ConnectionFactory();
109 factory.setHost(serverIp); 97 factory.setHost(serverIp);
110 factory.setPort(serverPort); 98 factory.setPort(serverPort);
111 factory.setVirtualHost(virtualHostName); 99 factory.setVirtualHost(virtualHostName);
112 - factory.setUsername(username);  
113 - factory.setPassword(password); 100 + factory.setUsername(superUsername);
  101 + factory.setPassword(split[split.length - 1]);
114 Connection connection = factory.newConnection(); 102 Connection connection = factory.newConnection();
115 return connection; 103 return connection;
116 } 104 }
117 105
118 /** 106 /**
119 - * 获取 rabbitMq 的连接,重载  
120 - *  
121 - * @param hostIp 服务器ip  
122 - * @param hostPort 服务器端口号  
123 - * @param vHostName 虚拟主机名称  
124 - * @param userName 用户名  
125 - * @param password 密码  
126 - * @return  
127 - * @throws Exception  
128 - */  
129 - public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password)  
130 - throws Exception  
131 - {  
132 - ConnectionFactory factory = new ConnectionFactory();  
133 - factory.setHost(hostIp);  
134 - factory.setPort(hostPort);  
135 - factory.setVirtualHost(vHostName);  
136 - factory.setUsername(userName);  
137 - factory.setPassword(password);  
138 - return factory.newConnection();  
139 - }  
140 -  
141 - /**  
142 * 添加交换机 107 * 添加交换机
143 */ 108 */
144 - public void createExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange) 109 + public void createExchange(BusServer server, String virtualHostName, BusExchange busExchange)
145 throws IOException, TimeoutException 110 throws IOException, TimeoutException
146 { 111 {
147 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 112 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
148 Channel channel = connection.createChannel(); 113 Channel channel = connection.createChannel();
149 channel.exchangeDeclare(busExchange.getExchangeName(), 114 channel.exchangeDeclare(busExchange.getExchangeName(),
150 busExchange.getExchangeType(), 115 busExchange.getExchangeType(),
@@ -158,10 +123,10 @@ public class RabbitUtils { @@ -158,10 +123,10 @@ public class RabbitUtils {
158 /** 123 /**
159 * 删除交换机 channel.exchangeDelete(exchangeName); 124 * 删除交换机 channel.exchangeDelete(exchangeName);
160 */ 125 */
161 - public void removeExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange) 126 + public void removeExchange(BusServer server, String virtualHostName, BusExchange busExchange)
162 throws IOException, TimeoutException 127 throws IOException, TimeoutException
163 { 128 {
164 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 129 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
165 Channel channel = connection.createChannel(); 130 Channel channel = connection.createChannel();
166 channel.exchangeDelete(busExchange.getExchangeName()); 131 channel.exchangeDelete(busExchange.getExchangeName());
167 closeConnectionAndChanel(channel, connection); 132 closeConnectionAndChanel(channel, connection);
@@ -170,10 +135,10 @@ public class RabbitUtils { @@ -170,10 +135,10 @@ public class RabbitUtils {
170 /** 135 /**
171 * 添加队列(默认设置参数为 null) 136 * 添加队列(默认设置参数为 null)
172 */ 137 */
173 - public void createQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue) 138 + public void createQueue(BusServer server, String virtualHostName, BusQueue busQueue)
174 throws IOException, TimeoutException 139 throws IOException, TimeoutException
175 { 140 {
176 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 141 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
177 Channel channel = connection.createChannel(); 142 Channel channel = connection.createChannel();
178 channel.queueDeclare(busQueue.getQueueName(), 143 channel.queueDeclare(busQueue.getQueueName(),
179 busQueue.getDurability(), 144 busQueue.getDurability(),
@@ -190,10 +155,10 @@ public class RabbitUtils { @@ -190,10 +155,10 @@ public class RabbitUtils {
190 /** 155 /**
191 * 删除队列 channel.queueDelete(queueName); 156 * 删除队列 channel.queueDelete(queueName);
192 */ 157 */
193 - public void removeQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue) 158 + public void removeQueue(BusServer server, String virtualHostName, BusQueue busQueue)
194 throws IOException, TimeoutException 159 throws IOException, TimeoutException
195 { 160 {
196 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 161 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
197 Channel channel = connection.createChannel(); 162 Channel channel = connection.createChannel();
198 channel.queueDelete(busQueue.getQueueName()); 163 channel.queueDelete(busQueue.getQueueName());
199 closeConnectionAndChanel(channel, connection); 164 closeConnectionAndChanel(channel, connection);
@@ -202,10 +167,10 @@ public class RabbitUtils { @@ -202,10 +167,10 @@ public class RabbitUtils {
202 /** 167 /**
203 * 创建绑定 168 * 创建绑定
204 */ 169 */
205 - public void createBinding(String serverIp, Integer serverPort, String virtualHostName, UserMessageBinding userMessageBinding) 170 + public void createBinding(BusServer server, String virtualHostName, UserMessageBinding userMessageBinding)
206 throws IOException, TimeoutException 171 throws IOException, TimeoutException
207 { 172 {
208 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 173 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
209 Channel channel = connection.createChannel(); 174 Channel channel = connection.createChannel();
210 channel.queueBind(userMessageBinding.getQueueName(), 175 channel.queueBind(userMessageBinding.getQueueName(),
211 userMessageBinding.getExchangeName(), 176 userMessageBinding.getExchangeName(),
@@ -216,11 +181,10 @@ public class RabbitUtils { @@ -216,11 +181,10 @@ public class RabbitUtils {
216 /** 181 /**
217 * 解除绑定 channel.queueUnbind("queueName", "exchangeName","routingKey"); 182 * 解除绑定 channel.queueUnbind("queueName", "exchangeName","routingKey");
218 */ 183 */
219 - public void removeBinding(String serverIp, Integer serverPort, String virtualHostName,  
220 - UserMessageBinding userMessageBinding) 184 + public void removeBinding(BusServer server, String virtualHostName, UserMessageBinding userMessageBinding)
221 throws IOException, TimeoutException 185 throws IOException, TimeoutException
222 { 186 {
223 - Connection connection = getConnection(serverIp, serverPort, virtualHostName); 187 + Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
224 Channel channel = connection.createChannel(); 188 Channel channel = connection.createChannel();
225 channel.queueUnbind(userMessageBinding.getQueueName(), 189 channel.queueUnbind(userMessageBinding.getQueueName(),
226 userMessageBinding.getExchangeName(), 190 userMessageBinding.getExchangeName(),
@@ -231,41 +195,45 @@ public class RabbitUtils { @@ -231,41 +195,45 @@ public class RabbitUtils {
231 /** 195 /**
232 * 前往创建交换机的路上 196 * 前往创建交换机的路上
233 */ 197 */
234 - public void toCreateExchange(BusExchange busExchange) throws IOException, TimeoutException 198 + public void toCreateExchange(BusExchange busExchange)
  199 + throws IOException, TimeoutException
235 { 200 {
236 VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId()); 201 VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
237 BusServer busServer = getBusServer(virtualHost.getServerId()); 202 BusServer busServer = getBusServer(virtualHost.getServerId());
238 - createExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange); 203 + createExchange(busServer, virtualHost.getVirtualHostName(), busExchange);
239 } 204 }
240 205
241 /** 206 /**
242 * 前往删除交换机的路上 207 * 前往删除交换机的路上
243 */ 208 */
244 - public void toRemoveExchange(BusExchange busExchange) throws IOException, TimeoutException 209 + public void toRemoveExchange(BusExchange busExchange)
  210 + throws IOException, TimeoutException
245 { 211 {
246 VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId()); 212 VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
247 BusServer busServer = getBusServer(virtualHost.getServerId()); 213 BusServer busServer = getBusServer(virtualHost.getServerId());
248 - removeExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange); 214 + removeExchange(busServer, virtualHost.getVirtualHostName(), busExchange);
249 } 215 }
250 216
251 /** 217 /**
252 * 前往创建队列的路上 218 * 前往创建队列的路上
253 */ 219 */
254 - public void toCreateQueue(BusQueue BusQueue) throws IOException, TimeoutException 220 + public void toCreateQueue(BusQueue BusQueue)
  221 + throws IOException, TimeoutException
255 { 222 {
256 VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId()); 223 VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
257 BusServer busServer = getBusServer(virtualHost.getServerId()); 224 BusServer busServer = getBusServer(virtualHost.getServerId());
258 - createQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue); 225 + createQueue(busServer, virtualHost.getVirtualHostName(), BusQueue);
259 } 226 }
260 227
261 /** 228 /**
262 * 前往删除队列的路上 229 * 前往删除队列的路上
263 */ 230 */
264 - public void toRemoveQueue(BusQueue BusQueue) throws IOException, TimeoutException 231 + public void toRemoveQueue(BusQueue BusQueue)
  232 + throws IOException, TimeoutException
265 { 233 {
266 VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId()); 234 VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
267 BusServer busServer = getBusServer(virtualHost.getServerId()); 235 BusServer busServer = getBusServer(virtualHost.getServerId());
268 - removeQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue); 236 + removeQueue(busServer, virtualHost.getVirtualHostName(), BusQueue);
269 } 237 }
270 238
271 /** 239 /**
@@ -276,7 +244,7 @@ public class RabbitUtils { @@ -276,7 +244,7 @@ public class RabbitUtils {
276 { 244 {
277 VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId()); 245 VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
278 BusServer busServer = getBusServer(virtualHost.getServerId()); 246 BusServer busServer = getBusServer(virtualHost.getServerId());
279 - createBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding); 247 + createBinding(busServer, virtualHost.getVirtualHostName(), userMessageBinding);
280 } 248 }
281 249
282 /** 250 /**
@@ -287,7 +255,7 @@ public class RabbitUtils { @@ -287,7 +255,7 @@ public class RabbitUtils {
287 { 255 {
288 VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId()); 256 VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
289 BusServer busServer = getBusServer(virtualHost.getServerId()); 257 BusServer busServer = getBusServer(virtualHost.getServerId());
290 - removeBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding); 258 + removeBinding(busServer, virtualHost.getVirtualHostName(), userMessageBinding);
291 } 259 }
292 260
293 /** 261 /**