限流,支持用户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 @PostMapping
public Object alterClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) { public Object alterClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) {
if (CollectionUtils.isEmpty(request.getTypes()) if (request.getTypes().size() != 2) {
|| CollectionUtils.isEmpty(request.getNames()) if (CollectionUtils.isEmpty(request.getTypes())
|| request.getTypes().size() != request.getNames().size()) { || CollectionUtils.isEmpty(request.getNames())
return ResponseData.create().failed("types length and names length is invalid."); || request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
}
} }
return clientQuotaService.alterClientQuotaConfigs(request); return clientQuotaService.alterClientQuotaConfigs(request);
} }
@DeleteMapping @DeleteMapping
public Object deleteClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) { public Object deleteClientQuotaConfigs(@RequestBody AlterClientQuotaDTO request) {
if (CollectionUtils.isEmpty(request.getTypes()) if (request.getTypes().size() != 2) {
|| CollectionUtils.isEmpty(request.getNames()) if (CollectionUtils.isEmpty(request.getTypes())
|| request.getTypes().size() != request.getNames().size()) { || CollectionUtils.isEmpty(request.getNames())
return ResponseData.create().failed("types length and names length is invalid."); || request.getTypes().size() != request.getNames().size()) {
return ResponseData.create().failed("types length and names length is invalid.");
}
} }
return clientQuotaService.deleteClientQuotaConfigs(request); 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) = { 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 => { e => {
log.error("getAllClientQuotasConfigs error.", e) log.error("getAllClientQuotasConfigs error.", e)
(false, e.getMessage) (false, e.getMessage)
}) }).asInstanceOf[(Boolean, String)]
(true, "")
} }
private def getAllClientQuotasConfigs(adminClient: Admin, entityTypes: List[String], entityNames: List[String]): java.util.Map[ClientQuotaEntity, java.util.Map[String, Double]] = { 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"> <a-tab-pane key="4" tab="用户_客户端ID">
<UserAndClientIDQuota></UserAndClientIDQuota> <UserAndClientIDQuota></UserAndClientIDQuota>
</a-tab-pane> </a-tab-pane>
<a-tab-pane key="5" tab="IP"> <!-- <a-tab-pane key="5" tab="IP">-->
<IpQuota></IpQuota> <!-- <IpQuota></IpQuota>-->
</a-tab-pane> <!-- </a-tab-pane>-->
</a-tabs> </a-tabs>
</a-spin> </a-spin>
</div> </div>
@@ -25,11 +25,10 @@
import ClientIDQuota from "@/views/quota/ClientIDQuota.vue"; import ClientIDQuota from "@/views/quota/ClientIDQuota.vue";
import UserQuota from "@/views/quota/UserQuota.vue"; import UserQuota from "@/views/quota/UserQuota.vue";
import UserAndClientIDQuota from "@/views/quota/UserAndClientIDQuota.vue"; import UserAndClientIDQuota from "@/views/quota/UserAndClientIDQuota.vue";
import IpQuota from "@/views/quota/IpQuota.vue";
export default { export default {
name: "ClientQuota", name: "ClientQuota",
components: {ClientIDQuota, UserQuota, UserAndClientIDQuota, IpQuota}, components: {ClientIDQuota, UserQuota, UserAndClientIDQuota},
data() { data() {
return { return {
loading: false, loading: false,

View File

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

View File

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

View File

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