作者 王勇

使用http-client创建MQ用户

@@ -29,18 +29,26 @@ @@ -29,18 +29,26 @@
29 <groupId>org.springframework.boot</groupId> 29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-web</artifactId> 30 <artifactId>spring-boot-starter-web</artifactId>
31 </dependency> 31 </dependency>
  32 +
32 <dependency> 33 <dependency>
33 <groupId>org.springframework.boot</groupId> 34 <groupId>org.springframework.boot</groupId>
34 <artifactId>spring-boot-starter</artifactId> 35 <artifactId>spring-boot-starter</artifactId>
35 </dependency> 36 </dependency>
  37 +
36 <dependency> 38 <dependency>
37 <groupId>org.springframework.boot</groupId> 39 <groupId>org.springframework.boot</groupId>
38 <artifactId>spring-boot-starter-security</artifactId> 40 <artifactId>spring-boot-starter-security</artifactId>
39 </dependency> 41 </dependency>
  42 +
40 <dependency> 43 <dependency>
41 <groupId>org.springframework.boot</groupId> 44 <groupId>org.springframework.boot</groupId>
42 <artifactId>spring-boot-starter-amqp</artifactId> 45 <artifactId>spring-boot-starter-amqp</artifactId>
43 - <version>2.1.8.RELEASE</version> 46 + </dependency>
  47 +
  48 + <dependency>
  49 + <groupId>com.rabbitmq</groupId>
  50 + <artifactId>http-client</artifactId>
  51 + <version>3.7.0.RELEASE</version>
44 </dependency> 52 </dependency>
45 53
46 <dependency> 54 <dependency>
@@ -48,6 +56,7 @@ @@ -48,6 +56,7 @@
48 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 56 <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
49 </dependency> 57 </dependency>
50 <!-- SpringBoot end --> 58 <!-- SpringBoot end -->
  59 +
51 <!-- SpringCloud start --> 60 <!-- SpringCloud start -->
52 <dependency> 61 <dependency>
53 <groupId>org.springframework.cloud</groupId> 62 <groupId>org.springframework.cloud</groupId>
@@ -15,8 +15,18 @@ import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; @@ -15,8 +15,18 @@ import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
15 */ 15 */
16 @Configuration 16 @Configuration
17 public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { 17 public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
  18 +
  19 +
  20 + private String hostname;
  21 +
  22 + private Integer port;
  23 +
  24 + private String scheme;
  25 +
  26 +
18 @Override 27 @Override
19 @Bean 28 @Bean
  29 +
20 public RestHighLevelClient elasticsearchClient() 30 public RestHighLevelClient elasticsearchClient()
21 { 31 {
22 RestHighLevelClient client = new RestHighLevelClient( 32 RestHighLevelClient client = new RestHighLevelClient(
@@ -29,7 +39,8 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration { @@ -29,7 +39,8 @@ public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
29 } 39 }
30 40
31 @Bean 41 @Bean
32 - public ElasticsearchRestTemplate elasticsearchRestTemplate() { 42 + public ElasticsearchRestTemplate elasticsearchRestTemplate()
  43 + {
33 return new ElasticsearchRestTemplate(elasticsearchClient()); 44 return new ElasticsearchRestTemplate(elasticsearchClient());
34 } 45 }
35 } 46 }
@@ -6,7 +6,10 @@ import com.sunyo.wlpt.message.bus.service.service.UserInfoService; @@ -6,7 +6,10 @@ import com.sunyo.wlpt.message.bus.service.service.UserInfoService;
6 import org.springframework.web.bind.annotation.*; 6 import org.springframework.web.bind.annotation.*;
7 7
8 import javax.annotation.Resource; 8 import javax.annotation.Resource;
  9 +import java.io.IOException;
  10 +import java.net.URISyntaxException;
9 import java.util.List; 11 import java.util.List;
  12 +import java.util.concurrent.TimeoutException;
10 13
11 /** 14 /**
12 * @author 子诚 15 * @author 子诚
@@ -40,30 +43,12 @@ public class UserInfoController { @@ -40,30 +43,12 @@ public class UserInfoController {
40 * @return {@link ResultJson} 43 * @return {@link ResultJson}
41 */ 44 */
42 @PostMapping("/insert") 45 @PostMapping("/insert")
43 - public ResultJson insertUserByEntity(UserInfo userInfo) 46 + public ResultJson insertUserByEntity(@RequestBody UserInfo userInfo) throws IOException, URISyntaxException, TimeoutException
44 { 47 {
45 return userInfoService.insertSelective(userInfo); 48 return userInfoService.insertSelective(userInfo);
46 } 49 }
47 50
48 /** 51 /**
49 - * @param description 描述  
50 - * @param username 用户名称  
51 - * @param password 密码  
52 - * @return {@link ResultJson}  
53 - */  
54 - @PostMapping("/insertByParam")  
55 - public ResultJson insertUserByParam(@RequestParam(value = "description", required = false) String description,  
56 - String username, String password)  
57 - {  
58 - // 接收参数  
59 - UserInfo userInfo = UserInfo.builder().username(username)  
60 - .password(password)  
61 - .description(description)  
62 - .build();  
63 - return userInfoService.insertSelective(userInfo);  
64 - }  
65 -  
66 - /**  
67 * 编辑用户信息 52 * 编辑用户信息
68 * 53 *
69 * @param userInfo {@link UserInfo} 54 * @param userInfo {@link UserInfo}
@@ -3,7 +3,10 @@ package com.sunyo.wlpt.message.bus.service.service; @@ -3,7 +3,10 @@ package com.sunyo.wlpt.message.bus.service.service;
3 import com.sunyo.wlpt.message.bus.service.domain.UserInfo; 3 import com.sunyo.wlpt.message.bus.service.domain.UserInfo;
4 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 4 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
5 5
  6 +import java.io.IOException;
  7 +import java.net.URISyntaxException;
6 import java.util.List; 8 import java.util.List;
  9 +import java.util.concurrent.TimeoutException;
7 10
8 /** 11 /**
9 * @author 子诚 12 * @author 子诚
@@ -34,7 +37,7 @@ public interface UserInfoService { @@ -34,7 +37,7 @@ public interface UserInfoService {
34 * @param record the record 37 * @param record the record
35 * @return insert count 38 * @return insert count
36 */ 39 */
37 - ResultJson insertSelective(UserInfo record); 40 + ResultJson insertSelective(UserInfo record) throws IOException, URISyntaxException, TimeoutException;
38 41
39 /** 42 /**
40 * 查询,根据主键 43 * 查询,根据主键
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);