作者 朱兆平

kafka消息总线路由管理消息联查

@@ -6,6 +6,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusQueue; @@ -6,6 +6,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
6 import com.sunyo.wlpt.message.bus.service.model.MessageType; 6 import com.sunyo.wlpt.message.bus.service.model.MessageType;
7 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 7 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
8 import com.sunyo.wlpt.message.bus.service.service.MessageTypeService; 8 import com.sunyo.wlpt.message.bus.service.service.MessageTypeService;
  9 +import io.swagger.annotations.ApiOperation;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
11 import org.springframework.web.bind.annotation.*; 12 import org.springframework.web.bind.annotation.*;
@@ -36,4 +37,12 @@ public class MessageTypesController { @@ -36,4 +37,12 @@ public class MessageTypesController {
36 PageInfo<MessageType> list = messageTypeService.pageList(messageType); 37 PageInfo<MessageType> list = messageTypeService.pageList(messageType);
37 return new ResultJson("200","success",list); 38 return new ResultJson("200","success",list);
38 } 39 }
  40 +
  41 + @ApiOperation(value = "添加一个消息类", notes = "类名与消息类型必填")
  42 + @PostMapping("add")
  43 + public ResultJson add(@RequestBody MessageType messageType){
  44 + boolean result = messageTypeService.add(messageType);
  45 + return result ? new ResultJson("200","success") : new ResultJson("400","error");
  46 + }
  47 +
39 } 48 }
@@ -2,7 +2,10 @@ package com.sunyo.wlpt.message.bus.service.controller; @@ -2,7 +2,10 @@ package com.sunyo.wlpt.message.bus.service.controller;
2 2
3 3
4 import com.sunyo.wlpt.message.bus.service.model.MessageRouter; 4 import com.sunyo.wlpt.message.bus.service.model.MessageRouter;
  5 +import com.sunyo.wlpt.message.bus.service.response.ResultJson;
  6 +import com.sunyo.wlpt.message.bus.service.service.RouterService;
5 import io.swagger.annotations.ApiOperation; 7 import io.swagger.annotations.ApiOperation;
  8 +import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.PostMapping; 9 import org.springframework.web.bind.annotation.PostMapping;
