我使用以下命令
启动了Kafkadocker run -p 2181:2181 -p 9092:9092 -p 8081:8081 --env ADVERTISED_HOST=`docker-machine ip \`docker-machine active\`` --env ADVERTISED_PORT=9092 spotify/kafka
现在,我写了一个简单的程序,它将一个字符串发布到一个kafka主题中。它没有任何问题。
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.99.100:9092")
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String, String](props)
val inputRecord = new ProducerRecord[String, String]("test", "key2", "Hello World")
producer.send(inputRecord)
producer.close()
所以现在我修改了这个程序并尝试将avro消息发送到kafka主题
val props = new Properties()
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.99.100:9092")
props.put("schema.registry.url", "http://192.168.99.100:8081")
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "io.confluent.kafka.serializers.KafkaAvroSerializer")
val producer = new KafkaProducer[String, Object](props)
val inputRecord = createAvroRecord(schemaStr, "test1", "test1")
val producer: KafkaProducer[String, Object] = CreateProducerAvro
val producerAvroRecord = new ProducerRecord[String, Object]("test", "key1", inputRecord)
producer.send(producerAvroRecord)
producer.close()
但我收到错误
[error] (run-main-0) org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:851)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1092)
at io.confluent.kafka.schemaregistry.client.rest.utils.RestUtils.httpRequest(RestUtils.java:128)
at io.confluent.kafka.schemaregistry.client.rest.utils.RestUtils.registerSchema(RestUtils.java:174)
at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.registerAndGetId(CachedSchemaRegistryClient.java:51)
at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.register(CachedSchemaRegistryClient.java:89)
at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:49)
at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:67)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:424)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:339)
at KafkaPublisher$.SendAvroMessage(KafkaPublisher.scala:35)
at KafkaPublisher$.main(KafkaPublisher.scala:20)
at KafkaPublisher.main(KafkaPublisher.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
[trace] Stack trace suppressed: run last compile:run for the full output.
答案 0 :(得分:0)
将key.serializer和value.serializer更改为avro,如下所示。
props.put("key.serializer", io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put("value.serializer", io.confluent.kafka.serializers.KafkaAvroSerializer.class);
如果您正在使用架构注册表,请设置架构注册表URL
props.put("schema.registry.url", "http://localhost:8081");
注意: 将架构注册表URL设置为运行架构注册表的服务器,主题名称为“Kafka-value” 如果您没有使用上述内容,可以放弃使用。
答案 1 :(得分:0)
我假设您正在Windows上工作,或者至少在docker机器上工作,并且您在bootstrap_servers
和schema.registry.url
上设置的ip地址是docker machine ip地址。
尝试删除两个选项:ADVERTISED_HOST
和ADVERTISED_PORT
,通常在这种情况下,端口绑定就足够了。