|
|
|
@ -1,11 +1,17 @@
|
|
|
|
|
package com.example.demokafka.controller;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.example.demokafka.common.api.vo.Result;
|
|
|
|
|
import com.example.demokafka.model.Person;
|
|
|
|
|
import com.example.demokafka.service.ProducerService;
|
|
|
|
|
import com.example.demokafka.util.Kafka;
|
|
|
|
|
import com.example.demokafka.util.KafkaUtil;
|
|
|
|
|
import io.swagger.annotations.*;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.kafka.clients.consumer.Consumer;
|
|
|
|
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
|
|
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
|
|
|
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
|
|
@ -13,10 +19,7 @@ import org.springframework.kafka.core.KafkaTemplate;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import springfox.documentation.annotations.ApiIgnore;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author 赵恒
|
|
|
|
@ -41,4 +44,19 @@ public class TestController {
|
|
|
|
|
producerService.sendMessage(topicName, "测试");
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/consumer/{topic}")
|
|
|
|
|
@ApiOperation(value = "consumer")
|
|
|
|
|
public Result getConsumerData(@PathVariable String topic) {
|
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
|
Consumer<String, String> consumer = new KafkaConsumer<String, String>(KafkaUtil.getSafeConsumerPro());
|
|
|
|
|
consumer.subscribe(Arrays.asList(topic));
|
|
|
|
|
ConsumerRecords<String, String> records = consumer.poll(5000);
|
|
|
|
|
for (ConsumerRecord<String, String> consumerRecord : records) {
|
|
|
|
|
String kfkContent = consumerRecord.value();
|
|
|
|
|
result.add(kfkContent);
|
|
|
|
|
}
|
|
|
|
|
consumer.close();
|
|
|
|
|
return Result.ok(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|