7 import org.springframework.web.bind.annotation.RequestBody; 10 import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMapping;
@@ -10,16 +13,18 @@ import org.springframework.web.bind.annotation.RestController; @@ -10,16 +13,18 @@ import org.springframework.web.bind.annotation.RestController;
10 13
11 import java.util.List; 14 import java.util.List;
12 15
13 -@RequestMapping("router") 16 +@RequestMapping("/bus/router")
14 @RestController 17 @RestController
15 public class RouterController { 18 public class RouterController {
16 19
17 - @ApiOperation(value = "批量添加消息路由", notes = "超级管理修改其他用户密码")  
18 - @PostMapping("batchAdd")  
19 - public void batchAddRouter(@RequestBody MessageRouter messageRouter){  
20 -  
21 - 20 + @Autowired
  21 + RouterService routerService;
22 22
23 23
  24 + @ApiOperation(value = "批量添加消息路由", notes = "超级管理修改其他用户密码")
  25 + @PostMapping("batchAdd")
  26 + public ResultJson batchAddRouter(@RequestBody MessageRouter messageRouter){
  27 + boolean result = routerService.batchAddRouter(messageRouter);
  28 + return result ? new ResultJson("200","success") :new ResultJson("400","error");
24 } 29 }
25 } 30 }
@@ -61,6 +61,8 @@ public interface BusQueueMapper { @@ -61,6 +61,8 @@ public interface BusQueueMapper {
61 */ 61 */
62 List<BusQueue> selectByVirtualHostId(String virtualHostId); 62 List<BusQueue> selectByVirtualHostId(String virtualHostId);
63 63
  64 + List<BusQueue> selectTopicByUsername(String virtualHostId);
  65 +
64 /** 66 /**
65 * update record selective 67 * update record selective
66 * 68 *
@@ -2,6 +2,8 @@ package com.sunyo.wlpt.message.bus.service.mapper; @@ -2,6 +2,8 @@ package com.sunyo.wlpt.message.bus.service.mapper;
2 2
3 import com.sunyo.wlpt.message.bus.service.model.MessageRouterReciver; 3 import com.sunyo.wlpt.message.bus.service.model.MessageRouterReciver;
4 4
  5 +import java.util.List;
  6 +
5 public interface MessageRouterReciverMapper { 7 public interface MessageRouterReciverMapper {
6 int deleteByPrimaryKey(String id); 8 int deleteByPrimaryKey(String id);
7 9
@@ -11,6 +13,8 @@ public interface MessageRouterReciverMapper { @@ -11,6 +13,8 @@ public interface MessageRouterReciverMapper {
11 13
12 MessageRouterReciver selectByPrimaryKey(String id); 14 MessageRouterReciver selectByPrimaryKey(String id);
13 15
  16 + List<MessageRouterReciver> selectByRouterKey(String id);
  17 +
14 int updateByPrimaryKeySelective(MessageRouterReciver record); 18 int updateByPrimaryKeySelective(MessageRouterReciver record);
15 19
16 int updateByPrimaryKey(MessageRouterReciver record); 20 int updateByPrimaryKey(MessageRouterReciver record);
@@ -2,6 +2,8 @@ package com.sunyo.wlpt.message.bus.service.mapper; @@ -2,6 +2,8 @@ package com.sunyo.wlpt.message.bus.service.mapper;
2 2
3 import com.sunyo.wlpt.message.bus.service.model.UserTopic; 3 import com.sunyo.wlpt.message.bus.service.model.UserTopic;
4 4
  5 +import java.util.List;
  6 +
5 public interface UserTopicMapper { 7 public interface UserTopicMapper {
6 int deleteByPrimaryKey(String id); 8 int deleteByPrimaryKey(String id);
7 9
@@ -30,6 +30,8 @@ public class MessageRouter { @@ -30,6 +30,8 @@ public class MessageRouter {
30 30
31 private Date updateTime; 31 private Date updateTime;
32 32
  33 + private List<MessageRouterReciver> rcvrList;
  34 +
33 private List<String> sndrs; 35 private List<String> sndrs;
34 36
35 private List<MessageType> types; 37 private List<MessageType> types;
@@ -163,4 +165,12 @@ public class MessageRouter { @@ -163,4 +165,12 @@ public class MessageRouter {
163 public void setRcvrs(List<String> rcvrs) { 165 public void setRcvrs(List<String> rcvrs) {
164 this.rcvrs = rcvrs; 166 this.rcvrs = rcvrs;
165 } 167 }
  168 +
  169 + public List<MessageRouterReciver> getRcvrList() {
  170 + return rcvrList;
  171 + }
  172 +
  173 + public void setRcvrList(List<MessageRouterReciver> rcvrList) {
  174 + this.rcvrList = rcvrList;
  175 + }
166 } 176 }
@@ -12,4 +12,6 @@ public interface MessageTypeService { @@ -12,4 +12,6 @@ public interface MessageTypeService {
12 12
13 List<MessageType> list(MessageType messageType); 13 List<MessageType> list(MessageType messageType);
14 14
  15 + boolean add(MessageType messageType);
  16 +
15 } 17 }
@@ -6,6 +6,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusQueue; @@ -6,6 +6,7 @@ import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
6 import com.sunyo.wlpt.message.bus.service.mapper.MessageTypeMapper; 6 import com.sunyo.wlpt.message.bus.service.mapper.MessageTypeMapper;
7 import com.sunyo.wlpt.message.bus.service.model.MessageType; 7 import com.sunyo.wlpt.message.bus.service.model.MessageType;
8 import com.sunyo.wlpt.message.bus.service.service.MessageTypeService; 8 import com.sunyo.wlpt.message.bus.service.service.MessageTypeService;
  9 +import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
9 import org.springframework.stereotype.Service; 10 import org.springframework.stereotype.Service;
10 11
11 import javax.annotation.Resource; 12 import javax.annotation.Resource;
@@ -30,4 +31,12 @@ public class MessageTypeServiceImp implements MessageTypeService { @@ -30,4 +31,12 @@ public class MessageTypeServiceImp implements MessageTypeService {
30 PageInfo<MessageType> pageInfo = new PageInfo<>(list); 31 PageInfo<MessageType> pageInfo = new PageInfo<>(list);
31 return pageInfo; 32 return pageInfo;
32 } 33 }
  34 +
  35 + @Override
  36 + public boolean add(MessageType messageType) {
  37 + String id = IdUtils.generateId();
  38 + messageType.setId(id);
  39 + int i = messageTypeMapper.insertSelective(messageType);
  40 + return i > 0;
  41 + }
33 } 42 }
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.sunyo.wlpt.message.bus.service.domain.BusQueue;
  4 +import com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper;
  5 +import com.sunyo.wlpt.message.bus.service.mapper.MessageRouterMapper;
  6 +import com.sunyo.wlpt.message.bus.service.mapper.MessageRouterReciverMapper;
  7 +import com.sunyo.wlpt.message.bus.service.mapper.UserTopicMapper;
3 import com.sunyo.wlpt.message.bus.service.model.MessageRouter; 8 import com.sunyo.wlpt.message.bus.service.model.MessageRouter;
4 import com.sunyo.wlpt.message.bus.service.model.MessageRouterReciver; 9 import com.sunyo.wlpt.message.bus.service.model.MessageRouterReciver;
  10 +import com.sunyo.wlpt.message.bus.service.model.MessageType;
  11 +import com.sunyo.wlpt.message.bus.service.model.UserTopic;
5 import com.sunyo.wlpt.message.bus.service.service.RouterService; 12 import com.sunyo.wlpt.message.bus.service.service.RouterService;
6 import com.sunyo.wlpt.message.bus.service.utils.IdUtils; 13 import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
  14 +import org.springframework.stereotype.Service;
  15 +import org.springframework.transaction.annotation.Transactional;
7 16
  17 +import javax.annotation.Resource;
8 import java.util.List; 18 import java.util.List;
9 19
  20 +@Service
10 public class RouterServiceImp implements RouterService { 21 public class RouterServiceImp implements RouterService {
11 22
  23 + @Resource
  24 + MessageRouterMapper messageRouterMapper;
12 25
  26 + @Resource
  27 + MessageRouterReciverMapper routerReciverMapper;
  28 +
  29 + @Resource
  30 + BusQueueMapper busQueueMapper;
  31 +
  32 +
  33 + @Transactional(rollbackFor = Exception.class)
13 @Override 34 @Override
14 public boolean batchAddRouter(MessageRouter record) { 35 public boolean batchAddRouter(MessageRouter record) {
  36 + if (record.getSndrs()!=null && record.getTypes()!=null && record.getRcvrs()!=null){
15 37
16 - List<String> senders = record.getSndrs();  
17 - if (senders!=null && !senders.isEmpty()){  
18 - /**  
19 - * 批量增加发送者表  
20 - */  
21 - for (String sndr: senders) {  
22 - String id = IdUtils.generateId();  
23 - MessageRouter router = new MessageRouter();  
24 - router.setId(id);  
25 - router.setSndr(sndr);  
26 - router.setDes(record.getDes());  
27 - router.setVer(record.getVer());  
28 - router.setStatus(record.getStatus());  
29 - router.setUsage(record.getUsage()); 38 + List<String> senders = record.getSndrs();
  39 + if (senders!=null && !senders.isEmpty()){
30 /** 40 /**
31 - * 批量增加接收者表 41 + * 批量增加发送者表
32 */ 42 */
33 - List<String> receivers = record.getRcvrs();  
34 - if (receivers!=null && !receivers.isEmpty()){  
35 - for (String rcvr :receivers) {  
36 - MessageRouterReciver messageRouterReciver = new MessageRouterReciver();  
37 - messageRouterReciver.setId(id);  
38 - messageRouterReciver.setMessageRouterId(id);  
39 - messageRouterReciver.setRcvrTopic(rcvr); 43 + for (String sndr: senders) {
  44 + String id = IdUtils.generateId();
  45 + MessageRouter router = new MessageRouter();
  46 + router.setId(id);
  47 + router.setSndr(sndr);
  48 + router.setDes(record.getDes());
  49 + router.setVer(record.getVer());
  50 + router.setStatus(record.getStatus());
  51 + router.setUsage(record.getUsage());
  52 +
  53 + /**
  54 + * 消息类型处理
  55 + */
  56 + List<MessageType> messageTypes = record.getTypes();
  57 + if (messageTypes!=null && !messageTypes.isEmpty()){
  58 + for (MessageType type: messageTypes) {
  59 + /**
  60 + * 判断消息类型是 大类 主类还是子类
  61 + */
  62 + if (type.getType() == 2){
  63 + //大类处理
  64 + router.setBtype(type.getName());
  65 + }
  66 + if (type.getType() == 3){
  67 + //二级类处理
  68 + router.setStype(type.getName());
  69 + }
  70 + }
40 } 71 }
41 - }  
42 72
  73 + /**
  74 + * 路由生产者消息入库
  75 + */
  76 + messageRouterMapper.insertSelective(router);
  77 +
  78 + /**
  79 + * 批量增加接收者表
  80 + * 这里得receiver列表指的是接收者账号
  81 + * 要通过账号找到 账号所对应的队列信息 再入库
  82 + */
  83 + List<String> receivers = record.getRcvrs();
  84 + if (receivers!=null && !receivers.isEmpty()){
  85 + for (String rcvr :receivers) {
  86 + String rcvr_id = IdUtils.generateId();
  87 + MessageRouterReciver messageRouterReciver = new MessageRouterReciver();
  88 + messageRouterReciver.setId(rcvr_id);
  89 + messageRouterReciver.setMessageRouterId(id);
  90 + /**
  91 + * 寻找账号对应的队列名称
  92 + */
  93 + List<BusQueue> busQueues = busQueueMapper.selectTopicByUsername(rcvr);
  94 + messageRouterReciver.setRcvrTopic(busQueues.get(0).getQueueName());
43 95
  96 + /**
  97 + * 插入数据到订阅者表
  98 + */
  99 + routerReciverMapper.insertSelective(messageRouterReciver);
  100 +
  101 + }
  102 + }
  103 +
  104 +
  105 + }
44 } 106 }
  107 + return true;
  108 + }else {
  109 + return false;
45 } 110 }
46 - return false;  
47 } 111 }
48 } 112 }
@@ -42,6 +42,32 @@ @@ -42,6 +42,32 @@
42 where id = #{id,jdbcType=VARCHAR} 42 where id = #{id,jdbcType=VARCHAR}
43 </select> 43 </select>
44 44
  45 +<select id="selectTopicByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" >
  46 + select
  47 + bq.id,
  48 + bq.queue_name,
  49 + bq.user_id,
  50 + bq.username,
  51 + bq.virtual_host_id,
  52 + bq.durability,
  53 + bq.auto_delete,
  54 + bq.arguments,
  55 + bq.description,
  56 + bq.gmt_create,
  57 + bq.gmt_modified,
  58 + bq.queue_type,
  59 + bq.partition_count,
  60 + bq.server_type
  61 + from user_topic ut
  62 + LEFT JOIN bus_queue bq
  63 + on
  64 + ut.bus_queue_id = bq.id
  65 + where
  66 + bq.server_type = 1
  67 + and bq.queue_type = 1
  68 + and ut.username = #{username,jdbcType=VARCHAR}
  69 +</select>
  70 +
