当使用自定义编码器实例化kafka生产者时,无法实例化

时间:2016-05-26 21:04:00

标签: apache-kafka kafka-producer-api

我是kafka的新手。我想在我的spring项目中使用kafka producer / consumer来替换activeMQ(jms)。我需要的是一个kafka生产者将我的消息对象发布到一个主题,消费者从主题中订阅它。

首先是我的自定义编码器,解码器相同(对于我的消息类ConfigurationActionMsg):

@Component
public class ConfigActionMessageEncoder implements Encoder<ConfigurationActionMsg> {

    public ConfigActionMessageEncoder() {
    /* This constructor must be present for successful compile. */
    }
    public ConfigActionMessageEncoder(VerifiableProperties verifiableProperties) {
    /* This constructor must be present for successful compile. */
    }

    @Override
    public byte[] toBytes(ConfigurationActionMsg actionMsg){
        return SerializationUtils.serialize(actionMsg);
    }}

以下是处理器和消费者的配置

@Configuration
@ComponentScan(basePackages = {"XXX"})
public class KafkaConfig {
@Bean
public KafkaProducer<String,ConfigurationActionMsg> kafkaProducer(){
    Properties props = new Properties();
    props.put("zk.connect", "127.0.0.1:2181");
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("batch.size", 16384);
    props.put("linger.ms", 1);
    props.put("buffer.memory", 33554432);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "com.atlas.configengine2.XXX.ConfigActionMessageEncoder");

    return new KafkaProducer<>(props);
}

@Bean
public KafkaConsumer<String, ConfigurationActionMsg> kafkaConsumer(){

    Properties props = new Properties();
    props.put("zk.connect", "127.0.0.1:2181");
    props.put("bootstrap.servers", "localhost:9092");
    //We should only have one process running for consumer
    props.put("group.id", "resolverActionTrigger");
    props.put("enable.auto.commit", "true");
    props.put("auto.commit.interval.ms", "1000");
    props.put("session.timeout.ms", "30000");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "com.atlas.configengine2.XXX.ConfigActionMessageDecoder");
    KafkaConsumer<String, ConfigurationActionMsg> consumer = new KafkaConsumer<>(props);
    consumer.subscribe(Arrays.asList("configAction"));
    return consumer;
}}

我不确定这是否是实例化生产者/消费者的正确方法。但这种方式不起作用。因为我的kafkaProducer无法实例化。

一些调试信息:

  

引起:org.apache.kafka.common.KafkaException:   com.atlas.configengine2.jms.ConfigActionMessageEncoder不是   org.apache.kafka.common.serialization.Serializer的实例

但我不确定这是否是唯一的问题?那么如何编写自定义编码器?

3 个答案:

答案 0 :(得分:0)

您应该实施org.apache.kafka.common.serialization.Serializer而不是Encoder

请参阅CustomSerializer示例。

答案 1 :(得分:0)

首先,您应该使用org.apache.kafka.common.serialization.Serializer的子类。

第二,我认为您可以发送对象的字符串表示形式(例如JSON),并与生产者中的StringSerializer保持同步,以避免实现自定义org.apache.kafka.common.serialization.Serializer的开销。

答案 2 :(得分:0)

 //create properties
        //https://kafka.apache.org/documentation/#producerconfigs
        Properties prop = new Properties();
        prop.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092,127.0.0.1:9094,127.0.0.1:9098");
        prop.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        prop.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, Employee.class.getName());

        //Create Producer
        //For String
        //KafkaProducer<String, String> producer= new KafkaProducer(prop);
        //For Object
        KafkaProducer<String, Employee> producer = new KafkaProducer(prop);
        Address add = new Address("India");
        Employee emp = new Employee(1, "Arun", add);
        //For String
    // ProducerRecord prodRecord = new ProducerRecord("aryan_topic", "Any String Value");
        ProducerRecord prodRecord = new ProducerRecord("aryan_topic", emp);
    //Send Record

        producer.send(prodRecord);
        System.out.println("Data Send");

}
}
import org.apache.kafka.common.header.Headers;

导入org.apache.kafka.common.serialization.Serializer;

import java.io.Serializable; 导入java.util.Map;

//由阿伦·辛格(Arun Singh)开发

public class Employee implements Serializable, Serializer {
//add all unImplemented methods here
}
 and it will work as
//
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka version: 2.4.0
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId: 77a89fcf8d7fa018
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka startTimeMs: 1583515298948
[kafka-producer-network-thread | producer-1] INFO org.apache.kafka.clients.Metadata - [Producer clientId=producer-1] Cluster ID: h_EDcwYmThiz-iBTc0AsGw
Data Send