重置消费位点

This commit is contained in:
许晓东
2021-10-22 17:22:26 +08:00
parent 11df2d016b
commit 36bb140c79
9 changed files with 164 additions and 7 deletions

View File

@@ -0,0 +1,36 @@
package com.xuxd.kafka.console.beans.dto;
import lombok.Data;
/**
* kafka-console-ui.
*
* @author xuxd
* @date 2021-10-22 16:21:28
**/
@Data
public class ResetOffsetDTO {
// 重置粒度1-> topic2->partition
private int level;
// 重置类型1-> earliest, 2-> latest 3-> timestamp
private int type;
private String groupId;
private String topic;
private int partition;
public interface Level {
int TOPIC = 1;
int PARTITION = 2;
}
public interface Type {
int EARLIEST = 1;
int LATEST = 2;
int TIMESTAMP = 3;
}
}

View File

@@ -1,7 +1,9 @@
package com.xuxd.kafka.console.controller;
import com.xuxd.kafka.console.beans.ResponseData;
import com.xuxd.kafka.console.beans.dto.AddSubscriptionDTO;
import com.xuxd.kafka.console.beans.dto.QueryConsumerGroupDTO;
import com.xuxd.kafka.console.beans.dto.ResetOffsetDTO;
import com.xuxd.kafka.console.service.ConsumerService;
import java.util.Collections;
import java.util.HashSet;
@@ -10,6 +12,7 @@ import java.util.Objects;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
import org.apache.kafka.common.ConsumerGroupState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -66,4 +69,32 @@ public class ConsumerController {
public Object addSubscription(@RequestBody AddSubscriptionDTO subscriptionDTO) {
return consumerService.addSubscription(subscriptionDTO.getGroupId(), subscriptionDTO.getTopic());
}
@PostMapping("/reset/offset")
public Object restOffset(@RequestBody ResetOffsetDTO offsetDTO) {
ResponseData res = ResponseData.create().failed("unknown");
switch (offsetDTO.getLevel()) {
case ResetOffsetDTO.Level.TOPIC:
switch (offsetDTO.getType()) {
case ResetOffsetDTO.Type
.EARLIEST:
res = consumerService.resetOffsetToEndpoint(offsetDTO.getGroupId(), offsetDTO.getTopic(), OffsetResetStrategy.EARLIEST);
break;
case ResetOffsetDTO.Type.LATEST:
res = consumerService.resetOffsetToEndpoint(offsetDTO.getGroupId(), offsetDTO.getTopic(), OffsetResetStrategy.LATEST);
break;
case ResetOffsetDTO.Type.TIMESTAMP:
break;
default:
return ResponseData.create().failed("unknown type");
}
break;
case ResetOffsetDTO.Level.PARTITION:
break;
default:
return ResponseData.create().failed("unknown level");
}
return res;
}
}

View File

@@ -3,6 +3,7 @@ package com.xuxd.kafka.console.service;
import com.xuxd.kafka.console.beans.ResponseData;
import java.util.List;
import java.util.Set;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
import org.apache.kafka.common.ConsumerGroupState;
/**
@@ -22,4 +23,6 @@ public interface ConsumerService {
ResponseData getConsumerDetail(String groupId);
ResponseData addSubscription(String groupId, String topic);
ResponseData resetOffsetToEndpoint(String groupId, String topic, OffsetResetStrategy strategy);
}

View File

@@ -19,6 +19,7 @@ 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.clients.consumer.OffsetResetStrategy;
import org.apache.kafka.common.ConsumerGroupState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -37,7 +38,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_###";
String simulateGroup = "inner_xxx_not_exit_group_###" + System.currentTimeMillis();
Set<String> groupList = new HashSet<>();
if (groupIds != null && !groupIds.isEmpty()) {
if (states != null && !states.isEmpty()) {
@@ -121,4 +122,9 @@ public class ConsumerServiceImpl implements ConsumerService {
return (boolean) tuple2._1() ? ResponseData.create().success() : ResponseData.create().failed(tuple2._2());
}
@Override public ResponseData resetOffsetToEndpoint(String groupId, String topic, OffsetResetStrategy strategy) {
Tuple2<Object, String> tuple2 = consumerConsole.resetOffsetToEndpoint(groupId, topic, strategy);
return (boolean) tuple2._1() ? ResponseData.create().success() : ResponseData.create().failed(tuple2._2());
}
}