根据偏移查询消息

This commit is contained in:
许晓东
2021-12-11 23:56:18 +08:00
parent 8169ddb019
commit c17b0aa4b9
11 changed files with 373 additions and 79 deletions

View File

@@ -18,4 +18,6 @@ public class QueryMessage {
private long startTime; private long startTime;
private long endTime; private long endTime;
private long offset;
} }

View File

@@ -21,12 +21,22 @@ public class QueryMessageDTO {
private Date endTime; private Date endTime;
private Long offset;
public QueryMessage toQueryMessage() { public QueryMessage toQueryMessage() {
QueryMessage queryMessage = new QueryMessage(); QueryMessage queryMessage = new QueryMessage();
queryMessage.setTopic(topic); queryMessage.setTopic(topic);
queryMessage.setPartition(partition); queryMessage.setPartition(partition);
queryMessage.setStartTime(startTime.getTime()); if (startTime != null) {
queryMessage.setEndTime(endTime.getTime()); queryMessage.setStartTime(startTime.getTime());
}
if (endTime != null) {
queryMessage.setEndTime(endTime.getTime());
}
if (offset != null) {
queryMessage.setOffset(offset);
}
return queryMessage; return queryMessage;
} }

View File

@@ -25,4 +25,9 @@ public class MessageController {
public Object searchByTime(@RequestBody QueryMessageDTO dto) { public Object searchByTime(@RequestBody QueryMessageDTO dto) {
return messageService.searchByTime(dto.toQueryMessage()); return messageService.searchByTime(dto.toQueryMessage());
} }
@PostMapping("/search/offset")
public Object searchByOffset(@RequestBody QueryMessageDTO dto) {
return messageService.searchByOffset(dto.toQueryMessage());
}
} }

View File

@@ -12,4 +12,6 @@ import com.xuxd.kafka.console.beans.ResponseData;
public interface MessageService { public interface MessageService {
ResponseData searchByTime(QueryMessage queryMessage); ResponseData searchByTime(QueryMessage queryMessage);
ResponseData searchByOffset(QueryMessage queryMessage);
} }

View File

@@ -38,6 +38,29 @@ public class MessageServiceImpl implements MessageService {
@Override public ResponseData searchByTime(QueryMessage queryMessage) { @Override public ResponseData searchByTime(QueryMessage queryMessage) {
int maxNums = 10000; int maxNums = 10000;
Set<TopicPartition> partitions = getPartitions(queryMessage);
List<ConsumerRecord<byte[], byte[]>> records = messageConsole.searchBy(partitions, queryMessage.getStartTime(), queryMessage.getEndTime(), maxNums);
List<ConsumerRecordVO> vos = records.stream().filter(record -> record.timestamp() <= queryMessage.getEndTime())
.map(ConsumerRecordVO::fromConsumerRecord).collect(Collectors.toList());
Map<String, Object> res = new HashMap<>();
res.put("maxNum", maxNums);
res.put("realNum", vos.size());
res.put("data", vos.subList(0, Math.min(maxNums, vos.size())));
return ResponseData.create().data(res).success();
}
@Override public ResponseData searchByOffset(QueryMessage queryMessage) {
Set<TopicPartition> partitions = getPartitions(queryMessage);
Map<TopicPartition, Object> offsetTable = new HashMap<>();
partitions.forEach(tp -> {
offsetTable.put(tp, queryMessage.getOffset());
});
Map<TopicPartition, ConsumerRecord<byte[], byte[]>> recordMap = messageConsole.searchBy(offsetTable);
return ResponseData.create().data(recordMap.values().stream().map(ConsumerRecordVO::fromConsumerRecord).collect(Collectors.toList())).success();
}
private Set<TopicPartition> getPartitions(QueryMessage queryMessage) {
Set<TopicPartition> partitions = new HashSet<>(); Set<TopicPartition> partitions = new HashSet<>();
if (queryMessage.getPartition() != -1) { if (queryMessage.getPartition() != -1) {
partitions.add(new TopicPartition(queryMessage.getTopic(), queryMessage.getPartition())); partitions.add(new TopicPartition(queryMessage.getTopic(), queryMessage.getPartition()));
@@ -50,13 +73,6 @@ public class MessageServiceImpl implements MessageService {
.map(tp -> new TopicPartition(queryMessage.getTopic(), tp.partition())).collect(Collectors.toSet()); .map(tp -> new TopicPartition(queryMessage.getTopic(), tp.partition())).collect(Collectors.toSet());
partitions.addAll(set); partitions.addAll(set);
} }
List<ConsumerRecord<byte[], byte[]>> records = messageConsole.searchBy(partitions, queryMessage.getStartTime(), queryMessage.getEndTime(), maxNums); return partitions;
List<ConsumerRecordVO> vos = records.stream().filter(record -> record.timestamp() <= queryMessage.getEndTime())
.map(ConsumerRecordVO::fromConsumerRecord).collect(Collectors.toList());
Map<String, Object> res = new HashMap<>();
res.put("maxNum", maxNums);
res.put("realNum", vos.size());
res.put("data", vos.subList(0, Math.min(maxNums, vos.size())));
return ResponseData.create().data(res).success();
} }
} }

