限流,支持用户and客户端ID同时存在.

This commit is contained in:
许晓东
2023-02-06 22:11:56 +08:00
parent 5a87e9cad8
commit fc7f05cf4e
6 changed files with 84 additions and 32 deletions

View File

@@ -28,20 +28,24 @@ public class ClientQuotaController {
@PostMapping
public Object alterClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) {
if (CollectionUtils.isEmpty(request.getTypes())
|| CollectionUtils.isEmpty(request.getNames())
|| request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
if (request.getTypes().size() != 2) {
if (CollectionUtils.isEmpty(request.getTypes())
|| CollectionUtils.isEmpty(request.getNames())
|| request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
}
}
return clientQuotaService.alterClientQuotaConfigs(request);
}
@DeleteMapping
public Object deleteClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) {
if (CollectionUtils.isEmpty(request.getTypes())
|| CollectionUtils.isEmpty(request.getNames())
|| request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
if (request.getTypes().size() != 2) {
if (CollectionUtils.isEmpty(request.getTypes())
|| CollectionUtils.isEmpty(request.getNames())
|| request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
}
}
return clientQuotaService.deleteClientQuotaConfigs(request);
}

View File

@@ -34,12 +34,14 @@ class ClientQuotaConsole(config: KafkaConfig) extends KafkaConsole(config: Kafka
}
def alterQuotaConfigs(entityTypes: java.util.List[String], entityNames: java.util.List[String], configsToBeAddedMap: java.util.Map[String, String], configsToBeDeleted: java.util.List[String]): (Boolean, String) = {
withAdminClientAndCatchError(admin => alterQuotaConfigsInner(admin, entityTypes.asScala.toList, entityNames.asScala.toList, configsToBeAddedMap.asScala.toMap, configsToBeDeleted.asScala.toSeq),
withAdminClientAndCatchError(admin => {
alterQuotaConfigsInner(admin, entityTypes.asScala.toList, entityNames.asScala.toList, configsToBeAddedMap.asScala.toMap, configsToBeDeleted.asScala.toSeq)
(true, "")
},
e => {
log.error("getAllClientQuotasConfigs error.", e)
(false, e.getMessage)
})
(true, "")
}).asInstanceOf[(Boolean, String)]
}
private def getAllClientQuotasConfigs(adminClient: Admin, entityTypes: List[String], entityNames: List[String]): java.util.Map[ClientQuotaEntity, java.util.Map[String, Double]] = {

View File

@@ -13,9 +13,9 @@
<a-tab-pane key="4" tab="用户_客户端ID">
<UserAndClientIDQuota></UserAndClientIDQuota>
</a-tab-pane>
<a-tab-pane key="5" tab="IP">
<IpQuota></IpQuota>
</a-tab-pane>
<!-- <a-tab-pane key="5" tab="IP">-->
<!-- <IpQuota></IpQuota>-->
<!-- </a-tab-pane>-->
</a-tabs>
</a-spin>
</div>
@@ -25,11 +25,10 @@
import ClientIDQuota from "@/views/quota/ClientIDQuota.vue";
import UserQuota from "@/views/quota/UserQuota.vue";
import UserAndClientIDQuota from "@/views/quota/UserAndClientIDQuota.vue";
import IpQuota from "@/views/quota/IpQuota.vue";
export default {
name: "ClientQuota",
components: {ClientIDQuota, UserQuota, UserAndClientIDQuota, IpQuota},
components: {ClientIDQuota, UserQuota, UserAndClientIDQuota},
data() {
return {
loading: false,

View File

@@ -90,27 +90,42 @@ export default {
this.loading = true;
const params = {type: this.type};
params.types = [];
params.names = [];
if (this.type == "user") {
params.types.push("user");
if (record.user) {
params.names = [record.user.trim()];
params.names.push(record.user.trim());
} else {
params.names = [""];
params.names.push("");
}
} else if (this.type == "client-id") {
params.types.push("client-id");
if (record.client) {
params.names = [record.client.trim()];
params.names.push(record.client.trim());
} else {
params.names = [""];
params.names.push("");
}
}
if (this.type == "ip") {
params.types.push("ip");
if (record.ip) {
params.names = [record.ip.trim()];
params.names.push(record.ip.trim());
} else {
params.names = [""];
params.names.push("");
}
}
if (this.type == "user&client-id") {
params.types.push("user");
params.types.push("client-id");
if (record.user) {
params.names.push(record.user.trim());
} else {
params.names.push("");
}
if (record.client) {
params.names.push(record.client.trim());
} else {
params.names.push("");
}
}
request({

View File

@@ -159,28 +159,43 @@ export default {
params.deleteConfigs.push("requestPercentage");
}
params.types = [];
params.names = [];
if (this.showUser) {
params.types.push("user");
if (values.user) {
params.names = [values.user.trim()];
params.names.push(values.user.trim());
} else {
params.names = [""];
params.names.push("");
}
}
if (this.showClientId) {
params.types.push("client-id");
if (values.client) {
params.names = [values.client.trim()];
params.names.push(values.client.trim());
} else {
params.names = [""];
params.names.push("");
}
}
if (this.showIP) {
params.types.push("ip");
if (values.ip) {
params.names = [values.ip.trim()];
params.names.push(values.ip.trim());
} else {
params.names = [""];
params.names.push("");
}
}
if (this.showUser && this.showClientId) {
params.types.push("user");
params.types.push("client-id");
if (values.user) {
params.names.push(values.user.trim());
} else {
params.names.push("");
}
if (values.client) {
params.names.push(values.client.trim());
} else {
params.names.push("");
}
}
this.loading = true;

View File

@@ -8,7 +8,7 @@
@submit="handleSearch"
>
<a-row :gutter="24">
<a-col :span="16">
<a-col :span="10">
<a-form-item label="用户标识">
<a-input
v-decorator="[
@@ -18,6 +18,16 @@
/>
</a-form-item>
</a-col>
<a-col :span="10">
<a-form-item label="客户端ID">
<a-input
v-decorator="[
'client',
]"
placeholder="请输入客户端ID!"
/>
</a-form-item>
</a-col>
<a-col :span="2" :style="{ textAlign: 'right' }">
<a-form-item>
<a-button type="primary" html-type="submit"> 搜索</a-button>
@@ -32,7 +42,8 @@
</a-button>
</div>
<QuotaList type="user&client-id" :columns="columns" :data="data" @refreshQuotaList="refresh"></QuotaList>
<AddQuotaConfig type="user&client-id" :visible="showAddQuotaDialog" :showUser="true" :showClientId="true" @closeAddQuotaDialog="closeAddQuotaDialog"></AddQuotaConfig>
<AddQuotaConfig type="user&client-id" :visible="showAddQuotaDialog" :showUser="true" :showClientId="true"
@closeAddQuotaDialog="closeAddQuotaDialog"></AddQuotaConfig>
</a-spin>
</div>
</template>
@@ -97,9 +108,15 @@ export default {
this.form.validateFields((err, values) => {
if (!err) {
this.loading = true;
const params = {types: ["user"]};
const params = {types: ["user", "client-id"], names: []};
if (values.user) {
params.names = [values.user.trim()];
params.names.push(values.user.trim());
}
if (values.client) {
if (params.names.length == 0) {
params.names.push("");
}
params.names.push(values.client.trim());
}
request({
url: KafkaClientQuotaApi.getClientQuotaConfigs.url,