45 <select id="selectByVirtualHostId" parameterType="java.lang.String" resultMap="BaseResultMap"> 71 <select id="selectByVirtualHostId" parameterType="java.lang.String" resultMap="BaseResultMap">
46 select 72 select
47 <include refid="Base_Column_List"/> 73 <include refid="Base_Column_List"/>
@@ -15,9 +15,10 @@ @@ -15,9 +15,10 @@
15 <result column="ver" property="ver" jdbcType="VARCHAR" /> 15 <result column="ver" property="ver" jdbcType="VARCHAR" />
16 <result column="creat_time" property="creatTime" jdbcType="TIMESTAMP" /> 16 <result column="creat_time" property="creatTime" jdbcType="TIMESTAMP" />
17 <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> 17 <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  18 + <collection column="id" javaType="java.util.ArrayList" ofType="com.sunyo.wlpt.message.bus.service.model.MessageRouterReciver" property="rcvrList" select="com.sunyo.wlpt.message.bus.service.mapper.selectByRouterKey" />
18 </resultMap> 19 </resultMap>
19 <sql id="Base_Column_List" > 20 <sql id="Base_Column_List" >
20 - id, sndr, btype, stype, optype, msg_limit, character, status, usage, des, ver, creat_time, 21 + id, sndr, btype, stype, optype, msg_limit, `character`, status, `usage`, des, ver, creat_time,
21 update_time 22 update_time
22 </sql> 23 </sql>
23 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > 24 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
@@ -33,7 +34,7 @@ @@ -33,7 +34,7 @@
33 <insert id="insert" parameterType="com.sunyo.wlpt.message.bus.service.model.MessageRouter" > 34 <insert id="insert" parameterType="com.sunyo.wlpt.message.bus.service.model.MessageRouter" >
34 insert into message_router (id, sndr, btype, 35 insert into message_router (id, sndr, btype,
35 stype, optype, msg_limit, 36 stype, optype, msg_limit,
36 - character, status, usage, des, 37 + `character`, status, `usage`, des,
37 ver, creat_time, update_time 38 ver, creat_time, update_time
38 ) 39 )
39 values (#{id,jdbcType=VARCHAR}, #{sndr,jdbcType=VARCHAR}, #{btype,jdbcType=VARCHAR}, 40 values (#{id,jdbcType=VARCHAR}, #{sndr,jdbcType=VARCHAR}, #{btype,jdbcType=VARCHAR},
@@ -64,13 +65,13 @@ @@ -64,13 +65,13 @@
64 msg_limit, 65 msg_limit,
65 </if> 66 </if>
66 <if test="character != null" > 67 <if test="character != null" >
67 - character, 68 + `character`,
68 </if> 69 </if>
69 <if test="status != null" > 70 <if test="status != null" >
70 status, 71 status,
71 </if> 72 </if>
72 <if test="usage != null" > 73 <if test="usage != null" >
73 - usage, 74 + `usage`,
74 </if> 75 </if>
75 <if test="des != null" > 76 <if test="des != null" >
76 des, 77 des,
@@ -146,13 +147,13 @@ @@ -146,13 +147,13 @@
146 msg_limit = #{msgLimit,jdbcType=TINYINT}, 147 msg_limit = #{msgLimit,jdbcType=TINYINT},
147 </if> 148 </if>
148 <if test="character != null" > 149 <if test="character != null" >
149 - character = #{character,jdbcType=VARCHAR}, 150 + `character` = #{character,jdbcType=VARCHAR},
150 </if> 151 </if>
151 <if test="status != null" > 152 <if test="status != null" >
152 status = #{status,jdbcType=BIT}, 153 status = #{status,jdbcType=BIT},
153 </if> 154 </if>
154 <if test="usage != null" > 155 <if test="usage != null" >
155 - usage = #{usage,jdbcType=TINYINT}, 156 + `usage` = #{usage,jdbcType=TINYINT},
156 </if> 157 </if>
157 <if test="des != null" > 158 <if test="des != null" >
158 des = #{des,jdbcType=VARCHAR}, 159 des = #{des,jdbcType=VARCHAR},
@@ -176,9 +177,9 @@ @@ -176,9 +177,9 @@
176 stype = #{stype,jdbcType=VARCHAR}, 177 stype = #{stype,jdbcType=VARCHAR},
177 optype = #{optype,jdbcType=VARCHAR}, 178 optype = #{optype,jdbcType=VARCHAR},
178 msg_limit = #{msgLimit,jdbcType=TINYINT}, 179 msg_limit = #{msgLimit,jdbcType=TINYINT},
179 - character = #{character,jdbcType=VARCHAR}, 180 + `character` = #{character,jdbcType=VARCHAR},
180 status = #{status,jdbcType=BIT}, 181 status = #{status,jdbcType=BIT},
181 - usage = #{usage,jdbcType=TINYINT}, 182 + `usage` = #{usage,jdbcType=TINYINT},
182 des = #{des,jdbcType=VARCHAR}, 183 des = #{des,jdbcType=VARCHAR},
183 ver = #{ver,jdbcType=VARCHAR}, 184 ver = #{ver,jdbcType=VARCHAR},
184 creat_time = #{creatTime,jdbcType=TIMESTAMP}, 185 creat_time = #{creatTime,jdbcType=TIMESTAMP},
@@ -17,6 +17,12 @@ @@ -17,6 +17,12 @@
17 from message_router_reciver 17 from message_router_reciver
18 where id = #{id,jdbcType=VARCHAR} 18 where id = #{id,jdbcType=VARCHAR}
19 </select> 19 </select>
  20 + <select id="selectByRouterKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
  21 + select
  22 + <include refid="Base_Column_List" />
  23 + from message_router_reciver
  24 + where message_router_id = #{messageRouterId,jdbcType=VARCHAR}
  25 + </select>
20 <delete id="deleteByPrimaryKey" parameterType="java.lang.String" > 26 <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
21 delete from message_router_reciver 27 delete from message_router_reciver
22 where id = #{id,jdbcType=VARCHAR} 28 where id = #{id,jdbcType=VARCHAR}