作者 王勇

增加ElasticSearch操作messagenote

正在显示 32 个修改的文件 包含 696 行增加72 行删除
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 <parent> 5 <parent>
6 <groupId>org.springframework.boot</groupId> 6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId> 7 <artifactId>spring-boot-starter-parent</artifactId>
8 - <version>2.2.1.RELEASE</version> 8 + <version>2.2.5.RELEASE</version>
9 <relativePath/> 9 <relativePath/>
10 </parent> 10 </parent>
11 <groupId>com.sunyo.wlpt.message.bus.service</groupId> 11 <groupId>com.sunyo.wlpt.message.bus.service</groupId>
@@ -19,7 +19,8 @@ @@ -19,7 +19,8 @@
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21 <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> 21 <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
22 -<!-- <elasticsearch.version>6.8.0</elasticsearch.version>--> 22 + <!-- springboot 2.2.1默认的es版本是6.8.4,下面的es的版本要和ES的版本一致 -->
  23 + <elasticsearch.version>7.4.0</elasticsearch.version>
23 </properties> 24 </properties>
24 25
25 <dependencies> 26 <dependencies>
@@ -46,7 +47,6 @@ @@ -46,7 +47,6 @@
46 <groupId>org.springframework.boot</groupId> 47 <groupId>org.springframework.boot</groupId>
47 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 48 <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
48 </dependency> 49 </dependency>
49 -  
50 <!-- SpringBoot end --> 50 <!-- SpringBoot end -->
51 <!-- SpringCloud start --> 51 <!-- SpringCloud start -->
52 <dependency> 52 <dependency>
@@ -168,12 +168,12 @@ @@ -168,12 +168,12 @@
168 <dependencyManagement> 168 <dependencyManagement>
169 <dependencies> 169 <dependencies>
170 <!-- <dependency>--> 170 <!-- <dependency>-->
171 -<!-- <groupId>org.springframework.boot</groupId>-->  
172 -<!-- <artifactId>spring-boot-dependencies</artifactId>-->  
173 -<!-- <version>${spring-boot.version}</version>-->  
174 -<!-- <type>pom</type>-->  
175 -<!-- <scope>import</scope>-->  
176 -<!-- </dependency>--> 171 + <!-- <groupId>org.springframework.boot</groupId>-->
  172 + <!-- <artifactId>spring-boot-dependencies</artifactId>-->
  173 + <!-- <version>${spring-boot.version}</version>-->
  174 + <!-- <type>pom</type>-->
  175 + <!-- <scope>import</scope>-->
  176 + <!-- </dependency>-->
177 <dependency> 177 <dependency>
178 <groupId>org.springframework.cloud</groupId> 178 <groupId>org.springframework.cloud</groupId>
179 <artifactId>spring-cloud-dependencies</artifactId> 179 <artifactId>spring-cloud-dependencies</artifactId>
  1 +package com.sunyo.wlpt.message.bus.service.config;
  2 +
  3 +import org.apache.http.HttpHost;
  4 +import org.elasticsearch.client.RestClient;
  5 +import org.elasticsearch.client.RestHighLevelClient;
  6 +import org.springframework.context.annotation.Bean;
  7 +import org.springframework.context.annotation.Configuration;
  8 +import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
  9 +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  10 +
  11 +/**
  12 + * @author 子诚
  13 + * Description:ES的配置文件
  14 + * 时间:2020/8/5 10:23
  15 + */
  16 +@Configuration
  17 +public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
  18 + @Override
  19 + @Bean
  20 + public RestHighLevelClient elasticsearchClient()
  21 + {
  22 + RestHighLevelClient client = new RestHighLevelClient(
  23 + RestClient.builder(
  24 + // 天生契合集群,有几个es环境,就 new HttpHost 几个,用,相隔
  25 + new HttpHost("192.168.37.139", 9200, "http")
  26 + )
  27 + );
  28 + return client;
  29 + }
  30 +
  31 + @Bean
  32 + public ElasticsearchRestTemplate elasticsearchRestTemplate() {
  33 + return new ElasticsearchRestTemplate(elasticsearchClient());
  34 + }
  35 +}
1 package com.sunyo.wlpt.message.bus.service.controller; 1 package com.sunyo.wlpt.message.bus.service.controller;
2 2
3 -import com.github.pagehelper.PageInfo;  
4 import com.sunyo.wlpt.message.bus.service.domain.*; 3 import com.sunyo.wlpt.message.bus.service.domain.*;
5 import com.sunyo.wlpt.message.bus.service.response.ResultJson; 4 import com.sunyo.wlpt.message.bus.service.response.ResultJson;
6 import com.sunyo.wlpt.message.bus.service.service.*; 5 import com.sunyo.wlpt.message.bus.service.service.*;
  6 +import com.sunyo.wlpt.message.bus.service.service.impl.ElasticsearchService;
7 import org.springframework.format.annotation.DateTimeFormat; 7 import org.springframework.format.annotation.DateTimeFormat;
8 import org.springframework.scheduling.annotation.Scheduled; 8 import org.springframework.scheduling.annotation.Scheduled;
9 import org.springframework.web.bind.annotation.*; 9 import org.springframework.web.bind.annotation.*;
10 10
11 import javax.annotation.Resource; 11 import javax.annotation.Resource;
12 import javax.validation.constraints.NotNull; 12 import javax.validation.constraints.NotNull;
  13 +import java.io.IOException;
