New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How should I send/receive protobuf message? #727
Comments
You are using |
@nyilmaz thank you for reply, I want to use |
Now I define two class: public class ProtoSerializer extends Adapter implements Serializer<Person> {
private static final Logger logger = LoggerFactory.getLogger(ProtoSerializer.class);
@Override
public byte[] serialize(final String topic, final Person data) {
logger.info("Person serialize ...");
return data.toByteArray();
}
}
public class ProtoDeserializer extends Adapter implements Deserializer<Event> {
private static final Logger logger = LoggerFactory.getLogger(ProtoDeserializer.class);
@Override
public Event deserialize(final String topic, byte[] data) {
logger.info("Event deserialize ...");
try {
return Event.parseFrom(data);
} catch (final InvalidProtocolBufferException e) {
logger.error("Received unparseable message", e);
throw new RuntimeException("Received unparseable message " + e.getMessage(), e);
}
}
} And update spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=mock-test
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
# HERE update
spring.kafka.consumer.value-deserializer=com.thinkerou.test.kafka.ProtoDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
# HERE update
spring.kafka.producer.value-serializer=com.thinkerou.test.kafka.ProtoSerializer When I send one message, it will print debug log And run it still error: org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.thinkerou.test.kafka.Receiver.listen(org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.Long, com.thinkerou.test.Event>) throws java.lang.InterruptedException' threw exception; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to com.thinkerou.test.Event |
Look at the consumer config in the log to make sure the deserializer is correct
|
@garyrussell thank you for reply. 2018-07-06 21:17:03.510 INFO 83794 --- [ main] o.a.k.clients.consumer.ConsumerConfig : ConsumerConfig values:
auto.commit.interval.ms = 5000
auto.offset.reset = latest
bootstrap.servers = [localhost:9092]
check.crcs = true
client.id =
connections.max.idle.ms = 540000
enable.auto.commit = true
exclude.internal.topics = true
fetch.max.bytes = 52428800
fetch.max.wait.ms = 500
fetch.min.bytes = 1
group.id = media-cloud-mock-test
heartbeat.interval.ms = 3000
interceptor.classes = null
internal.leave.group.on.close = true
isolation.level = read_uncommitted
key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
max.partition.fetch.bytes = 1048576
max.poll.interval.ms = 300000
max.poll.records = 500
metadata.max.age.ms = 300000
metric.reporters = []
metrics.num.samples = 2
metrics.recording.level = INFO
metrics.sample.window.ms = 30000
partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
receive.buffer.bytes = 65536
reconnect.backoff.max.ms = 1000
reconnect.backoff.ms = 50
request.timeout.ms = 305000
retry.backoff.ms = 100
sasl.jaas.config = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.min.time.before.relogin = 60000
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
sasl.kerberos.ticket.renew.window.factor = 0.8
sasl.mechanism = GSSAPI
security.protocol = PLAINTEXT
send.buffer.bytes = 131072
session.timeout.ms = 10000
ssl.cipher.suites = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.endpoint.identification.algorithm = null
ssl.key.password = null
ssl.keymanager.algorithm = SunX509
ssl.keystore.location = null
ssl.keystore.password = null
ssl.keystore.type = JKS
ssl.protocol = TLS
ssl.provider = null
ssl.secure.random.implementation = null
ssl.trustmanager.algorithm = PKIX
ssl.truststore.location = null
ssl.truststore.password = null
ssl.truststore.type = JKS
value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
2018-07-06 21:17:03.590 INFO 83794 --- [ main] o.a.kafka.common.utils.AppInfoParser : Kafka version : 1.0.1 I find |
Then you need to double check your application.properties; perhaps some other version of the file is picked up. When I copy/paste your |
@garyrussell thanks....a lot, one typo error!!! |
For example, I have the following protobuf:
And
application.properties
:And
Sender.java
:when kafka receive Sender message after, it will do something and return
Event
message.And
Receiver.java
:the project can compile success, but run it will error:
The text was updated successfully, but these errors were encountered: