broker config

This commit is contained in:
许晓东
2021-11-03 21:09:02 +08:00
parent 7b43fa7343
commit 2c2558d157
8 changed files with 242 additions and 38 deletions

View File

@@ -0,0 +1,62 @@
package com.xuxd.kafka.console.beans.vo;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import org.apache.kafka.clients.admin.ConfigEntry;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-11-03 15:32:08
**/
@Data
public class ConfigEntryVO implements Comparable {
private static final Map<String, Integer> ORDER_DICTIONARY = new HashMap<>();
static {
ORDER_DICTIONARY.put("DYNAMIC_TOPIC_CONFIG", 0);
ORDER_DICTIONARY.put("DYNAMIC_BROKER_LOGGER_CONFIG", 1);
ORDER_DICTIONARY.put("DYNAMIC_BROKER_CONFIG", 2);
ORDER_DICTIONARY.put("DYNAMIC_DEFAULT_BROKER_CONFIG", 3);
ORDER_DICTIONARY.put("DEFAULT_CONFIG", 4);
ORDER_DICTIONARY.put("STATIC_BROKER_CONFIG", 5);
ORDER_DICTIONARY.put("UNKNOWN", 6);
}
private String name;
private String value;
private String source;
private boolean sensitive;
private boolean readOnly;
public static ConfigEntryVO from(ConfigEntry entry) {
ConfigEntryVO vo = new ConfigEntryVO();
vo.name = entry.name();
vo.value = entry.value();
vo.source = entry.source().name();
vo.sensitive = entry.isSensitive();
vo.readOnly = entry.isReadOnly();
return vo;
}
@Override public int compareTo(Object o) {
ConfigEntryVO that = (ConfigEntryVO) o;
if (!this.source.equals(that.source)) {
return ORDER_DICTIONARY.get(this.source) - ORDER_DICTIONARY.get(that.source);
}
if (this.readOnly != that.readOnly) {
return this.readOnly ? 1 : -1;
}
return this.name.compareTo(that.name);
}
}

View File

@@ -5,7 +5,6 @@ import com.xuxd.kafka.console.config.KafkaConfig;
import com.xuxd.kafka.console.service.ConfigService;
import com.xuxd.kafka.console.utils.ConvertUtil;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -43,7 +42,7 @@ public class ConfigController {
}
@GetMapping("/broker")
public Object getTopicConfig() {
return configService.getBrokerConfig();
public Object getBrokerConfig(String brokerId) {
return configService.getBrokerConfig(brokerId);
}
}

View File

@@ -12,5 +12,5 @@ public interface ConfigService {
ResponseData getTopicConfig(String topic);
ResponseData getBrokerConfig();
ResponseData getBrokerConfig(String brokerId);
}

View File

@@ -1,8 +1,10 @@
package com.xuxd.kafka.console.service.impl;
import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.vo.ConfigEntryVO;
import com.xuxd.kafka.console.service.ConfigService;
import java.util.List;
import java.util.stream.Collectors;
import kafka.console.ConfigConsole;
import org.apache.kafka.clients.admin.ConfigEntry;
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,11 +24,14 @@ public class ConfigServiceImpl implements ConfigService {
@Override public ResponseData getTopicConfig(String topic) {
List<ConfigEntry> configEntries = configConsole.getTopicConfig(topic);
return ResponseData.create().success();
List<ConfigEntryVO> vos = configEntries.stream().map(ConfigEntryVO::from).sorted().collect(Collectors.toList());
return ResponseData.create().data(vos).success();
}
@Override public ResponseData getBrokerConfig() {
List<ConfigEntry> configEntries = configConsole.getBrokerConfig();
return ResponseData.create().success();
@Override public ResponseData getBrokerConfig(String brokerId) {
List<ConfigEntry> configEntries = configConsole.getBrokerConfig(brokerId);
List<ConfigEntryVO> vos = configEntries.stream().map(ConfigEntryVO::from).sorted().collect(Collectors.toList());
return ResponseData.create().data(vos).success();
}
}

View File

@@ -5,9 +5,9 @@ import java.util.Collections
import java.util.concurrent.TimeUnit
import com.xuxd.kafka.console.config.KafkaConfig
import kafka.admin.ConfigCommand.{BrokerDefaultEntityName, BrokerLoggerConfigType}
import kafka.admin.ConfigCommand.BrokerLoggerConfigType
import kafka.server.ConfigType
import org.apache.kafka.clients.admin.{Admin, Config, ConfigEntry, DescribeConfigsOptions}
import org.apache.kafka.clients.admin.{Config, ConfigEntry, DescribeConfigsOptions}
import org.apache.kafka.common.config.ConfigResource
import org.apache.kafka.common.internals.Topic
@@ -23,50 +23,39 @@ class ConfigConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConfi
import java.util.List
def getTopicConfig(topic: String) : List[ConfigEntry] = {
def getTopicConfig(topic: String): List[ConfigEntry] = {
getConfig(ConfigType.Topic, topic)
}
def getBrokerConfig() : List[ConfigEntry] = {
getConfig(ConfigType.Broker, "0")
def getBrokerConfig(broker: String): List[ConfigEntry] = {
getConfig(ConfigType.Broker, broker)
}
def getConfig(entityType: String, entityName: String): List[ConfigEntry] = {
getResourceConfig(entityType, entityName, true, false).asJava
getResourceConfig(entityType, entityName, false).asJava
}
private def getResourceConfig(entityType: String, entityName: String, includeSynonyms: Boolean,
describeAll: Boolean) = {
private def getResourceConfig(entityType: String, entityName: String, includeSynonyms: Boolean) = {
def validateBrokerId(): Unit = try entityName.toInt catch {
case _: NumberFormatException =>
throw new IllegalArgumentException(s"The entity name for $entityType must be a valid integer broker id, found: $entityName")
}
val (configResourceType, dynamicConfigSource) = entityType match {
val configResourceType = entityType match {
case ConfigType.Topic =>
if (!entityName.isEmpty)
Topic.validate(entityName)
(ConfigResource.Type.TOPIC, Some(ConfigEntry.ConfigSource.DYNAMIC_TOPIC_CONFIG))
case ConfigType.Broker => entityName match {
case BrokerDefaultEntityName =>
(ConfigResource.Type.BROKER, Some(ConfigEntry.ConfigSource.DYNAMIC_DEFAULT_BROKER_CONFIG))
case _ =>
validateBrokerId()
(ConfigResource.Type.BROKER, Some(ConfigEntry.ConfigSource.STATIC_BROKER_CONFIG))
}
ConfigResource.Type.TOPIC
case ConfigType.Broker =>
validateBrokerId()
ConfigResource.Type.BROKER
case BrokerLoggerConfigType =>
if (!entityName.isEmpty)
validateBrokerId()
(ConfigResource.Type.BROKER_LOGGER, None)
ConfigResource.Type.BROKER_LOGGER
case entityType => throw new IllegalArgumentException(s"Invalid entity type: $entityType")
}
val configSourceFilter = if (describeAll)
None
else
dynamicConfigSource
val configResource = new ConfigResource(configResourceType, entityName)
val describeOptions = new DescribeConfigsOptions().includeSynonyms(includeSynonyms)
val configs = withAdminClientAndCatchError(admin => Some(admin.describeConfigs(Collections.singleton(configResource), describeOptions)
@@ -78,11 +67,7 @@ class ConfigConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaConfi
configs match {
case None => Seq.empty
case Some(c: util.Map[ConfigResource, Config]) => c.get(configResource).entries.asScala
.filter(entry => configSourceFilter match {
case Some(configSource) => entry.source == configSource
case None => true
}).toSeq
case Some(c: util.Map[ConfigResource, Config]) => c.get(configResource).entries.asScala.toSeq
}
}

View File

@@ -54,6 +54,10 @@ export const KafkaConfigApi = {
url: "/config",
method: "get",
},
getBrokerConfig: {
url: "/config/broker",
method: "get",
},
};
export const KafkaTopicApi = {

View File

@@ -0,0 +1,119 @@
<template>
<a-modal
title="Broker配置"
:visible="show"
:width="1200"
:mask="false"
:destroyOnClose="true"
:footer="null"
:maskClosable="false"
@cancel="handleCancel"
>
<div>
<a-spin :spinning="loading">
<a-table
:columns="columns"
:data-source="data"
bordered
:rowKey="(record) => record.memberId"
>
<ul slot="partitions" slot-scope="text">
<ol v-for="i in text" :key="i.topic + i.partition">
{{
i.topic
}}:
{{
i.partition
}}
</ol>
</ul>
</a-table>
</a-spin>
</div>
</a-modal>
</template>
<script>
import request from "@/utils/request";
import { KafkaConfigApi } from "@/utils/api";
import notification from "ant-design-vue/es/notification";
export default {
name: "BrokerConfig",
props: {
group: {
type: String,
default: "",
},
visible: {
type: Boolean,
default: false,
},
id: {
type: String,
default: "",
},
},
data() {
return {
columns: columns,
show: this.visible,
data: [],
loading: false,
};
},
watch: {
visible(v) {
this.show = v;
if (this.show) {
this.getPartitionInfo();
}
},
},
methods: {
getPartitionInfo() {
this.loading = true;
request({
url: KafkaConfigApi.getBrokerConfig.url + "?brokerId=" + this.id,
method: KafkaConfigApi.getBrokerConfig.method,
}).then((res) => {
this.loading = false;
if (res.code != 0) {
notification.error({
message: "error",
description: res.msg,
});
} else {
this.data = res.data;
}
});
},
handleCancel() {
this.data = [];
this.$emit("closeBrokerConfigDialog", {});
},
},
};
const columns = [
{
title: "属性",
dataIndex: "name",
key: "name",
width: 300,
},
{
title: "值",
dataIndex: "value",
key: "value",
},
{
title: "属性源",
dataIndex: "source",
key: "source",
width: 200,
},
];
</script>
<style scoped></style>

View File

@@ -6,15 +6,29 @@
<h3>集群ID{{ clusterId }}</h3>
</div>
<a-table :columns="columns" :data-source="data" bordered row-key="name">
<a-table :columns="columns" :data-source="data" bordered row-key="id">
<div slot="addr" slot-scope="text, record">
{{ record.host }}:{{ record.port }}
</div>
<div slot="controller" slot-scope="text">
<span v-if="text" style="color: red"></span><span v-else></span>
</div>
<div slot="operation" slot-scope="record" v-show="!record.internal">
<a-button
size="small"
href="javascript:;"
class="operation-btn"
@click="openBrokerConfigDialog(record)"
>属性配置
</a-button>
</div>
</a-table>
</div>
<BrokerConfig
:visible="showBrokerConfigDialog"
:id="this.select.id"
@closeBrokerConfigDialog="closeBrokerConfigDialog"
></BrokerConfig>
</a-spin>
</div>
</template>
@@ -22,15 +36,19 @@
<script>
import request from "@/utils/request";
import { KafkaClusterApi } from "@/utils/api";
import BrokerConfig from "@/views/cluster/BrokerConfig";
export default {
name: "Topic",
components: { BrokerConfig },
data() {
return {
data: [],
columns,
loading: false,
clusterId: "",
showBrokerConfigDialog: false,
select: {},
};
},
methods: {
@@ -45,6 +63,13 @@ export default {
this.clusterId = res.data.clusterId;
});
},
openBrokerConfigDialog(record) {
this.select = record;
this.showBrokerConfigDialog = true;
},
closeBrokerConfigDialog() {
this.showBrokerConfigDialog = false;
},
},
created() {
this.getClusterInfo();
@@ -68,6 +93,11 @@ const columns = [
dataIndex: "controller",
scopedSlots: { customRender: "controller" },
},
{
title: "操作",
key: "operation",
scopedSlots: { customRender: "operation" },
},
];
</script>