13 import java.util.Date; 14 import java.util.Date;
14 15
15 /** 16 /**
@@ -43,6 +44,9 @@ public class MessageNoteController { @@ -43,6 +44,9 @@ public class MessageNoteController {
43 @Resource 44 @Resource
44 private SchedulingDeleteService schedulingDeleteService; 45 private SchedulingDeleteService schedulingDeleteService;
45 46
  47 + @Resource
  48 + private ElasticsearchService elasticsearchService;
  49 +
46 /** 50 /**
47 * 分页查询,消息收发记录 51 * 分页查询,消息收发记录
48 * 52 *
@@ -50,28 +54,26 @@ public class MessageNoteController { @@ -50,28 +54,26 @@ public class MessageNoteController {
50 * @param serverName MQ服务器名称 54 * @param serverName MQ服务器名称
51 * @param virtualHostName 虚拟主机名称 55 * @param virtualHostName 虚拟主机名称
52 * @param exchangeName 交换机名称 56 * @param exchangeName 交换机名称
53 - * @param queueName 队列名称  
54 * @param routingKeyName 路由键名称 57 * @param routingKeyName 路由键名称
55 - * @param sendTime 发送消息时间  
56 - * @param receiveTime 接收消息时间 58 + * @param sendTimeBegin 查询时间段,开始
  59 + * @param sendTimeEnd 查询时间段,结束
57 * @param pageNum 当前页数,默认 1 60 * @param pageNum 当前页数,默认 1
58 * @param pageSize 每页条数,默认 10 61 * @param pageSize 每页条数,默认 10
59 * @return 消息收发记录-列表 62 * @return 消息收发记录-列表
60 */ 63 */
61 @GetMapping("/list") 64 @GetMapping("/list")
62 - public ResultJson selectMessageNoteList( 65 + public ResultJson selectMessageNoteListFromElasticSearch(
63 @RequestParam(value = "username", required = false) String username, 66 @RequestParam(value = "username", required = false) String username,
64 @RequestParam(value = "serverName", required = false) String serverName, 67 @RequestParam(value = "serverName", required = false) String serverName,
65 @RequestParam(value = "virtualHostName", required = false) String virtualHostName, 68 @RequestParam(value = "virtualHostName", required = false) String virtualHostName,
66 @RequestParam(value = "exchangeName", required = false) String exchangeName, 69 @RequestParam(value = "exchangeName", required = false) String exchangeName,
67 - @RequestParam(value = "queueName", required = false) String queueName,  
68 @RequestParam(value = "routingKeyName", required = false) String routingKeyName, 70 @RequestParam(value = "routingKeyName", required = false) String routingKeyName,
69 @DateTimeFormat(pattern = "yyyy-MM-dd") 71 @DateTimeFormat(pattern = "yyyy-MM-dd")
70 - @RequestParam(value = "sendTime", required = false) Date sendTime, 72 + @RequestParam(value = "sendTimeBegin", required = false) Date sendTimeBegin,
71 @DateTimeFormat(pattern = "yyyy-MM-dd") 73 @DateTimeFormat(pattern = "yyyy-MM-dd")
72 - @RequestParam(value = "receiveTime", required = false) Date receiveTime, 74 + @RequestParam(value = "sendTimeEnd", required = false) Date sendTimeEnd,
73 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, 75 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
74 - @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) 76 + @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws IOException
75 { 77 {
76 // 获取查询参数 78 // 获取查询参数
77 MessageNote messageNote = MessageNote.builder() 79 MessageNote messageNote = MessageNote.builder()
@@ -79,16 +81,11 @@ public class MessageNoteController { @@ -79,16 +81,11 @@ public class MessageNoteController {
79 .serverName(serverName) 81 .serverName(serverName)
80 .virtualHostName(virtualHostName) 82 .virtualHostName(virtualHostName)
81 .exchangeName(exchangeName) 83 .exchangeName(exchangeName)
82 - .queueName(queueName)  
83 .routingKeyName(routingKeyName) 84 .routingKeyName(routingKeyName)
84 - .sendTime(sendTime)  
85 - .receiveTime(receiveTime) 85 + .sendTimeBegin(sendTimeBegin)
  86 + .sendTimeEnd(sendTimeEnd)
86 .build(); 87 .build();
87 - // 分页查询  
88 - PageInfo pageInfo = messageNoteService.selectMessageNoteList(messageNote, pageNum, pageSize);  
89 - return pageInfo.getTotal() > 0  
90 - ? new ResultJson<>("200", "查询MQ服务器列表,成功!", pageInfo)  
91 - : new ResultJson<>("500", "查询MQ服务器列表,失败!"); 88 + return elasticsearchService.selectMessageNoteList(messageNote, pageNum, pageSize);
92 } 89 }
93 90
94 /** 91 /**
@@ -100,9 +97,15 @@ public class MessageNoteController { @@ -100,9 +97,15 @@ public class MessageNoteController {
100 @DeleteMapping("/delete") 97 @DeleteMapping("/delete")
101 public ResultJson deleteMessageNote(@RequestBody MessageNote messageNote) 98 public ResultJson deleteMessageNote(@RequestBody MessageNote messageNote)
102 { 99 {
103 - return messageNoteService.deleteByPrimaryKey(messageNote.getId()) > 0  
104 - ? new ResultJson<>("200", "删除-消息收发记录,成功")  
105 - : new ResultJson<>("500", "删除-消息收发记录,失败"); 100 + int num = messageNoteService.deleteByPrimaryKey(messageNote.getId());
  101 + if (num > 0) {
  102 + if (num == 2) {
  103 + return new ResultJson<>("200", "删除ES-消息收发记录,成功");
  104 + }
  105 + return new ResultJson<>("200", "删除-消息收发记录,成功");
  106 + } else {
  107 + return new ResultJson<>("500", "删除-消息收发记录,失败");
  108 + }
106 } 109 }
107 110
108 /** 111 /**
@@ -114,9 +117,15 @@ public class MessageNoteController { @@ -114,9 +117,15 @@ public class MessageNoteController {
114 @GetMapping("/batchRemove") 117 @GetMapping("/batchRemove")
115 public ResultJson batchRemoveMessageNote(String ids) 118 public ResultJson batchRemoveMessageNote(String ids)
116 { 119 {
117 - return messageNoteService.deleteByPrimaryKey(ids) > 0  
118 - ? new ResultJson<>("200", "删除-消息收发记录,成功")  
119 - : new ResultJson<>("500", "删除-消息收发记录,失败"); 120 + int num = messageNoteService.deleteByPrimaryKey(ids);
  121 + if (num > 0) {
  122 + if (num == 2) {
  123 + return new ResultJson<>("200", "批量删除ES-消息收发记录,成功");
  124 + }
  125 + return new ResultJson<>("200", "批量删除-消息收发记录,成功");
  126 + } else {
  127 + return new ResultJson<>("500", "批量删除-消息收发记录,失败");
  128 + }
120 } 129 }
121 130
122 /** 131 /**
@@ -104,12 +104,7 @@ public class RabbitController { @@ -104,12 +104,7 @@ public class RabbitController {
104 if (!binding) { 104 if (!binding) {
105 return ResultJson.error(CustomExceptionType.BINDING_ERROR); 105 return ResultJson.error(CustomExceptionType.BINDING_ERROR);
106 } 106 }
107 - // 4、mq发送消息,数据库中保存消息  
108 -// ResultJson result = directUtils.sendMessage(sentData);  
109 -// if (CustomExceptionType.MESSAGE_SUCCESS.getCode().equals(result.getCode())) {  
110 -// // mq发送消息成功之后,将消息存储于数据库  
111 -// messageNoteService.insertMessageSelective(sentData);  
112 -// } 107 + // 4、mq发送消息,数据库中保存消息并保存至ES
113 return sendAndSave(sentData); 108 return sendAndSave(sentData);
114 } 109 }
115 110
1 package com.sunyo.wlpt.message.bus.service.domain; 1 package com.sunyo.wlpt.message.bus.service.domain;
2 2
  3 +import com.fasterxml.jackson.annotation.JsonFormat;
3 import lombok.AllArgsConstructor; 4 import lombok.AllArgsConstructor;
4 import lombok.Builder; 5 import lombok.Builder;
5 import lombok.Data; 6 import lombok.Data;
6 import lombok.NoArgsConstructor; 7 import lombok.NoArgsConstructor;
  8 +import org.springframework.data.annotation.Id;
  9 +import org.springframework.data.elasticsearch.annotations.Document;
  10 +import org.springframework.data.elasticsearch.annotations.Field;
  11 +import org.springframework.data.elasticsearch.annotations.FieldType;
7 12
8 import java.io.Serializable; 13 import java.io.Serializable;
9 import java.util.Date; 14 import java.util.Date;
@@ -18,6 +23,7 @@ import java.util.Date; @@ -18,6 +23,7 @@ import java.util.Date;
18 @Builder 23 @Builder
19 @AllArgsConstructor 24 @AllArgsConstructor
20 @NoArgsConstructor 25 @NoArgsConstructor
  26 +@Document(indexName = "message_note")
21 public class MessageNote implements Serializable { 27 public class MessageNote implements Serializable {
22 28
23 private static final long serialVersionUID = -2119333801860569470L; 29 private static final long serialVersionUID = -2119333801860569470L;
@@ -25,73 +31,104 @@ public class MessageNote implements Serializable { @@ -25,73 +31,104 @@ public class MessageNote implements Serializable {
25 /** 31 /**
26 * 消息收发记录表的ID 32 * 消息收发记录表的ID
27 */ 33 */
  34 + @Id
28 private String id; 35 private String id;
29 36
30 /** 37 /**
31 * 用户的ID 38 * 用户的ID
32 */ 39 */
  40 + @Field(type = FieldType.Text)
33 private String userId; 41 private String userId;
34 42
35 /** 43 /**
36 * 所属用户登陆名称 44 * 所属用户登陆名称
37 */ 45 */
  46 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
38 private String username; 47 private String username;
39 48
40 /** 49 /**
41 * 所属服务器的ID 50 * 所属服务器的ID
42 */ 51 */
  52 + @Field(type = FieldType.Text)
43 private String serverId; 53 private String serverId;
44 54
45 /** 55 /**
46 * 所属服务器名称 56 * 所属服务器名称
47 */ 57 */
  58 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
48 private String serverName; 59 private String serverName;
49 60
50 /** 61 /**
51 * 所属虚拟主机的ID 62 * 所属虚拟主机的ID
52 */ 63 */
  64 + @Field(type = FieldType.Text)
53 private String virtualHostId; 65 private String virtualHostId;
54 66
55 /** 67 /**
56 * 所属虚拟主机名称 68 * 所属虚拟主机名称
57 */ 69 */
  70 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
58 private String virtualHostName; 71 private String virtualHostName;
59 72
60 /** 73 /**
61 * 所属交换机的ID 74 * 所属交换机的ID
62 */ 75 */
  76 + @Field(type = FieldType.Text)
63 private String exchangeId; 77 private String exchangeId;
64 78
65 /** 79 /**
66 * 所属交换机名称 80 * 所属交换机名称
67 */ 81 */
  82 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
68 private String exchangeName; 83 private String exchangeName;
69 84
70 /** 85 /**
71 * 所属队列的ID 86 * 所属队列的ID
72 */ 87 */
  88 + @Field(type = FieldType.Text)
73 private String queueId; 89 private String queueId;
74 90
75 /** 91 /**
76 * 所属队列名称 92 * 所属队列名称
77 */ 93 */
  94 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
78 private String queueName; 95 private String queueName;
79 96
80 /** 97 /**
81 * 所属路由键的ID 98 * 所属路由键的ID
82 */ 99 */
  100 + @Field(type = FieldType.Text)
83 private String routingKeyId; 101 private String routingKeyId;
84 102
85 /** 103 /**
86 * 所属路由键的名称 104 * 所属路由键的名称
87 */ 105 */
  106 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
88 private String routingKeyName; 107 private String routingKeyName;
89 108
90 /** 109 /**
91 * 消息发送时间 110 * 消息发送时间
92 */ 111 */
  112 + @Field(type = FieldType.Date)
  113 + @JsonFormat(timezone = "GMT+8")
93 private Date sendTime; 114 private Date sendTime;
94 115
  116 + @Field(type = FieldType.Text)
  117 + private String alias_sendTime;
  118 +
  119 + /**
  120 + * 查询时间段,开始时间
  121 + */
  122 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  123 + private Date sendTimeBegin;
  124 +
  125 + /**
  126 + * 查询时间段,结束时间
  127 + */
  128 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  129 + private Date sendTimeEnd;
  130 +
  131 +
95 /** 132 /**
96 * 消息获取时间 133 * 消息获取时间
97 */ 134 */
@@ -105,16 +142,20 @@ public class MessageNote implements Serializable { @@ -105,16 +142,20 @@ public class MessageNote implements Serializable {
105 /** 142 /**
106 * 发送消息内容,别名 143 * 发送消息内容,别名
107 */ 144 */
  145 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
108 private String alias_sendContent; 146 private String alias_sendContent;
109 147
110 /** 148 /**
111 * 相关描述 149 * 相关描述
112 */ 150 */
  151 + @Field(type = FieldType.Text, analyzer = "ik_max_word")
113 private String description; 152 private String description;
114 153
115 /** 154 /**
116 * 创建时间 155 * 创建时间
117 */ 156 */
  157 + @Field(type = FieldType.Date)
  158 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
118 private Date gmtCreate; 159 private Date gmtCreate;
119 160
120 /** 161 /**
@@ -134,7 +175,8 @@ public class MessageNote implements Serializable { @@ -134,7 +175,8 @@ public class MessageNote implements Serializable {
134 * @param sendTime 发送消息时间 175 * @param sendTime 发送消息时间
135 * @param receiveTime 接收消息时间 176 * @param receiveTime 接收消息时间
136 */ 177 */
137 - public MessageNote(String username, String serverName, String virtualHostName, String exchangeName, String queueName, String routingKeyName, Date sendTime, Date receiveTime) { 178 + public MessageNote(String username, String serverName, String virtualHostName, String exchangeName, String queueName, String routingKeyName, Date sendTime, Date receiveTime)
  179 + {
138 this.username = username; 180 this.username = username;
139 this.serverName = serverName; 181 this.serverName = serverName;
140 this.virtualHostName = virtualHostName; 182 this.virtualHostName = virtualHostName;
  1 +package com.sunyo.wlpt.message.bus.service.elasticsearch.dao;
  2 +
  3 +import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
  4 +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  5 +
  6 +/**
  7 + * @author 子诚
  8 + * Description:
  9 + * 时间:2020/8/5 14:43
  10 + */
  11 +public interface MessageNoteRepository extends ElasticsearchRepository<MessageNote, String> {
  12 +
  13 +}
@@ -53,6 +53,14 @@ public interface BusExchangeMapper { @@ -53,6 +53,14 @@ public interface BusExchangeMapper {
53 BusExchange selectByPrimaryKey(String id); 53 BusExchange selectByPrimaryKey(String id);
54 54
55 /** 55 /**
  56 + * 查询,根据交换机名称
  57 + *
  58 + * @param exchangeName 交换机名称
  59 + * @return
  60 + */
  61 + BusExchange selectByExchangeName(String exchangeName);
  62 +
  63 + /**
56 * 更新,选择性,根据主键 64 * 更新,选择性,根据主键
57 * 65 *
58 * @param record the updated record 66 * @param record the updated record
@@ -47,6 +47,14 @@ public interface BusServerMapper { @@ -47,6 +47,14 @@ public interface BusServerMapper {
47 BusServer selectByPrimaryKey(String id); 47 BusServer selectByPrimaryKey(String id);
48 48
49 /** 49 /**
  50 + * 查询,根据服务器名称
  51 + *
  52 + * @param serverName 服务器名称
  53 + * @return
  54 + */
  55 + BusServer selectByServerName(String serverName);
  56 +
  57 + /**
50 * 查询服务器列表,选择性 58 * 查询服务器列表,选择性
51 * 59 *
52 * @param busServer 服务器以及参数 60 * @param busServer 服务器以及参数
@@ -47,6 +47,14 @@ public interface RoutingKeyMapper { @@ -47,6 +47,14 @@ public interface RoutingKeyMapper {
47 RoutingKey selectByPrimaryKey(String id); 47 RoutingKey selectByPrimaryKey(String id);
48 48
49 /** 49 /**
  50 + * 查询,根据路由键名称
  51 + *
  52 + * @param routingKeyName 路由键名称
  53 + * @return
  54 + */
  55 + RoutingKey selectByRoutingKeyName(String routingKeyName);
  56 +
  57 + /**
50 * 更新,选择性,根据主键 58 * 更新,选择性,根据主键
51 * 59 *
52 * @param record the updated record 60 * @param record the updated record
@@ -46,6 +46,14 @@ public interface UserInfoMapper { @@ -46,6 +46,14 @@ public interface UserInfoMapper {
46 UserInfo selectByPrimaryKey(String id); 46 UserInfo selectByPrimaryKey(String id);
47 47
48 /** 48 /**
  49 + * 查询,根据用户名称
  50 + *
  51 + * @param username 用户登录名称
  52 + * @return {@link UserInfo}
  53 + */
  54 + UserInfo selectByUsername(String username);
  55 +
  56 + /**
49 * update record selective 57 * update record selective
50 * 58 *
51 * @param record the updated record 59 * @param record the updated record
@@ -54,6 +54,14 @@ public interface VirtualHostMapper { @@ -54,6 +54,14 @@ public interface VirtualHostMapper {
54 VirtualHost selectByPrimaryKey(String id); 54 VirtualHost selectByPrimaryKey(String id);
55 55
56 /** 56 /**
  57 + * 查询,根据虚拟主机名称
  58 + *
  59 + * @param virtualHostName 虚拟主机名称
  60 + * @return
  61 + */
  62 + VirtualHost selectByVirtualHostName(String virtualHostName);
  63 +
  64 + /**
57 * 根据服务器id,查询虚拟主机列表 65 * 根据服务器id,查询虚拟主机列表
58 * 66 *
59 * @param serverId 服务器id 67 * @param serverId 服务器id
@@ -41,6 +41,8 @@ public class ResultJson<T> implements Serializable { @@ -41,6 +41,8 @@ public class ResultJson<T> implements Serializable {
41 */ 41 */
42 private String jwtToken; 42 private String jwtToken;
43 43
  44 + private Integer total;
  45 +
44 /** 46 /**
45 * 无参,构造方法 47 * 无参,构造方法
46 */ 48 */
@@ -74,6 +76,14 @@ public class ResultJson<T> implements Serializable { @@ -74,6 +76,14 @@ public class ResultJson<T> implements Serializable {
74 this.data = data; 76 this.data = data;
75 } 77 }
76 78
  79 + public ResultJson(String code, String msg, T data, Integer total)
  80 + {
  81 + this.code = code;
  82 + this.msg = msg;
  83 + this.data = data;
  84 + this.total = total;
  85 + }
  86 +
77 /** 87 /**
78 * 定义静态、成功方法(重载) 88 * 定义静态、成功方法(重载)
79 * 89 *
@@ -103,10 +113,12 @@ public class ResultJson<T> implements Serializable { @@ -103,10 +113,12 @@ public class ResultJson<T> implements Serializable {
103 { 113 {
104 return new ResultJson<>("200", message, data); 114 return new ResultJson<>("200", message, data);
105 } 115 }
  116 +
106 public static ResultJson success(CustomExceptionType customExceptionType) 117 public static ResultJson success(CustomExceptionType customExceptionType)
107 { 118 {
108 return new ResultJson<>(customExceptionType.getCode(), customExceptionType.getMsg()); 119 return new ResultJson<>(customExceptionType.getCode(), customExceptionType.getMsg());
109 } 120 }
  121 +
110 /** 122 /**
111 * 请求出现异常时的响应数据封装 123 * 请求出现异常时的响应数据封装
112 * 124 *
@@ -48,6 +48,14 @@ public interface BusExchangeService { @@ -48,6 +48,14 @@ public interface BusExchangeService {
48 BusExchange selectByPrimaryKey(String id); 48 BusExchange selectByPrimaryKey(String id);
49 49
50 /** 50 /**
  51 + * 查询,根据交换机名称
  52 + *
  53 + * @param exchangeName 交换机名称
  54 + * @return
  55 + */
  56 + BusExchange selectByExchangeName(String exchangeName);
  57 +
  58 + /**
51 * 更新,选择性,根据主键 59 * 更新,选择性,根据主键
52 * 60 *
53 * @param record the updated record 61 * @param record the updated record
@@ -112,4 +120,5 @@ public interface BusExchangeService { @@ -112,4 +120,5 @@ public interface BusExchangeService {
112 * @return 120 * @return
113 */ 121 */
114 List<BusExchange> selectByVirtualHostId(String virtualHostId); 122 List<BusExchange> selectByVirtualHostId(String virtualHostId);
  123 +
115 } 124 }
@@ -47,6 +47,14 @@ public interface BusServerService { @@ -47,6 +47,14 @@ public interface BusServerService {
47 BusServer selectByPrimaryKey(String id); 47 BusServer selectByPrimaryKey(String id);
48 48
49 /** 49 /**
  50 + * 查询,根据服务器名称
  51 + *
  52 + * @param serverName 服务器名称
  53 + * @return
  54 + */
  55 + BusServer selectByServerName(String serverName);
  56 +
  57 + /**
50 * 更新,选择性,根据主键 58 * 更新,选择性,根据主键
51 * 59 *
52 * @param record the updated record 60 * @param record the updated record
@@ -113,8 +121,9 @@ public interface BusServerService { @@ -113,8 +121,9 @@ public interface BusServerService {
113 121
114 /** 122 /**
115 * 查询服务器名称是否存在 123 * 查询服务器名称是否存在
116 - * @param serverName 服务器名称 124 + *
  125 + * @param serverName 服务器名称
117 * @return true or false 126 * @return true or false
118 */ 127 */
119 - List<BusServer> selectServerExist(String serverName); 128 + List<BusServer> selectServerExist(String serverName);
120 } 129 }
@@ -56,6 +56,14 @@ public interface RoutingKeyService { @@ -56,6 +56,14 @@ public interface RoutingKeyService {
56 RoutingKey selectByPrimaryKey(String id); 56 RoutingKey selectByPrimaryKey(String id);
57 57
58 /** 58 /**
  59 + * 查询,根据路由键名称
  60 + *
  61 + * @param routingKeyName 路由键名称
  62 + * @return
  63 + */
  64 + RoutingKey selectByRoutingKeyName(String routingKeyName);
  65 +
  66 + /**
59 * 根据exchangeID查询路由键 67 * 根据exchangeID查询路由键
60 * 68 *
61 * @param exchangeId 交换机id 69 * @param exchangeId 交换机id
@@ -112,4 +120,5 @@ public interface RoutingKeyService { @@ -112,4 +120,5 @@ public interface RoutingKeyService {
112 * @return List<RoutingKey> 120 * @return List<RoutingKey>
113 */ 121 */
114 List<RoutingKey> selectRoutingKeyExist(RoutingKey routingKey); 122 List<RoutingKey> selectRoutingKeyExist(RoutingKey routingKey);
  123 +
115 } 124 }
@@ -44,6 +44,14 @@ public interface UserInfoService { @@ -44,6 +44,14 @@ public interface UserInfoService {
44 UserInfo selectByPrimaryKey(String id); 44 UserInfo selectByPrimaryKey(String id);
45 45
46 /** 46 /**
  47 + * 查询,根据用户名称
  48 + *
  49 + * @param username 用户登录名称
  50 + * @return {@link UserInfo}
  51 + */
  52 + UserInfo selectByUsername(String username);
  53 +
  54 + /**
47 * 更新,选择性,根据主键 55 * 更新,选择性,根据主键
48 * 56 *
49 * @param record the updated record 57 * @param record the updated record
@@ -73,6 +81,8 @@ public interface UserInfoService { @@ -73,6 +81,8 @@ public interface UserInfoService {
73 * @return List<UserInfo> 81 * @return List<UserInfo>
74 */ 82 */
75 List<UserInfo> selectUserExist(String username); 83 List<UserInfo> selectUserExist(String username);
  84 +
  85 +
76 } 86 }
77 87
78 88
@@ -47,6 +47,14 @@ public interface VirtualHostService { @@ -47,6 +47,14 @@ public interface VirtualHostService {
47 VirtualHost selectByPrimaryKey(String id); 47 VirtualHost selectByPrimaryKey(String id);
48 48
49 /** 49 /**
  50 + * 查询,根据虚拟主机名称
  51 + *
  52 + * @param virtualHostName 虚拟主机名称
  53 + * @return
  54 + */
  55 + VirtualHost selectByVirtualHostName(String virtualHostName);
  56 +
  57 + /**
50 * 更新,选择性,根据主键 58 * 更新,选择性,根据主键
51 * 59 *
52 * @param record the updated record 60 * @param record the updated record
@@ -111,4 +119,6 @@ public interface VirtualHostService { @@ -111,4 +119,6 @@ public interface VirtualHostService {
111 * @return 119 * @return
112 */ 120 */
113 int deleteByServerId(String serverId); 121 int deleteByServerId(String serverId);
  122 +
  123 +
114 } 124 }
@@ -101,6 +101,12 @@ public class BusExchangeServiceImpl implements BusExchangeService { @@ -101,6 +101,12 @@ public class BusExchangeServiceImpl implements BusExchangeService {
101 } 101 }
102 102
103 @Override 103 @Override
  104 + public BusExchange selectByExchangeName(String exchangeName)
  105 + {
  106 + return busExchangeMapper.selectByExchangeName(exchangeName);
  107 + }
  108 +
  109 + @Override
104 public int updateByPrimaryKeySelective(BusExchange record) 110 public int updateByPrimaryKeySelective(BusExchange record)
105 { 111 {
106 return busExchangeMapper.updateByPrimaryKeySelective(record); 112 return busExchangeMapper.updateByPrimaryKeySelective(record);
@@ -112,6 +112,12 @@ public class BusServerServiceImpl implements BusServerService { @@ -112,6 +112,12 @@ public class BusServerServiceImpl implements BusServerService {
112 } 112 }
113 113
114 @Override 114 @Override
  115 + public BusServer selectByServerName(String serverName)
  116 + {
  117 + return busServerMapper.selectByServerName(serverName);
  118 + }
  119 +
  120 + @Override
115 public int updateByPrimaryKeySelective(BusServer record) 121 public int updateByPrimaryKeySelective(BusServer record)
116 { 122 {
117 return busServerMapper.updateByPrimaryKeySelective(record); 123 return busServerMapper.updateByPrimaryKeySelective(record);
  1 +package com.sunyo.wlpt.message.bus.service.service.impl;
  2 +
  3 +import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
  4 +import com.sunyo.wlpt.message.bus.service.elasticsearch.dao.MessageNoteRepository;
  5 +import com.sunyo.wlpt.message.bus.service.response.ResultJson;
  6 +import com.sunyo.wlpt.message.bus.service.utils.DateUtils;
  7 +import io.netty.util.internal.StringUtil;
  8 +import org.elasticsearch.action.search.SearchRequest;
  9 +import org.elasticsearch.action.search.SearchResponse;
  10 +import org.elasticsearch.client.RequestOptions;
  11 +import org.elasticsearch.client.RestHighLevelClient;
  12 +import org.elasticsearch.common.unit.TimeValue;
  13 +import org.elasticsearch.index.query.*;
  14 +import org.elasticsearch.search.SearchHit;
  15 +import org.elasticsearch.search.builder.SearchSourceBuilder;
  16 +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  17 +import org.elasticsearch.search.sort.SortOrder;
  18 +import org.springframework.data.domain.Page;
  19 +import org.springframework.data.domain.PageRequest;
  20 +import org.springframework.data.domain.Sort;
  21 +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  22 +import org.springframework.stereotype.Component;
  23 +
  24 +import javax.annotation.Resource;
  25 +import java.io.IOException;
  26 +import java.util.*;
  27 +import java.util.concurrent.TimeUnit;
  28 +
  29 +/**
  30 + * @author 子诚
  31 + * Description:
  32 + * 时间:2020/8/5 14:08
  33 + */
  34 +@Component
  35 +public class ElasticsearchService {
  36 +
  37 + /**
  38 + * 这里不能使用@Resource,必须使用@Autowired
  39 + * 原因:I don`t know
  40 + */
  41 + @Resource
  42 + private ElasticsearchRestTemplate elasticsearchRestTemplate;
  43 +
  44 + @Resource
  45 + private RestHighLevelClient restHighLevelClient;
  46 +
  47 + @Resource
  48 + private MessageNoteRepository messageNoteRepository;
  49 +
  50 +
  51 + public ResultJson selectMessageNoteList(MessageNote messageNote, Integer pageNum, Integer pageSize) throws IOException
  52 + {
  53 + // 条件搜索
  54 + SearchRequest searchRequest = new SearchRequest("message_note");
  55 + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
  56 +
  57 + // 高亮设置
  58 + HighlightBuilder highlightBuilder = new HighlightBuilder();
  59 +
  60 + // 高亮字段,不设置
  61 + highlightBuilder.field("*").requireFieldMatch(false);
  62 + /*高亮前缀标签
  63 + .preTags("<span style='color=green'>")
  64 + // 高亮后缀标签
  65 + .postTags("</span>");
  66 + */
  67 +
  68 + // 分页
  69 + sourceBuilder
  70 + .from((pageNum - 1) * pageSize)
  71 + .size(pageSize)
  72 + // 检索所有字段
  73 + .postFilter(QueryBuilders.matchAllQuery())
  74 + .sort("sendTime", SortOrder.DESC)
  75 + .highlighter(new HighlightBuilder().
  76 + field("*").
  77 + requireFieldMatch(false));
  78 +
  79 +
  80 + if (StringUtil.isNullOrEmpty(messageNote.getUsername())
  81 + && StringUtil.isNullOrEmpty(messageNote.getServerName())
  82 + && StringUtil.isNullOrEmpty(messageNote.getVirtualHostName())
  83 + && StringUtil.isNullOrEmpty(messageNote.getExchangeName())
  84 + && StringUtil.isNullOrEmpty(messageNote.getRoutingKeyName())
  85 + && messageNote.getSendTimeBegin() == null
  86 + && messageNote.getSendTimeEnd() == null) {
  87 + sourceBuilder.query(QueryBuilders.matchAllQuery());
  88 + } else {
  89 + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  90 + if (!StringUtil.isNullOrEmpty(messageNote.getUsername())) {
  91 + WildcardQueryBuilder usernameQuery = QueryBuilders.wildcardQuery("username", messageNote.getUsername());
  92 + boolQueryBuilder.must(usernameQuery);
  93 + }
  94 + if (!StringUtil.isNullOrEmpty(messageNote.getServerName())) {
  95 + WildcardQueryBuilder serverNameQuery = QueryBuilders.wildcardQuery("serverName", messageNote.getServerName());
  96 + boolQueryBuilder.must(serverNameQuery);
  97 + }
  98 + if (!StringUtil.isNullOrEmpty(messageNote.getVirtualHostName())) {
  99 + WildcardQueryBuilder virtualHostNameQuery = QueryBuilders.wildcardQuery("virtualHostName", messageNote.getVirtualHostName());
  100 + boolQueryBuilder.must(virtualHostNameQuery);
  101 + }
  102 + if (!StringUtil.isNullOrEmpty(messageNote.getExchangeName())) {
  103 + WildcardQueryBuilder exchangeNameQuery = QueryBuilders.wildcardQuery("exchangeName", messageNote.getExchangeName());
  104 + boolQueryBuilder.must(exchangeNameQuery);
  105 + }
  106 + if (!StringUtil.isNullOrEmpty(messageNote.getRoutingKeyName())) {
  107 + WildcardQueryBuilder routingKeyNameQuery = QueryBuilders.wildcardQuery("routingKeyName", messageNote.getRoutingKeyName());
  108 + boolQueryBuilder.must(routingKeyNameQuery);
  109 + }
  110 + if (messageNote.getSendTimeBegin() != null && messageNote.getSendTimeEnd() != null) {
  111 + RangeQueryBuilder sendTimeQuery = QueryBuilders.rangeQuery("sendTime")
  112 + .from(messageNote.getSendTimeBegin(), true)
  113 + .to(DateUtils.addDays(messageNote.getSendTimeEnd(), 1), true);
  114 + boolQueryBuilder.must(sendTimeQuery);
  115 + }
  116 + if (messageNote.getSendTimeBegin() != null && messageNote.getSendTimeEnd() == null) {
  117 + RangeQueryBuilder sendTimeQuery = QueryBuilders.rangeQuery("sendTime")
  118 + .gte(messageNote.getSendTimeBegin());
  119 + boolQueryBuilder.must(sendTimeQuery);
  120 + }
  121 + if (messageNote.getSendTimeBegin() == null && messageNote.getSendTimeEnd() != null) {
  122 + RangeQueryBuilder sendTimeQuery = QueryBuilders.rangeQuery("sendTime")
  123 + .lte(DateUtils.addDays(messageNote.getSendTimeEnd(), 1));
  124 + boolQueryBuilder.must(sendTimeQuery);
  125 + }
  126 +
  127 + sourceBuilder.query(boolQueryBuilder);
  128 + }
  129 + sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
  130 +
  131 +
  132 + // 执行搜索
  133 + searchRequest.source(sourceBuilder);
  134 + SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  135 +
  136 + // 解析结果
  137 + List<Map<String, Object>> list = new ArrayList<>();
  138 + for (SearchHit hit : searchResponse.getHits().getHits()) {
  139 + // 原来的结果
  140 + Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  141 + sourceAsMap.get("sendTime");
  142 + System.out.println(sourceAsMap.get("sendTime"));
  143 +
  144 + /* 解析高亮的字段
  145 + Map<String, HighlightField> highlightFields = hit.getHighlightFields();
  146 + HighlightField username = highlightFields.get("username");
  147 + if (username != null) {
  148 + // 取出该字段
  149 + Text[] fragments = username.fragments();
  150 + // 将高亮字段替换原来没有高亮的字段
  151 + String user_name = "";
  152 + for (Text text : fragments) {
  153 + user_name += text; }
  154 + // 将高亮字段替换原来的内容
  155 + sourceAsMap.put("username", user_name);
  156 + }
  157 + */
  158 + list.add(sourceAsMap);
  159 + }
  160 + Integer total = Math.toIntExact(searchResponse.getHits().getTotalHits().value);
  161 + return total > 0
  162 + ? new ResultJson<>("200", "ES分页查询成功", list, total)
  163 + : new ResultJson<>("500", "ES分页查询失败");
  164 + }
  165 +
  166 +
  167 + /**
  168 + * 保存或者更新一条文档,于ES服务器中
  169 + *
  170 + * @param messageNote {@link MessageNote}
  171 + */
  172 + public void saveOrUpdateMessageNote(MessageNote messageNote)
  173 + {
  174 + // id,存在即是更新;id不存在即是保存
  175 + messageNoteRepository.save(messageNote);
  176 + }
  177 +
  178 + /**
  179 + * 根据Id,删除ES中的一条文档
  180 + *
  181 + * @param id 消息记录id
  182 + */
  183 + public void deleteMessageNoteById(String id)
  184 + {
  185 + messageNoteRepository.deleteById(id);
  186 + }
  187 +
  188 + /**
  189 + * 清空所有文档
  190 + */
  191 + public void deleteAllMessageNote()
  192 + {
  193 + messageNoteRepository.deleteAll();
  194 + }
  195 +
  196 + /**
  197 + * 根据id查询
  198 + */
  199 + public void selectById(String id)
  200 + {
  201 + Optional<MessageNote> optionalMessageNote = messageNoteRepository.findById(id);
  202 + MessageNote messageNote = optionalMessageNote.get();
  203 + }
  204 +
  205 + /**
  206 + * 查询所有
  207 + */
  208 + public List<MessageNote> selectAll()
  209 + {
  210 + Iterable<MessageNote> gets = messageNoteRepository.findAll(Sort.by(Sort.Order.desc("gmtCreate")));
  211 + List<MessageNote> list = new ArrayList<>();
  212 + for (MessageNote get : gets) {
  213 + list.add(get);
  214 + }
  215 + return list;
  216 + }
  217 +
  218 + /**
  219 + * 分页查询
  220 + *
  221 + * @param pageNum 当前页-1
  222 + * @param pageSize 每页数量
  223 + * @return
  224 + */
  225 + public List<MessageNote> selectByPage(Integer pageNum, Integer pageSize)
  226 + {
  227 + List<MessageNote> list = new ArrayList<>();
  228 + Page<MessageNote> pages = messageNoteRepository.search(QueryBuilders.matchAllQuery(), PageRequest.of(pageNum - 1,
  229 + pageSize));
  230 + for (MessageNote note : pages) {
  231 + list.add(note);
  232 + }
  233 + return list;
  234 + }
  235 +
  236 +
  237 +}
@@ -7,6 +7,7 @@ import com.sunyo.wlpt.message.bus.service.mapper.MessageNoteMapper; @@ -7,6 +7,7 @@ import com.sunyo.wlpt.message.bus.service.mapper.MessageNoteMapper;
7 import com.sunyo.wlpt.message.bus.service.mapper.UserMessageBindingMapper; 7 import com.sunyo.wlpt.message.bus.service.mapper.UserMessageBindingMapper;
8 import com.sunyo.wlpt.message.bus.service.rabbit.utils.DirectUtils; 8 import com.sunyo.wlpt.message.bus.service.rabbit.utils.DirectUtils;
9 import com.sunyo.wlpt.message.bus.service.service.*; 9 import com.sunyo.wlpt.message.bus.service.service.*;
  10 +import com.sunyo.wlpt.message.bus.service.utils.DateUtils;
10 import com.sunyo.wlpt.message.bus.service.utils.IdUtils; 11 import com.sunyo.wlpt.message.bus.service.utils.IdUtils;
11 import lombok.extern.slf4j.Slf4j; 12 import lombok.extern.slf4j.Slf4j;
12 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
@@ -49,6 +50,9 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -49,6 +50,9 @@ public class MessageNoteServiceImpl implements MessageNoteService {
49 private MessageNoteMapper messageNoteMapper; 50 private MessageNoteMapper messageNoteMapper;
50 51
51 @Resource 52 @Resource
  53 + private ElasticsearchService elasticsearchService;
  54 +
  55 + @Resource
52 private DirectUtils directUtils; 56 private DirectUtils directUtils;
53 57
54 @Override 58 @Override
@@ -56,24 +60,25 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -56,24 +60,25 @@ public class MessageNoteServiceImpl implements MessageNoteService {
56 public int deleteByPrimaryKey(String id) 60 public int deleteByPrimaryKey(String id)
57 { 61 {
58 // 判断删除的个数,需被删除的个数是否一致 62 // 判断删除的个数,需被删除的个数是否一致
59 - int index = 0;  
60 String splitItem = ","; 63 String splitItem = ",";
61 //如果id,传过来多个,以','分割,即批量删除 64 //如果id,传过来多个,以','分割,即批量删除
62 if (id.contains(splitItem)) { 65 if (id.contains(splitItem)) {
63 String[] split = id.split(splitItem); 66 String[] split = id.split(splitItem);
64 for (int i = 0; i < split.length; i++) { 67 for (int i = 0; i < split.length; i++) {
65 - int num = messageNoteMapper.deleteByPrimaryKey(split[i]);  
66 - if (num > 0) {  
67 - index = index + num; 68 + MessageNote messageNote = messageNoteMapper.selectByPrimaryKey(split[i]);
  69 + if (messageNote != null) {
  70 + messageNoteMapper.deleteByPrimaryKey(split[i]);
68 } 71 }
  72 + elasticsearchService.deleteMessageNoteById(split[i]);
69 } 73 }
70 - if (index == split.length) {  
71 - return 1;  
72 - } else {  
73 - return 0;  
74 - } 74 + return 2;
75 } else { 75 } else {
76 - return messageNoteMapper.deleteByPrimaryKey(id); 76 + MessageNote messageNote = messageNoteMapper.selectByPrimaryKey(id);
  77 + if (messageNote != null) {
  78 + messageNoteMapper.deleteByPrimaryKey(id);
  79 + }
  80 + elasticsearchService.deleteMessageNoteById(id);
  81 + return 2;
77 } 82 }
78 } 83 }
79 84
@@ -116,6 +121,7 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -116,6 +121,7 @@ public class MessageNoteServiceImpl implements MessageNoteService {
116 messageNoteList.parallelStream().forEach(item -> { 121 messageNoteList.parallelStream().forEach(item -> {
117 String content = new String(item.getSendContent()); 122 String content = new String(item.getSendContent());
118 item.setAlias_sendContent(content + ""); 123 item.setAlias_sendContent(content + "");
  124 + item.setAlias_sendTime(DateUtils.formatLong(item.getSendTime()));
119 }); 125 });
120 PageInfo<MessageNote> pageInfo = new PageInfo<>(messageNoteList); 126 PageInfo<MessageNote> pageInfo = new PageInfo<>(messageNoteList);
121 return pageInfo; 127 return pageInfo;
@@ -146,15 +152,46 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -146,15 +152,46 @@ public class MessageNoteServiceImpl implements MessageNoteService {
146 .routingKeyName(xmlData.getRoutingKeyName()) 152 .routingKeyName(xmlData.getRoutingKeyName())
147 // 发送时间 153 // 发送时间
148 .sendTime(xmlData.getSendDateTime()) 154 .sendTime(xmlData.getSendDateTime())
  155 + .alias_sendTime(DateUtils.formatLong(xmlData.getSendDateTime()))
  156 + // 消息内容,别名
  157 + .alias_sendContent(xmlData.getSendContent())
149 // 消息内容 158 // 消息内容
150 .sendContent(xmlData.getSendContent().getBytes()) 159 .sendContent(xmlData.getSendContent().getBytes())
151 // 描述:序列+token,or 自我描述 160 // 描述:序列+token,or 自我描述
152 .description(description) 161 .description(description)
153 .build(); 162 .build();
154 - int num = messageNoteMapper.insertSelective(messageNote); 163 + MessageNote note = note_fillId(messageNote);
  164 + int num = messageNoteMapper.insertSelective(note);
  165 + // ES没有事务,故先执行SQL
  166 + insertMessageToES(note);
155 return num; 167 return num;
156 } 168 }
157 169
  170 + /**
  171 + * 客户端发来的消息,填充id
  172 + *
  173 + * @param messageNote {@link MessageNote}
  174 + * @return
  175 + */
  176 + public MessageNote note_fillId(MessageNote messageNote)
  177 + {
  178 + UserInfo userInfo = userInfoService.selectByUsername(messageNote.getUsername());
  179 + messageNote.setUserId(userInfo.getId());
  180 +
  181 + BusServer busServer = busServerService.selectByServerName(messageNote.getServerName());
  182 + messageNote.setServerId(busServer.getId());
  183 +
  184 + VirtualHost virtualHost = virtualHostService.selectByVirtualHostName(messageNote.getVirtualHostName());
  185 + messageNote.setVirtualHostId(virtualHost.getId());
  186 +
  187 + BusExchange busExchange = busExchangeService.selectByExchangeName(messageNote.getExchangeName());
  188 + messageNote.setExchangeId(busExchange.getId());
  189 +
  190 + RoutingKey routingKey = routingKeyService.selectByRoutingKeyName(messageNote.getRoutingKeyName());
  191 + messageNote.setRoutingKeyId(routingKey.getId());
  192 +
  193 + return messageNote;
  194 + }
158 195
159 /** 196 /**
160 * 填充名称(使用get方法,如果不存在就会报空指针异常) 197 * 填充名称(使用get方法,如果不存在就会报空指针异常)
@@ -167,16 +204,10 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -167,16 +204,10 @@ public class MessageNoteServiceImpl implements MessageNoteService {
167 // 设置id 204 // 设置id
168 messageNote.setId(IdUtils.generateId()); 205 messageNote.setId(IdUtils.generateId());
169 206
170 - // 填充,用户名称  
171 -// UserInfo userInfo = userInfoService.selectByPrimaryKey(messageNote.getUserId());  
172 -// messageNote.setUsername(userInfo.getUsername());  
173 -  
174 // 填充,发送内容(编辑 or 新增) 207 // 填充,发送内容(编辑 or 新增)
175 messageNote.setSendContent(messageNote.getAlias_sendContent().getBytes()); 208 messageNote.setSendContent(messageNote.getAlias_sendContent().getBytes());
176 209
177 - // 填充,服务器名称  
178 -// BusServer busServer = busServerService.selectByPrimaryKey(messageNote.getServerId());  
179 -// messageNote.setServerName(busServer.getServerName()); 210 + messageNote.setAlias_sendTime(DateUtils.formatLong(messageNote.getSendTime()));
180 211
181 // 填充,虚拟主机名称 212 // 填充,虚拟主机名称
182 VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(messageNote.getVirtualHostId()); 213 VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(messageNote.getVirtualHostId());
@@ -234,11 +265,25 @@ public class MessageNoteServiceImpl implements MessageNoteService { @@ -234,11 +265,25 @@ public class MessageNoteServiceImpl implements MessageNoteService {
234 .serverPort(busServer.getServerPort()) 265 .serverPort(busServer.getServerPort())
235 .build(); 266 .build();
236 directUtils.sendMessage(xmlData); 267 directUtils.sendMessage(xmlData);
237 - return messageNoteMapper.insertSelective(note); 268 + int num = messageNoteMapper.insertSelective(note);
  269 + // ES没有事务,故先执行SQL
  270 + insertMessageToES(note);
  271 + return num;
238 } else { 272 } else {
239 return 0; 273 return 0;
240 } 274 }
241 } 275 }
  276 +
  277 +
  278 + /**
  279 + * 保存消息至ES服务器
  280 + *
  281 + * @param note 消息
  282 + */
  283 + public void insertMessageToES(MessageNote note)
  284 + {
  285 + elasticsearchService.saveOrUpdateMessageNote(note);
  286 + }
242 } 287 }
243 288
244 289
@@ -88,6 +88,12 @@ public class RoutingKeyServiceImpl implements RoutingKeyService { @@ -88,6 +88,12 @@ public class RoutingKeyServiceImpl implements RoutingKeyService {
88 } 88 }
89 89
90 @Override 90 @Override
  91 + public RoutingKey selectByRoutingKeyName(String routingKeyName)
  92 + {
  93 + return routingKeyMapper.selectByRoutingKeyName(routingKeyName);
  94 + }
  95 +
  96 + @Override
91 public List<RoutingKey> selectByExchangeId(String exchangeId) 97 public List<RoutingKey> selectByExchangeId(String exchangeId)
92 { 98 {
93 return routingKeyMapper.selectByExchangeId(exchangeId); 99 return routingKeyMapper.selectByExchangeId(exchangeId);
@@ -44,6 +44,12 @@ public class UserInfoServiceImpl implements UserInfoService { @@ -44,6 +44,12 @@ public class UserInfoServiceImpl implements UserInfoService {
44 } 44 }
45 45
46 @Override 46 @Override
  47 + public UserInfo selectByUsername(String username)
  48 + {
  49 + return userInfoMapper.selectByUsername(username);
  50 + }
  51 +
  52 + @Override
47 public int updateByPrimaryKeySelective(UserInfo record) 53 public int updateByPrimaryKeySelective(UserInfo record)
48 { 54 {
49 return userInfoMapper.updateByPrimaryKeySelective(record); 55 return userInfoMapper.updateByPrimaryKeySelective(record);
@@ -96,6 +96,12 @@ public class VirtualHostServiceImpl implements VirtualHostService { @@ -96,6 +96,12 @@ public class VirtualHostServiceImpl implements VirtualHostService {
96 } 96 }
97 97
98 @Override 98 @Override
  99 + public VirtualHost selectByVirtualHostName(String virtualHostName)
  100 + {
  101 + return virtualHostMapper.selectByVirtualHostName(virtualHostName);
  102 + }
  103 +
  104 + @Override
99 public int updateByPrimaryKeySelective(VirtualHost record) 105 public int updateByPrimaryKeySelective(VirtualHost record)
100 { 106 {
101 return virtualHostMapper.updateByPrimaryKeySelective(record); 107 return virtualHostMapper.updateByPrimaryKeySelective(record);
  1 +package com.sunyo.wlpt.message.bus.service.utils;
  2 +
  3 +import java.text.SimpleDateFormat;
  4 +import java.util.Calendar;
  5 +import java.util.Date;
  6 +
  7 +/**
  8 + * @author 子诚
  9 + * Description:时间字符串
  10 + * 时间:2020/8/6 18:57
  11 + */
  12 +public class DateUtils {
  13 + /**
  14 + * 时间戳转换成字符串
  15 + */
  16 + public static String getDateToLong(long time)
  17 + {
  18 + Date d = new Date(time);
  19 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20 + return sf.format(d);
  21 + }
  22 +
  23 + /**
  24 + * 时间戳转换成字符串
  25 + */
  26 + public static String getDateToShort(long time)
  27 + {
  28 + Date d = new Date(time);
  29 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  30 + return sf.format(d);
  31 + }
  32 +
  33 + /**
  34 + * 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd"
  35 + *
  36 + * @param time Date类型时间
  37 + * @return
  38 + */
  39 + public static String isNullShort(Date time)
  40 + {
  41 + if (null == time) {
  42 + return "";
  43 + } else {
  44 + return getDateToShort(time.getTime());
  45 + }
  46 + }
  47 +
  48 + /**
  49 + * 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd HH:mm:ss"
  50 + *
  51 + * @param time Date类型时间
  52 + * @return
  53 + */
  54 + public static String isNullLong(Date time)
  55 + {
  56 + if (null == time) {
  57 + return "";
  58 + } else {
  59 + return getDateToLong(time.getTime());
  60 + }
  61 + }
  62 +
  63 + /**
  64 + * 将 Date类型转成 yyyy-MM-dd HH:mm:ss
  65 + *
  66 + * @param date 时间
  67 + * @return 字符串
  68 + */
  69 + public static String formatLong(Date date)
  70 + {
  71 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  72 + return sf.format(date);
  73 + }
  74 +
  75 + /**
  76 + * 增加天数
  77 + *
  78 + * @param date 时间
  79 + * @param days 增加的天数
  80 + * @return 增加天数之后的时间
  81 + */
  82 + public static Date addDays(Date date, int days)
  83 + {
  84 + Calendar calendar = Calendar.getInstance();
  85 + calendar.setTime(date);
  86 + calendar.add(Calendar.DATE, days);
  87 + return calendar.getTime();
  88 + }
  89 +}
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusExchangeMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusExchangeMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/> 4 +
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusExchange"> 5 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusExchange">
7 <!--@mbg.generated--> 6 <!--@mbg.generated-->
8 <!--@Table bus_exchange--> 7 <!--@Table bus_exchange-->
@@ -43,6 +42,14 @@ @@ -43,6 +42,14 @@
43 where id = #{id,jdbcType=VARCHAR} 42 where id = #{id,jdbcType=VARCHAR}
44 </select> 43 </select>
45 44
  45 + <select id="selectByExchangeName" parameterType="java.lang.String" resultMap="BaseResultMap">
  46 + <!--@mbg.generated-->
  47 + select
  48 + <include refid="Base_Column_List"/>
  49 + from bus_exchange
  50 + where exchange_name = #{exchangeName,jdbcType=VARCHAR}
  51 + </select>
  52 +
46 <!-- 获取交换机列表,根据虚拟主机id --> 53 <!-- 获取交换机列表,根据虚拟主机id -->
47 <select id="selectByVirtualHostId" parameterType="java.lang.String" resultMap="BaseResultMap"> 54 <select id="selectByVirtualHostId" parameterType="java.lang.String" resultMap="BaseResultMap">
48 <!--@mbg.generated--> 55 <!--@mbg.generated-->
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusQueueMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/>  
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusQueue"> 4 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusQueue">
7 <!--@mbg.generated--> 5 <!--@mbg.generated-->
8 <!--@Table bus_queue--> 6 <!--@Table bus_queue-->
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/>  
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusServer"> 4 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.BusServer">
7 <!--@mbg.generated--> 5 <!--@mbg.generated-->
8 <!--@Table bus_server--> 6 <!--@Table bus_server-->
@@ -53,6 +51,14 @@ @@ -53,6 +51,14 @@
53 where id = #{id,jdbcType=VARCHAR} 51 where id = #{id,jdbcType=VARCHAR}
54 </select> 52 </select>
55 53
  54 + <select id="selectByServerName" parameterType="java.lang.String" resultMap="BaseResultMap">
  55 + <!--@mbg.generated-->
  56 + select
  57 + <include refid="Base_Column_List"/>
  58 + from bus_server
  59 + where server_name = #{serverName,jdbcType=VARCHAR}
  60 + </select>
  61 +
56 <!-- 获取服务器列表,可能要级联或者懒加载 --> 62 <!-- 获取服务器列表,可能要级联或者懒加载 -->
57 <select id="getServerList" resultMap="BaseResultMap"> 63 <select id="getServerList" resultMap="BaseResultMap">
58 select 64 select
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.RoutingKeyMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.RoutingKeyMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/>  
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.RoutingKey"> 4 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.RoutingKey">
7 <!--@mbg.generated--> 5 <!--@mbg.generated-->
8 <!--@Table routing_key--> 6 <!--@Table routing_key-->
@@ -36,6 +34,15 @@ @@ -36,6 +34,15 @@
36 where id = #{id,jdbcType=VARCHAR} 34 where id = #{id,jdbcType=VARCHAR}
37 </select> 35 </select>
38 36
  37 + <select id="selectByRoutingKeyName" parameterType="java.lang.String" resultMap="BaseResultMap">
  38 + <!--@mbg.generated-->
  39 + select
  40 + <include refid="Base_Column_List"/>
  41 + from routing_key
  42 + where routing_key_name = #{routingKeyName,jdbcType=VARCHAR}
  43 + </select>
  44 +
  45 +
39 <!-- 查询路由键列表 --> 46 <!-- 查询路由键列表 -->
40 <select id="selectRoutingKeyList" parameterType="com.sunyo.wlpt.message.bus.service.domain.RoutingKey" 47 <select id="selectRoutingKeyList" parameterType="com.sunyo.wlpt.message.bus.service.domain.RoutingKey"
41 resultMap="RoutingKeyAndExchangeMap"> 48 resultMap="RoutingKeyAndExchangeMap">
@@ -23,6 +23,14 @@ @@ -23,6 +23,14 @@
23 where id = #{id,jdbcType=VARCHAR} 23 where id = #{id,jdbcType=VARCHAR}
24 </select> 24 </select>
25 25
  26 + <select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
  27 + <!--@mbg.generated-->
  28 + select
  29 + <include refid="Base_Column_List"/>
  30 + from user_info
  31 + where username = #{username,jdbcType=VARCHAR}
  32 + </select>
  33 +
26 <select id="getUserInfoList" resultMap="BaseResultMap"> 34 <select id="getUserInfoList" resultMap="BaseResultMap">
27 <!--@mbg.generated--> 35 <!--@mbg.generated-->
28 select id, 36 select id,
1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
2 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.UserMessageBindingMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.UserMessageBindingMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/>  
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding"> 4 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding">
7 <!--@mbg.generated--><!--@Table user_message_binding--> 5 <!--@mbg.generated--><!--@Table user_message_binding-->
8 <id column="id" jdbcType="VARCHAR" property="id"/> 6 <id column="id" jdbcType="VARCHAR" property="id"/>
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.VirtualHostMapper"> 3 <mapper namespace="com.sunyo.wlpt.message.bus.service.mapper.VirtualHostMapper">
4 - <!-- 使用Redis做mybatis的二级缓存 -->  
5 - <cache type="com.sunyo.wlpt.message.bus.service.cache.RedisCache"/>  
6 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.VirtualHost"> 4 <resultMap id="BaseResultMap" type="com.sunyo.wlpt.message.bus.service.domain.VirtualHost">
7 <!--@mbg.generated--> 5 <!--@mbg.generated-->
8 <!--@Table virtual_host--> 6 <!--@Table virtual_host-->
@@ -35,6 +33,14 @@ @@ -35,6 +33,14 @@
35 where id = #{id,jdbcType=VARCHAR} 33 where id = #{id,jdbcType=VARCHAR}
36 </select> 34 </select>
37 35
  36 + <select id="selectByVirtualHostName" parameterType="java.lang.String" resultMap="BaseResultMap">
  37 + <!--@mbg.generated-->
  38 + select
  39 + <include refid="Base_Column_List"/>
  40 + from virtual_host
  41 + where virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
  42 + </select>
  43 +
38 <!-- 根据服务器id,查询虚拟主机列表 --> 44 <!-- 根据服务器id,查询虚拟主机列表 -->
39 <select id="selectByServerId" parameterType="java.lang.String" resultMap="BaseResultMap"> 45 <select id="selectByServerId" parameterType="java.lang.String" resultMap="BaseResultMap">
40 <!--@mbg.generated--> 46 <!--@mbg.generated-->