topic search

This commit is contained in:
许晓东
2021-09-09 19:53:02 +08:00
parent f4413107d7
commit 966f22da44
8 changed files with 103 additions and 39 deletions

View File

@@ -0,0 +1,11 @@
package com.xuxd.kafka.console.beans.enums;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-09-09 17:42:38
**/
public enum TopicType {
ALL, SYSTEM, NORMAL;
}

View File

@@ -1,9 +1,11 @@
package com.xuxd.kafka.console.controller; package com.xuxd.kafka.console.controller;
import com.xuxd.kafka.console.beans.enums.TopicType;
import com.xuxd.kafka.console.service.TopicService; import com.xuxd.kafka.console.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
@@ -20,7 +22,7 @@ public class TopicController {
private TopicService topicService; private TopicService topicService;
@GetMapping("/list") @GetMapping("/list")
public Object getTopicList() { public Object getTopicList(@RequestParam(required = false) String topic, @RequestParam String type) {
return topicService.getTopicList(); return topicService.getTopicList(topic, TopicType.valueOf(type.toUpperCase()));
} }
} }

View File

@@ -1,6 +1,7 @@
package com.xuxd.kafka.console.service; package com.xuxd.kafka.console.service;
import com.xuxd.kafka.console.beans.ResponseData; import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.enums.TopicType;
/** /**
* kafka-console-ui. * kafka-console-ui.
@@ -12,6 +13,6 @@ public interface TopicService {
ResponseData getTopicNameList(); ResponseData getTopicNameList();
ResponseData getTopicList(); ResponseData getTopicList(String topic, TopicType type);
} }

View File

@@ -1,12 +1,17 @@
package com.xuxd.kafka.console.service.impl; package com.xuxd.kafka.console.service.impl;
import com.xuxd.kafka.console.beans.ResponseData; import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.enums.TopicType;
import com.xuxd.kafka.console.beans.vo.TopicDescriptionVO; import com.xuxd.kafka.console.beans.vo.TopicDescriptionVO;
import com.xuxd.kafka.console.service.TopicService; import com.xuxd.kafka.console.service.TopicService;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import kafka.console.TopicConsole; import kafka.console.TopicConsole;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.admin.TopicDescription;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -25,11 +30,36 @@ public class TopicServiceImpl implements TopicService {
private TopicConsole topicConsole; private TopicConsole topicConsole;
@Override public ResponseData getTopicNameList() { @Override public ResponseData getTopicNameList() {
return ResponseData.create().data(topicConsole.getTopicNameList()).success(); return ResponseData.create().data(topicConsole.getTopicNameList(true)).success();
} }
@Override public ResponseData getTopicList() { @Override public ResponseData getTopicList(String topic, TopicType type) {
List<TopicDescription> topicDescriptions = topicConsole.getTopicList(topicConsole.getTopicNameList()); Set<String> topicSet = new HashSet<>();
switch (type) {
case SYSTEM:
Set<String> internalTopicSet = topicConsole.getInternalTopicNameList();
if (StringUtils.isEmpty(topic)) {
topicSet.addAll(internalTopicSet);
} else {
if (internalTopicSet.contains(topic)) {
topicSet.add(topic);
} else {
return ResponseData.create().data(Collections.emptyList()).success();
}
}
break;
case NORMAL:
Set<String> internalTopicS = topicConsole.getInternalTopicNameList();
if (internalTopicS.contains(topic)) {
return ResponseData.create().data(Collections.emptyList()).success();
}
topicSet.addAll(StringUtils.isEmpty(topic) ? topicConsole.getTopicNameList(false) : Collections.singleton(topic));
break;
default:
topicSet.addAll(StringUtils.isEmpty(topic) ? topicConsole.getTopicNameList(true) : Collections.singleton(topic));
break;
}
List<TopicDescription> topicDescriptions = topicConsole.getTopicList(topicSet);
topicDescriptions.sort(Comparator.comparing(TopicDescription::name)); topicDescriptions.sort(Comparator.comparing(TopicDescription::name));
return ResponseData.create().data(topicDescriptions.stream().map(d -> TopicDescriptionVO.from(d))).success(); return ResponseData.create().data(topicDescriptions.stream().map(d -> TopicDescriptionVO.from(d))).success();

View File

@@ -7,7 +7,7 @@ server:
kafka: kafka:
config: config:
# kafka broker地址多个以逗号分隔 # kafka broker地址多个以逗号分隔
# bootstrap-server: 'localhost:9092' bootstrap-server: 'localhost:9092'
request-timeout-ms: 60000 request-timeout-ms: 60000
# 服务端是否启用acl如果不启用下面的几项都忽略即可 # 服务端是否启用acl如果不启用下面的几项都忽略即可
enable-acl: true enable-acl: true
@@ -17,7 +17,7 @@ kafka:
# 超级管理员用户名在broker上已经配置为超级管理员 # 超级管理员用户名在broker上已经配置为超级管理员
admin-username: admin admin-username: admin
# 超级管理员密码 # 超级管理员密码
admin-password: admin!QAZ admin-password: admin
# 启动自动创建配置的超级管理员用户 # 启动自动创建配置的超级管理员用户
admin-create: true admin-create: true
# broker连接的zk地址 # broker连接的zk地址
@@ -48,6 +48,3 @@ logging:
cron: cron:
# clear-dirty-user: 0 * * * * ? # clear-dirty-user: 0 * * * * ?
clear-dirty-user: 0 0 1 * * ? clear-dirty-user: 0 0 1 * * ?
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

View File

@@ -7,6 +7,8 @@ import java.util.{Collections, List, Set}
import com.xuxd.kafka.console.config.KafkaConfig import com.xuxd.kafka.console.config.KafkaConfig
import org.apache.kafka.clients.admin.{ListTopicsOptions, TopicDescription} import org.apache.kafka.clients.admin.{ListTopicsOptions, TopicDescription}
import scala.jdk.CollectionConverters.{CollectionHasAsScala, SetHasAsJava}
/** /**
* kafka-console-ui. * kafka-console-ui.
* *
@@ -15,8 +17,13 @@ import org.apache.kafka.clients.admin.{ListTopicsOptions, TopicDescription}
* */ * */
class TopicConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConfig) with Logging { class TopicConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConfig) with Logging {
def getTopicNameList(): Set[String] = { /**
withAdminClientAndCatchError(admin => admin.listTopics(new ListTopicsOptions().listInternal(true)).names() * get all topic name set.
*
* @return all topic name set.
*/
def getTopicNameList(internal: Boolean = true): Set[String] = {
withAdminClientAndCatchError(admin => admin.listTopics(new ListTopicsOptions().listInternal(internal)).names()
.get(3000, TimeUnit.MILLISECONDS), .get(3000, TimeUnit.MILLISECONDS),
e => { e => {
log.error("listTopics error.", e) log.error("listTopics error.", e)
@@ -24,6 +31,20 @@ class TopicConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConfig
}).asInstanceOf[Set[String]] }).asInstanceOf[Set[String]]
} }
/**
* get all internal topic name set.
*
* @return internal topic name set.
*/
def getInternalTopicNameList(): Set[String] = {
withAdminClientAndCatchError(admin => admin.listTopics(new ListTopicsOptions().listInternal(true)).listings()
.get(3000, TimeUnit.MILLISECONDS).asScala.filter(_.isInternal).map(_.name()).toSet[String].asJava,
e => {
log.error("listInternalTopics error.", e)
Collections.emptySet()
}).asInstanceOf[Set[String]]
}
def getTopicList(topics: Set[String]): List[TopicDescription] = { def getTopicList(topics: Set[String]): List[TopicDescription] = {
if (topics == null || topics.isEmpty) { if (topics == null || topics.isEmpty) {
Collections.emptyList() Collections.emptyList()

View File

@@ -1,7 +1,7 @@
package com.xuxd.kafka.console.service.impl; package com.xuxd.kafka.console.service.impl;
import com.xuxd.kafka.console.beans.enums.TopicType;
import com.xuxd.kafka.console.service.TopicService; import com.xuxd.kafka.console.service.TopicService;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -27,6 +27,6 @@ public class TopicServiceImplTest {
@Test @Test
public void getTopicList() { public void getTopicList() {
log.info(topicService.getTopicList().getData().toString()); log.info(topicService.getTopicList(null, TopicType.ALL).getData().toString());
} }
} }

View File

@@ -17,6 +17,19 @@
/> />
</a-form-item> </a-form-item>
</a-col> </a-col>
<a-col :span="8">
<a-form-item :label="`类型`">
<a-select
class="type-select"
v-decorator="['type', { initialValue: 'all' }]"
placeholder="Please select a country"
>
<a-select-option value="all"> 所有 </a-select-option>
<a-select-option value="normal"> 普通 </a-select-option>
<a-select-option value="system"> 系统 </a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="8" :style="{ textAlign: 'right' }"> <a-col :span="8" :style="{ textAlign: 'right' }">
<a-form-item> <a-form-item>
@@ -32,22 +45,18 @@
<div class="operation-row-button"> <div class="operation-row-button">
<a-button type="primary" @click="handleReset">新增/更新</a-button> <a-button type="primary" @click="handleReset">新增/更新</a-button>
</div> </div>
<a-table :columns="columns" :data-source="data" bordered> <a-table :columns="columns" :data-source="data" bordered row-key="name">
<div slot="partitions" slot-scope="text"> <div slot="partitions" slot-scope="text">
<a href="#">{{ text }} </a> <a href="#">{{ text }} </a>
</div> </div>
<div slot="internal" slot-scope="text"> <div slot="internal" slot-scope="text">
<span v-if="text"></span><span v-else></span> <span v-if="text" style="color: red"></span><span v-else></span>
</div> </div>
<div <div slot="operation" slot-scope="record" v-show="!record.internal">
slot="operation"
slot-scope="record"
v-show="!record.user || record.user.role != 'admin'"
>
<a-popconfirm <a-popconfirm
:title="'删除用户: ' + record.username + '及相关权限'" :title="'删除topic: ' + record.name + ''"
ok-text="确认" ok-text="确认"
cancel-text="取消" cancel-text="取消"
@confirm="handleReset(record)" @confirm="handleReset(record)"
@@ -70,11 +79,11 @@ export default {
components: {}, components: {},
data() { data() {
return { return {
queryParam: {}, queryParam: { type: "all" },
data: [], data: [],
columns, columns,
selectRow: {}, selectRow: {},
form: this.$form.createForm(this, { name: "advanced_search" }), form: this.$form.createForm(this, { name: "topic_advanced_search" }),
showUpdateUser: false, showUpdateUser: false,
deleteUserConfirm: false, deleteUserConfirm: false,
selectDetail: { selectDetail: {
@@ -87,20 +96,7 @@ export default {
methods: { methods: {
handleSearch(e) { handleSearch(e) {
e.preventDefault(); e.preventDefault();
this.form.validateFields((error, values) => { this.getTopicList();
let queryParam = {};
if (values.username) {
queryParam.username = values.username;
}
if (values.topic) {
queryParam.resourceType = "TOPIC";
queryParam.resourceName = values.topic;
} else if (values.groupId) {
queryParam.resourceType = "GROUP";
queryParam.resourceName = values.groupId;
}
Object.assign(this.queryParam, queryParam);
});
}, },
handleReset() { handleReset() {
@@ -108,9 +104,11 @@ export default {
}, },
getTopicList() { getTopicList() {
Object.assign(this.queryParam, this.form.getFieldsValue());
request({ request({
url: KafkaTopicApi.getTopicList.url, url: KafkaTopicApi.getTopicList.url,
method: KafkaTopicApi.getTopicList.method, method: KafkaTopicApi.getTopicList.method,
params: this.queryParam,
}).then((res) => { }).then((res) => {
this.data = res.data; this.data = res.data;
}); });
@@ -199,4 +197,8 @@ const columns = [
.operation-btn { .operation-btn {
margin-right: 3%; margin-right: 3%;
} }
.type-select {
width: 200px !important;
}
</style> </style>