客户端限流查询接口.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.xuxd.kafka.console.beans.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: xuxd
|
||||
* @date: 2023/1/9 21:53
|
||||
**/
|
||||
@Data
|
||||
public class QueryClientQuotaDTO {
|
||||
|
||||
private List<String> types;
|
||||
|
||||
private List<String> names;
|
||||
}
|
||||
@@ -4,12 +4,20 @@ import lombok.Data;
|
||||
import org.apache.kafka.common.config.internals.QuotaConfigs;
|
||||
import org.apache.kafka.common.quota.ClientQuotaEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 晓东哥哥
|
||||
*/
|
||||
@Data
|
||||
public class ClientQuotaEntityVO {
|
||||
|
||||
private String entry;
|
||||
private String user;
|
||||
|
||||
private String client;
|
||||
|
||||
private String ip;
|
||||
|
||||
private String consumerRate;
|
||||
|
||||
@@ -17,9 +25,24 @@ public class ClientQuotaEntityVO {
|
||||
|
||||
private String requestPercentage;
|
||||
|
||||
public static ClientQuotaEntityVO from(ClientQuotaEntity entity, String type, Map<String, Object> config) {
|
||||
public static ClientQuotaEntityVO from(ClientQuotaEntity entity, List<String> entityTypes, Map<String, Object> config) {
|
||||
ClientQuotaEntityVO entityVO = new ClientQuotaEntityVO();
|
||||
entityVO.setEntry(entity.entries().get(type));
|
||||
Map<String, String> entries = entity.entries();
|
||||
entityTypes.forEach(type -> {
|
||||
switch (type) {
|
||||
case ClientQuotaEntity.USER:
|
||||
entityVO.setUser(entries.get(type));
|
||||
break;
|
||||
case ClientQuotaEntity.CLIENT_ID:
|
||||
entityVO.setClient(entries.get(type));
|
||||
break;
|
||||
case ClientQuotaEntity.IP:
|
||||
entityVO.setIp(entries.get(type));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
entityVO.setConsumerRate(config.getOrDefault(QuotaConfigs.CONSUMER_BYTE_RATE_OVERRIDE_CONFIG, "").toString());
|
||||
entityVO.setProducerRate(config.getOrDefault(QuotaConfigs.PRODUCER_BYTE_RATE_OVERRIDE_CONFIG, "").toString());
|
||||
entityVO.setRequestPercentage(config.getOrDefault(QuotaConfigs.REQUEST_PERCENTAGE_OVERRIDE_CONFIG, "").toString());
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.xuxd.kafka.console.controller;
|
||||
|
||||
import com.xuxd.kafka.console.beans.dto.QueryClientQuotaDTO;
|
||||
import com.xuxd.kafka.console.service.ClientQuotaService;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author: xuxd
|
||||
* @date: 2023/1/9 21:50
|
||||
**/
|
||||
@RestController
|
||||
@RequestMapping("/client/quota")
|
||||
public class ClientQuotaController {
|
||||
|
||||
private final ClientQuotaService clientQuotaService;
|
||||
|
||||
public ClientQuotaController(ClientQuotaService clientQuotaService) {
|
||||
this.clientQuotaService = clientQuotaService;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public Object getClientQuotaConfigs(@RequestBody QueryClientQuotaDTO request) {
|
||||
return clientQuotaService.getClientQuotaConfigs(request.getTypes(), request.getNames());
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,10 @@ import com.xuxd.kafka.console.beans.vo.ClientQuotaEntityVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 晓东哥哥
|
||||
*/
|
||||
public interface ClientQuotaService {
|
||||
|
||||
List<ClientQuotaEntityVO> getClientQuotaConfigs(String type, String name);
|
||||
List<ClientQuotaEntityVO> getClientQuotaConfigs(List<String> types, List<String> names);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ import org.springframework.stereotype.Service;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author 晓东哥哥
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ClientQuotaServiceImpl implements ClientQuotaService {
|
||||
@@ -30,14 +33,28 @@ public class ClientQuotaServiceImpl implements ClientQuotaService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ClientQuotaEntityVO> getClientQuotaConfigs(String type, String name) {
|
||||
List<String> entityNames = StringUtils.isNotBlank(name) ? Arrays.asList(name) : Collections.emptyList();
|
||||
String entityType = typeDict.get(type);
|
||||
if (StringUtils.isEmpty(entityType)) {
|
||||
throw new IllegalArgumentException("type不正确:" + type);
|
||||
public List<ClientQuotaEntityVO> getClientQuotaConfigs(List<String> types, List<String> names) {
|
||||
List<String> entityNames = names == null ? Collections.emptyList() : new ArrayList<>(names);
|
||||
List<String> entityTypes = types.stream().map(e -> typeDict.get(e)).filter(e -> e != null).collect(Collectors.toList());
|
||||
if (entityTypes.isEmpty() || entityTypes.size() != types.size()) {
|
||||
throw new IllegalArgumentException("types illegal.");
|
||||
}
|
||||
Map<ClientQuotaEntity, Map<String, Object>> clientQuotasConfigs = clientQuotaConsole.getClientQuotasConfigs(Arrays.asList(entityType), entityNames);
|
||||
|
||||
return clientQuotasConfigs.entrySet().stream().map(entry -> ClientQuotaEntityVO.from(entry.getKey(), entityType, entry.getValue())).collect(Collectors.toList());
|
||||
boolean userWithClientFilterClientOnly = false;
|
||||
if (entityTypes.size() == 2) {
|
||||
if (names.size() == 2 && StringUtils.isBlank(names.get(0)) && StringUtils.isNotBlank(names.get(1))) {
|
||||
userWithClientFilterClientOnly = true;
|
||||
}
|
||||
}
|
||||
Map<ClientQuotaEntity, Map<String, Object>> clientQuotasConfigs = clientQuotaConsole.getClientQuotasConfigs(entityTypes,
|
||||
userWithClientFilterClientOnly ? Collections.emptyList() : entityNames);
|
||||
|
||||
List<ClientQuotaEntityVO> voList = clientQuotasConfigs.entrySet().stream().map(entry -> ClientQuotaEntityVO.from(
|
||||
entry.getKey(), entityTypes, entry.getValue())).collect(Collectors.toList());
|
||||
if (!userWithClientFilterClientOnly) {
|
||||
return voList;
|
||||
}
|
||||
return voList.stream().filter(e -> names.get(1).equals(e.getClient())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user