查看消费端成员

This commit is contained in:
许晓东
2021-09-26 21:06:50 +08:00
parent 99438887e5
commit 9deb37e9dc
9 changed files with 249 additions and 6 deletions

View File

@@ -0,0 +1,29 @@
package com.xuxd.kafka.console.beans;
import lombok.Data;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-09-26 20:32:15
**/
@Data
public class TopicPartition implements Comparable {
private final String topic;
private final int partition;
@Override public int compareTo(Object o) {
if (o == null) {
return -1;
}
TopicPartition other = (TopicPartition) o;
if (!this.topic.equals(other.getTopic())) {
return this.compareTo(other);
}
return this.partition - other.partition;
}
}

View File

@@ -0,0 +1,38 @@
package com.xuxd.kafka.console.beans.vo;
import com.xuxd.kafka.console.beans.TopicPartition;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import org.apache.kafka.clients.admin.MemberDescription;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-09-26 20:02:59
**/
@Data
public class ConsumerMemberVO {
private String memberId;
private String groupInstanceId;
private String clientId;
private String host;
private List<TopicPartition> partitions;
public static ConsumerMemberVO from(MemberDescription description) {
ConsumerMemberVO vo = new ConsumerMemberVO();
vo.setMemberId(description.consumerId());
vo.setGroupInstanceId(description.groupInstanceId().orElse(""));
vo.setClientId(description.clientId());
vo.setHost(description.host());
List<TopicPartition> collect = description.assignment().topicPartitions().stream().map(t -> new TopicPartition(t.topic(), t.partition())).collect(Collectors.toList());
collect.sort(Comparator.naturalOrder());
vo.setPartitions(collect);
return vo;
}
}

View File

@@ -12,6 +12,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.ConsumerGroupState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -49,4 +50,9 @@ public class ConsumerController {
public Object deleteConsumerGroup(@RequestParam String groupId) {
return consumerService.deleteConsumerGroup(groupId);
}
@GetMapping("/member")
public Object getConsumerMembers(@RequestParam String groupId) {
return consumerService.getConsumerMembers(groupId);
}
}

View File

@@ -16,4 +16,6 @@ public interface ConsumerService {
ResponseData getConsumerGroupList(List<String> groupIds, Set<ConsumerGroupState> states);
ResponseData deleteConsumerGroup(String groupId);
ResponseData getConsumerMembers(String groupId);
}

View File

@@ -3,7 +3,9 @@ package com.xuxd.kafka.console.service.impl;
import com.xuxd.kafka.console.beans.CounterList;
import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.vo.ConsumerGroupVO;
import com.xuxd.kafka.console.beans.vo.ConsumerMemberVO;
import com.xuxd.kafka.console.service.ConsumerService;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
@@ -11,6 +13,9 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import kafka.console.ConsumerConsole;
import org.apache.commons.collections.CollectionUtils;
import org.apache.kafka.clients.admin.ConsumerGroupDescription;
import org.apache.kafka.clients.admin.MemberDescription;
import org.apache.kafka.common.ConsumerGroupState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -29,6 +34,7 @@ public class ConsumerServiceImpl implements ConsumerService {
private ConsumerConsole consumerConsole;
@Override public ResponseData getConsumerGroupList(List<String> groupIds, Set<ConsumerGroupState> states) {
String simulateGroup = "inner_xxx_not_exit_group_###";
Set<String> groupList = new HashSet<>();
if (groupIds != null && !groupIds.isEmpty()) {
if (states != null && !states.isEmpty()) {
@@ -44,8 +50,15 @@ public class ConsumerServiceImpl implements ConsumerService {
}
} else {
groupList.addAll(consumerConsole.getConsumerGroupIdList(states));
if (groupList.isEmpty()) {
// The consumer groupId that match the specified states could not find, so simulate an impossible groupId.
groupList.add(simulateGroup);
}
}
List<ConsumerGroupVO> consumerGroupVOS = consumerConsole.getConsumerGroupList(groupList).stream().map(c -> ConsumerGroupVO.from(c)).collect(Collectors.toList());
if (consumerGroupVOS.size() == 1 && consumerGroupVOS.get(0).getGroupId().equals(simulateGroup)) {
consumerGroupVOS.clear();
}
consumerGroupVOS.sort(Comparator.comparing(ConsumerGroupVO::getGroupId));
return ResponseData.create().data(new CounterList<>(consumerGroupVOS)).success();
}
@@ -54,4 +67,15 @@ public class ConsumerServiceImpl implements ConsumerService {
Tuple2<Object, String> tuple2 = consumerConsole.deleteConsumerGroups(Collections.singletonList(groupId));
return (Boolean) tuple2._1 ? ResponseData.create().success() : ResponseData.create().failed(tuple2._2);
}
@Override public ResponseData getConsumerMembers(String groupId) {
Set<ConsumerGroupDescription> groupList = consumerConsole.getConsumerGroupList(Collections.singleton(groupId));
if (CollectionUtils.isEmpty(groupList)) {
return ResponseData.create().data(Collections.emptyList()).success();
}
Collection<MemberDescription> members = groupList.stream().findFirst().get().members();
List<ConsumerMemberVO> vos = members.stream().map(ConsumerMemberVO::from).collect(Collectors.toList());
vos.sort(Comparator.comparing(ConsumerMemberVO::getClientId));
return ResponseData.create().data(vos).success();
}
}