0

我遇到了关于 Knative Eventing 的问题,我希望得到您的帮助,因为我对此很陌生。我正在使用 Knative v0.10.0、minikube v1.5.2、Docker v19.03.1 和 Kafka 2.3.1(用作事件源)。我使用 Kafka Java 客户端 API 和 Spring Boot 开发了一个 kafka 生产者和一个 kafka 消费者。它们在管道中工作,所以我有一个 CronJobSource 每 X 分钟唤醒我的生产者,生产者将一些事件发送到 Kafka 主题,然后我配置了一个 KafkaSource 来监听该主题并应该唤醒消费者。实际上,即使一切似乎都配置得很好,KafkaSource 什么也没做,我尝试记录 KafkaSource,但它没有错误或成功日志。如果我在不通过 KafkaSource 的情况下手动唤醒生产者和消费者,它们都可以正常工作。

这是我的 KafkaSource yaml 文件配置:

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: KafkaSource
metadata:
  name: ods-datamartextractor-kafka-source
spec:
  consumerGroup: ods-dataconverter-consumer-group-id
  bootstrapServers: my-cluster-kafka-bootstrap.kafka:9092
  topics: datamartextractor-topic
  sink:
    ref:
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: ods-dataconverter-service

这是我的卡夫卡生产者代码:

@Service
public class ODSDataMartExtractor {

public void sendEvent(DataMart dataMart) {
        String event = dataMart.toString();
        logger.info(String.format("#### -> Producing message -> " + event));
        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("datamartextractor-topic", event);
        future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onSuccess(SendResult<String, String> result) {
                System.out.println("Sent message=[" + event + "] with offset=[" + result.getRecordMetadata().offset() + "]");
            }
            @Override
            public void onFailure(Throwable ex) {
                System.out.println("Unable to send message=[" + event + "] due to : " + ex.getMessage());
            }
        });
    }
}

卡夫卡生产者配置:

@Configuration
public class KafkaProducerConfig {

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "my-cluster-kafka-bootstrap.kafka:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return new DefaultKafkaProducerFactory<>(configProps);
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
}

卡夫卡消费者代码:

@Service
public class ODSDataConverter {

@KafkaListener(topics = "datamartextractor-topic", groupId = "ods-dataconverter-consumer-group-id")
    public void consumeEvent(String event) throws IOException {
        logger.info(String.format("#### -> Consumed message -> " + event));
    }
}

卡夫卡消费者配置:

@EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "my-cluster-kafka-bootstrap.kafka:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "ods-dataconverter-consumer-group-id");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}

谢谢!

4

0 回答 0