View File

@@ -8,7 +8,7 @@ import java.time.Duration
import java.util import java.util
import java.util.Properties import java.util.Properties
import scala.collection.immutable import scala.collection.immutable
import scala.jdk.CollectionConverters.CollectionHasAsScala import scala.jdk.CollectionConverters.{CollectionHasAsScala, MapHasAsScala}
/** /**
* kafka-console-ui. * kafka-console-ui.
@@ -86,4 +86,59 @@ class MessageConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConf
res res
} }
def searchBy(
tp2o: util.Map[TopicPartition, Long]): util.Map[TopicPartition, ConsumerRecord[Array[Byte], Array[Byte]]] = {
val props = new Properties()
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false")
val res = new util.HashMap[TopicPartition, ConsumerRecord[Array[Byte], Array[Byte]]]()
withConsumerAndCatchError(consumer => {
var tpSet = tp2o.keySet()
val tpSetCopy = new util.HashSet[TopicPartition](tpSet)
val endOffsets = consumer.endOffsets(tpSet)
val beginOffsets = consumer.beginningOffsets(tpSet)
for ((tp, off) <- tp2o.asScala) {
val endOff = endOffsets.get(tp)
// if (endOff <= off) {
// consumer.seek(tp, endOff)
// tpSetCopy.remove(tp)
// } else {
// consumer.seek(tp, off)
// }
val beginOff = beginOffsets.get(tp)
if (off < beginOff || off >= endOff) {
tpSetCopy.remove(tp)
}
}
tpSet = tpSetCopy
consumer.assign(tpSet)
tpSet.asScala.foreach(tp => {
consumer.seek(tp, tp2o.get(tp))
})
var terminate = tpSet.isEmpty
while (!terminate) {
val records = consumer.poll(Duration.ofMillis(timeoutMs))
val tps = new util.HashSet(tpSet).asScala
for (tp <- tps) {
if (!res.containsKey(tp)) {
val recordList = records.records(tp)
if (!recordList.isEmpty) {
val record = recordList.get(0)
res.put(tp, record)
tpSet.remove(tp)
}
}
if (tpSet.isEmpty) {
terminate = true
}
}
}
}, e => {
log.error("searchBy offset error.", e)
})
res
}
} }

View File

@@ -228,4 +228,8 @@ export const KafkaMessageApi = {
url: "/message/search/time", url: "/message/search/time",
method: "post", method: "post",
}, },
searchByOffset: {
url: "/message/search/offset",
method: "post",
},
}; };

View File

@@ -6,7 +6,7 @@
<SearchByTime :topic-list="topicList"></SearchByTime> <SearchByTime :topic-list="topicList"></SearchByTime>
</a-tab-pane> </a-tab-pane>
<a-tab-pane key="2" tab="根据位移查询消息" force-render> <a-tab-pane key="2" tab="根据位移查询消息" force-render>
根据位移查询消息 <SearchByOffset :topic-list="topicList"></SearchByOffset>
</a-tab-pane> </a-tab-pane>
<a-tab-pane key="3" tab="消息发送"> 消息发送1 </a-tab-pane> <a-tab-pane key="3" tab="消息发送"> 消息发送1 </a-tab-pane>
</a-tabs> </a-tabs>
@@ -16,12 +16,13 @@
<script> <script>
import SearchByTime from "@/views/message/SearchByTime"; import SearchByTime from "@/views/message/SearchByTime";
import SearchByOffset from "@/views/message/SearchByOffset";
import request from "@/utils/request"; import request from "@/utils/request";
import { KafkaTopicApi } from "@/utils/api"; import { KafkaTopicApi } from "@/utils/api";
import notification from "ant-design-vue/lib/notification"; import notification from "ant-design-vue/lib/notification";
export default { export default {
name: "Message", name: "Message",
components: { SearchByTime }, components: { SearchByTime, SearchByOffset },
data() { data() {
return { return {
loading: false, loading: false,

View File

@@ -0,0 +1,70 @@
<template>
<div>
<a-table
:columns="columns"
:data-source="data"
bordered
row-key="(record,index)=>{return index}"
>
<div slot="operation" slot-scope="{}">
<a-button size="small" href="javascript:;" class="operation-btn"
>消息详情
</a-button>
</div>
</a-table>
</div>
</template>
<script>
import moment from "moment";
export default {
name: "MessageList",
components: {},
props: {
data: {
type: Array,
},
},
data() {
return {
columns: columns,
};
},
};
const columns = [
{
title: "topic",
dataIndex: "topic",
key: "topic",
width: 300,
},
{
title: "分区",
dataIndex: "partition",
key: "partition",
},
{
title: "偏移",
dataIndex: "offset",
key: "offset",
},
{
title: "时间",
dataIndex: "timestamp",
key: "timestamp",
slots: { title: "timestamp" },
scopedSlots: { customRender: "timestamp" },
customRender: (text) => {
return moment(text).format("YYYY-MM-DD HH:mm:ss:SSS");
},
},
{
title: "操作",
key: "operation",
scopedSlots: { customRender: "operation" },
width: 200,
},
];
</script>
<style scoped></style>

View File

@@ -0,0 +1,192 @@
<template>
<div class="tab-content">
<a-spin :spinning="loading">
<div id="components-form-advanced-search">
<a-form
class="ant-advanced-search-form"
:form="form"
@submit="handleSearch"
>
<a-row :gutter="24">
<a-col :span="6">
<a-form-item label="topic">
<a-select
class="type-select"
@change="handleTopicChange"
show-search
option-filter-prop="children"
v-decorator="[
'topic',
{
rules: [{ required: true, message: '请选择一个topic!' }],
},
]"
placeholder="请选择一个topic"
>
<a-select-option v-for="v in topicList" :key="v" :value="v">
{{ v }}
</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="6">
<a-form-item label="分区">
<a-select
class="type-select"
show-search
option-filter-prop="children"
v-model="selectPartition"
placeholder="请选择一个分区"
>
<a-select-option v-for="v in partitions" :key="v" :value="v">
<span v-if="v == -1">全部</span> <span v-else>{{ v }}</span>
</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="10">
<a-form-item label="偏移">
<a-input
v-decorator="[
'offset',
{
rules: [{ required: true, message: '请输入消息偏移!' }],
},
]"
placeholder="消息偏移"
/>
</a-form-item>
</a-col>
<a-col :span="2" :style="{ textAlign: 'right' }">
<a-form-item>
<a-button type="primary" html-type="submit"> 搜索</a-button>
</a-form-item>
</a-col>
</a-row>
</a-form>
</div>
<MessageList :data="data"></MessageList>
</a-spin>
</div>
</template>
<script>
import request from "@/utils/request";
import { KafkaMessageApi, KafkaTopicApi } from "@/utils/api";
import notification from "ant-design-vue/lib/notification";
import MessageList from "@/views/message/MessageList";
export default {
name: "SearchByOffset",
components: { MessageList },
props: {
topicList: {
type: Array,
},
},
data() {
return {
loading: false,
form: this.$form.createForm(this, { name: "message_search_time" }),
partitions: [],
selectPartition: undefined,
rangeConfig: {
rules: [{ type: "array", required: true, message: "请选择时间!" }],
},
data: defaultData,
};
},
methods: {
handleSearch() {
this.form.validateFields((err, values) => {
if (!err) {
const data = Object.assign({}, values, {
partition: this.selectPartition,
});
this.loading = true;
request({
url: KafkaMessageApi.searchByOffset.url,
method: KafkaMessageApi.searchByOffset.method,
data: data,
}).then((res) => {
this.loading = false;
if (res.code == 0) {
this.$message.success(res.msg);
this.data = res.data;
} else {
notification.error({
message: "error",
description: res.msg,
});
}
});
}
});
},
getPartitionInfo(topic) {
this.loading = true;
request({
url: KafkaTopicApi.getPartitionInfo.url + "?topic=" + topic,
method: KafkaTopicApi.getPartitionInfo.method,
}).then((res) => {
this.loading = false;
if (res.code != 0) {
notification.error({
message: "error",
description: res.msg,
});
} else {
this.partitions = res.data.map((v) => v.partition);
this.partitions.splice(0, 0, -1);
}
});
},
handleTopicChange(topic) {
this.selectPartition = -1;
this.getPartitionInfo(topic);
},
},
};
const defaultData = { realNum: 0, maxNum: 0 };
</script>
<style scoped>
.tab-content {
width: 100%;
height: 100%;
}
.ant-advanced-search-form {
padding: 24px;
background: #fbfbfb;
border: 1px solid #d9d9d9;
border-radius: 6px;
}
.ant-advanced-search-form .ant-form-item {
display: flex;
}
.ant-advanced-search-form .ant-form-item-control-wrapper {
flex: 1;
}
#components-form-topic-advanced-search .ant-form {
max-width: none;
margin-bottom: 1%;
}
#components-form-advanced-search .search-result-list {
margin-top: 16px;
border: 1px dashed #e9e9e9;
border-radius: 6px;
background-color: #fafafa;
min-height: 200px;
text-align: center;
padding-top: 80px;
}
.type-select {
width: 200px !important;
}
</style>

View File

@@ -61,8 +61,6 @@
</a-row> </a-row>
</a-form> </a-form>
</div> </div>
<!-- <div class="operation-row-button">-->
<!-- </div>-->
<p style="margin-top: 1%"> <p style="margin-top: 1%">
<strong <strong
>检索条数:{{ data.realNum }},允许返回的最大条数:{{ >检索条数:{{ data.realNum }},允许返回的最大条数:{{
@@ -70,18 +68,7 @@
}}</strong }}</strong
> >
</p> </p>
<a-table <MessageList :data="data.data"></MessageList>
:columns="columns"
:data-source="data.data"
bordered
row-key="(record,index)=>{return index}"
>
<div slot="operation" slot-scope="{}">
<a-button size="small" href="javascript:;" class="operation-btn"
>消息详情
</a-button>
</div>
</a-table>
</a-spin> </a-spin>
</div> </div>
</template> </template>
@@ -90,11 +77,11 @@
import request from "@/utils/request"; import request from "@/utils/request";
import { KafkaMessageApi, KafkaTopicApi } from "@/utils/api"; import { KafkaMessageApi, KafkaTopicApi } from "@/utils/api";
import notification from "ant-design-vue/lib/notification"; import notification from "ant-design-vue/lib/notification";
import moment from "moment"; import MessageList from "@/views/message/MessageList";
export default { export default {
name: "SearchByTime", name: "SearchByTime",
components: {}, components: { MessageList },
props: { props: {
topicList: { topicList: {
type: Array, type: Array,
@@ -110,7 +97,6 @@ export default {
rules: [{ type: "array", required: true, message: "请选择时间!" }], rules: [{ type: "array", required: true, message: "请选择时间!" }],
}, },
data: defaultData, data: defaultData,
columns: columns,
}; };
}, },
methods: { methods: {
@@ -166,41 +152,6 @@ export default {
}, },
}, },
}; };
const columns = [
{
title: "topic",
dataIndex: "topic",
key: "topic",
width: 300,
},
{
title: "分区",
dataIndex: "partition",
key: "partition",
},
{
title: "偏移",
dataIndex: "offset",
key: "offset",
},
{
title: "时间",
dataIndex: "timestamp",
key: "timestamp",
slots: { title: "timestamp" },
scopedSlots: { customRender: "timestamp" },
customRender: (text) => {
return moment(text).format("YYYY-MM-DD HH:mm:ss:SSS");
},
},
{
title: "操作",
key: "operation",
scopedSlots: { customRender: "operation" },
width: 200,
},
];
const defaultData = { realNum: 0, maxNum: 0 }; const defaultData = { realNum: 0, maxNum: 0 };
</script> </script>
@@ -240,20 +191,6 @@ const defaultData = { realNum: 0, maxNum: 0 };
padding-top: 80px; padding-top: 80px;
} }
.input-w {
width: 400px;
}
.operation-row-button {
height: 4%;
text-align: left;
margin-bottom: 8px;
}
.operation-btn {
margin-right: 3%;
}
.type-select { .type-select {
width: 200px !important; width: 200px !important;
} }