1
|
package com.sunyo.wlpt.message.bus.service.service.impl;
|
1
|
package com.sunyo.wlpt.message.bus.service.service.impl;
|
2
|
|
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.BusQueue;
|
|
|
7
|
+import com.sunyo.wlpt.message.bus.service.domain.BusServer;
|
3
|
import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
|
8
|
import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
|
|
|
9
|
+import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
|
4
|
import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper;
|
10
|
import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper;
|
|
|
11
|
+import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils;
|
5
|
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
|
12
|
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
|
|
|
13
|
+import com.sunyo.wlpt.message.bus.service.service.BusQueueService;
|
|
|
14
|
+import com.sunyo.wlpt.message.bus.service.service.BusServerService;
|
6
|
import com.sunyo.wlpt.message.bus.service.service.UserInfoService;
|
15
|
import com.sunyo.wlpt.message.bus.service.service.UserInfoService;
|
|
|
16
|
+import com.sunyo.wlpt.message.bus.service.service.VirtualHostService;
|
7
|
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
|
17
|
import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
|
8
|
import io.netty.util.internal.StringUtil;
|
18
|
import io.netty.util.internal.StringUtil;
|
9
|
-import org.springframework.util.DigestUtils;
|
19
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
20
|
+import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
|
10
|
import org.springframework.stereotype.Service;
|
21
|
import org.springframework.stereotype.Service;
|
|
|
22
|
+import org.springframework.util.DigestUtils;
|
11
|
|
23
|
|
12
|
import javax.annotation.Resource;
|
24
|
import javax.annotation.Resource;
|
|
|
25
|
+import java.io.IOException;
|
|
|
26
|
+import java.net.MalformedURLException;
|
|
|
27
|
+import java.net.URISyntaxException;
|
|
|
28
|
+import java.util.ArrayList;
|
13
|
import java.util.List;
|
29
|
import java.util.List;
|
|
|
30
|
+import java.util.concurrent.TimeoutException;
|
14
|
|
31
|
|
15
|
/**
|
32
|
/**
|
16
|
* @author 子诚
|
33
|
* @author 子诚
|
|
@@ -23,6 +40,36 @@ public class UserInfoServiceImpl implements UserInfoService { |
|
@@ -23,6 +40,36 @@ public class UserInfoServiceImpl implements UserInfoService { |
23
|
@Resource
|
40
|
@Resource
|
24
|
private UserInfoMapper userInfoMapper;
|
41
|
private UserInfoMapper userInfoMapper;
|
25
|
|
42
|
|
|
|
43
|
+ @Resource
|
|
|
44
|
+ private RabbitProperties rabbitProperties;
|
|
|
45
|
+
|
|
|
46
|
+ @Resource
|
|
|
47
|
+ private RabbitUtils rabbitUtils;
|
|
|
48
|
+
|
|
|
49
|
+ @Resource
|
|
|
50
|
+ private BusServerService busServerService;
|
|
|
51
|
+
|
|
|
52
|
+ @Resource
|
|
|
53
|
+ private VirtualHostService virtualHostService;
|
|
|
54
|
+
|
|
|
55
|
+ @Resource
|
|
|
56
|
+ private BusQueueService busQueueService;
|
|
|
57
|
+
|
|
|
58
|
+ @Value("${spring.rabbitmq.host}")
|
|
|
59
|
+ private String host;
|
|
|
60
|
+
|
|
|
61
|
+ @Value("${spring.rabbitmq.port}")
|
|
|
62
|
+ private int port;
|
|
|
63
|
+
|
|
|
64
|
+ @Value("${spring.rabbitmq.username}")
|
|
|
65
|
+ private String rabbitUsername;
|
|
|
66
|
+
|
|
|
67
|
+ @Value("${spring.rabbitmq.password}")
|
|
|
68
|
+ private String rabbitPassword;
|
|
|
69
|
+
|
|
|
70
|
+ @Value("${spring.rabbitmq.virtual-host}")
|
|
|
71
|
+ private String vHost;
|
|
|
72
|
+
|
26
|
@Override
|
73
|
@Override
|
27
|
public int deleteByPrimaryKey(String id)
|
74
|
public int deleteByPrimaryKey(String id)
|
28
|
{
|
75
|
{
|
|
@@ -36,21 +83,84 @@ public class UserInfoServiceImpl implements UserInfoService { |
|
@@ -36,21 +83,84 @@ public class UserInfoServiceImpl implements UserInfoService { |
36
|
}
|
83
|
}
|
37
|
|
84
|
|
38
|
@Override
|
85
|
@Override
|
39
|
- public ResultJson insertSelective(UserInfo userInfo)
|
86
|
+ public ResultJson insertSelective(UserInfo userInfo) throws IOException, URISyntaxException, TimeoutException
|
40
|
{
|
87
|
{
|
|
|
88
|
+ String password = userInfo.getPassword();
|
|
|
89
|
+
|
|
|
90
|
+ VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(userInfo.getVirtualHostId());
|
|
|
91
|
+ userInfo.setVirtualHostName(virtualHost.getVirtualHostName());
|
|
|
92
|
+ BusServer busServer = busServerService.selectByPrimaryKey(userInfo.getServerId());
|
|
|
93
|
+ userInfo.setServerName(busServer.getServerName());
|
|
|
94
|
+
|
41
|
ResultJson validateResult = validateUser(userInfo);
|
95
|
ResultJson validateResult = validateUser(userInfo);
|
42
|
if (!"200".equals(validateUser(userInfo).getCode())) {
|
96
|
if (!"200".equals(validateUser(userInfo).getCode())) {
|
43
|
return validateResult;
|
97
|
return validateResult;
|
44
|
}
|
98
|
}
|
45
|
- if (StringUtil.isNullOrEmpty(userInfo.getId())) {
|
|
|
46
|
- userInfo.setId(IdUtils.generateId());
|
|
|
47
|
- }
|
99
|
+ userInfo.setId(IdUtils.generateId());
|
48
|
userInfo.setPassword(DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes()));
|
100
|
userInfo.setPassword(DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes()));
|
49
|
- return userInfoMapper.insertSelective(userInfo) > 0
|
101
|
+
|
|
|
102
|
+ int num = userInfoMapper.insertSelective(userInfo);
|
|
|
103
|
+ // 1.根据用户信息,在MQ创建用户
|
|
|
104
|
+ addMQUser(userInfo, password);
|
|
|
105
|
+ // 3.在对应虚拟机下创建队列
|
|
|
106
|
+ createQueue(userInfo);
|
|
|
107
|
+
|
|
|
108
|
+ return num > 0
|
50
|
? new ResultJson<>("200", "添加用户信息,成功")
|
109
|
? new ResultJson<>("200", "添加用户信息,成功")
|
51
|
: new ResultJson<>("500", "添加用户信息,失败");
|
110
|
: new ResultJson<>("500", "添加用户信息,失败");
|
52
|
}
|
111
|
}
|
53
|
|
112
|
|
|
|
113
|
+ /**
|
|
|
114
|
+ * 根据用户信息,添加队列
|
|
|
115
|
+ *
|
|
|
116
|
+ * @param userInfo
|
|
|
117
|
+ * @throws IOException
|
|
|
118
|
+ * @throws TimeoutException
|
|
|
119
|
+ */
|
|
|
120
|
+ void createQueue(UserInfo userInfo) throws IOException, TimeoutException
|
|
|
121
|
+ {
|
|
|
122
|
+ BusQueue busQueue = BusQueue.builder()
|
|
|
123
|
+ .id(IdUtils.generateId())
|
|
|
124
|
+ .userId(userInfo.getId())
|
|
|
125
|
+ .username(userInfo.getUsername())
|
|
|
126
|
+ .queueName(userInfo.getUsername() + "_R")
|
|
|
127
|
+ .virtualHostId(userInfo.getVirtualHostId())
|
|
|
128
|
+ .durability(true)
|
|
|
129
|
+ .autoDelete(false)
|
|
|
130
|
+ .description(userInfo.getUsername() + ":专属队列")
|
|
|
131
|
+ .build();
|
|
|
132
|
+ busQueueService.insertSelective(busQueue);
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+
|
|
|
136
|
+ /**
|
|
|
137
|
+ * 添加MQ用户信息,并配置MQ用户与虚拟主机的关系
|
|
|
138
|
+ *
|
|
|
139
|
+ * @param userInfo {@link UserInfo}
|
|
|
140
|
+ */
|
|
|
141
|
+ public void addMQUser(UserInfo userInfo, String password) throws MalformedURLException, URISyntaxException
|
|
|
142
|
+ {
|
|
|
143
|
+ String username = userInfo.getUsername();
|
|
|
144
|
+ String virtualHostName = userInfo.getVirtualHostName();
|
|
|
145
|
+
|
|
|
146
|
+ String url = "http://" + host + ":15672/api";
|
|
|
147
|
+ Client client = new Client(url, rabbitUsername, rabbitPassword);
|
|
|
148
|
+ ArrayList<String> list = new ArrayList<>();
|
|
|
149
|
+ client.createUser(username, password.toCharArray(), list);
|
|
|
150
|
+
|
|
|
151
|
+ UserPermissions p = new UserPermissions();
|
|
|
152
|
+ p.setConfigure(username + ".*");
|
|
|
153
|
+ p.setRead(username + ".*");
|
|
|
154
|
+ p.setWrite(username + ".*");
|
|
|
155
|
+ client.updatePermissions(virtualHostName, username, p);
|
|
|
156
|
+
|
|
|
157
|
+ TopicPermissions topicPermissions = new TopicPermissions();
|
|
|
158
|
+ topicPermissions.setVhost(virtualHostName);
|
|
|
159
|
+ topicPermissions.setExchange("");
|
|
|
160
|
+ topicPermissions.setRead(".*");
|
|
|
161
|
+ topicPermissions.setWrite(".*");
|
|
|
162
|
+ client.updateTopicPermissions(virtualHostName, username, topicPermissions);
|
|
|
163
|
+ }
|
54
|
|
164
|
|
55
|
@Override
|
165
|
@Override
|
56
|
public UserInfo selectByPrimaryKey(String id)
|
166
|
public UserInfo selectByPrimaryKey(String id)
|
|
@@ -70,8 +180,6 @@ public class UserInfoServiceImpl implements UserInfoService { |
|
@@ -70,8 +180,6 @@ public class UserInfoServiceImpl implements UserInfoService { |
70
|
if (StringUtil.isNullOrEmpty(userInfo.getId())) {
|
180
|
if (StringUtil.isNullOrEmpty(userInfo.getId())) {
|
71
|
return new ResultJson<>("400", "该用户不存在");
|
181
|
return new ResultJson<>("400", "该用户不存在");
|
72
|
}
|
182
|
}
|
73
|
-
|
|
|
74
|
-
|
|
|
75
|
return userInfoMapper.updateByPrimaryKeySelective(userInfo) > 0
|
183
|
return userInfoMapper.updateByPrimaryKeySelective(userInfo) > 0
|
76
|
? new ResultJson<>("200", "修改用户信息,成功")
|
184
|
? new ResultJson<>("200", "修改用户信息,成功")
|
77
|
: new ResultJson<>("500", "修改用户信息,失败");
|
185
|
: new ResultJson<>("500", "修改用户信息,失败");
|
|
@@ -85,6 +193,9 @@ public class UserInfoServiceImpl implements UserInfoService { |
|
@@ -85,6 +193,9 @@ public class UserInfoServiceImpl implements UserInfoService { |
85
|
if (StringUtil.isNullOrEmpty(userInfo.getUsername()) || StringUtil.isNullOrEmpty(userInfo.getPassword())) {
|
193
|
if (StringUtil.isNullOrEmpty(userInfo.getUsername()) || StringUtil.isNullOrEmpty(userInfo.getPassword())) {
|
86
|
return new ResultJson<>("400", "用户名称和密码,不能为空");
|
194
|
return new ResultJson<>("400", "用户名称和密码,不能为空");
|
87
|
}
|
195
|
}
|
|
|
196
|
+ if (StringUtil.isNullOrEmpty(userInfo.getServerName()) || StringUtil.isNullOrEmpty(userInfo.getVirtualHostName())) {
|
|
|
197
|
+ return new ResultJson<>("400", "服务器和虚拟主机,不能为空");
|
|
|
198
|
+ }
|
88
|
String userId = userInfo.getId();
|
199
|
String userId = userInfo.getId();
|
89
|
if (!StringUtil.isNullOrEmpty(userId)) {
|
200
|
if (!StringUtil.isNullOrEmpty(userId)) {
|
90
|
UserInfo oldUserInfo = userInfoMapper.selectByPrimaryKey(userId);
|
201
|
UserInfo oldUserInfo = userInfoMapper.selectByPrimaryKey(userId);
|