1. Problem description
ZK makes an error when reading Kafka’s broker information, as follows:
22:52:47,125 DEBUG ClientCnxn:846 - Reading reply sessionid:0x10b00878c620372, packet:: clientPath:null serverPath:null finished:false header:: 1,4 replyHeader:: 1,2793,0 request:: '/myKafka/brokers/ids/0,F response:: #7b226c697374656e65725f73656375726974795f70726f746f636f6c5f6d6170223a7b22504c41494e54455854223a22504c41494e54455854227d2c22656e64706f696e7473223a5b22504c41494e544558543a2f2f63756c2d746f757269736d2d303030383a38303835225d2c226a6d785f706f7274223a393538312c22686f7374223a2263756c2d746f757269736d2d30303038222c2274696d657374616d70223a2231363339333131303131363136222c22706f7274223a383038352c2276657273696f6e223a347d,s{2649,2649,1639311011619,1639311011619,0,0,0,75154400957563721,204,0,2649}
Exception in thread "main" org.I0Itec.zkclient.exception.ZkMarshallingError: java.io.StreamCorruptedException: invalid stream header: 7B226C69
at org.I0Itec.zkclient.serialize.SerializableSerializer.deserialize(SerializableSerializer.java:37)
at org.I0Itec.zkclient.ZkClient.derializable(ZkClient.java:740)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:773)
at org.I0Itec.zkclient.ZkClient.readData(ZkClient.java:761)
at com.lagou.zk.demo.Demo.main(Demo.java:33)
Caused by: java.io.StreamCorruptedException: invalid stream header: 7B226C69
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:936)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:394)
at org.I0Itec.zkclient.serialize.SerializableSerializer.deserialize(SerializableSerializer.java:31)
... 4 more
2. Solutions
According to the error message, an error occurs during deserialization. Here, we use the custom serialization type
public class ZkStringSerializer implements ZkSerializer {
@Override
public byte[] serialize(Object data) throws ZkMarshallingError {
return String.valueOf(data).getBytes();
}
@Override
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
return new String(bytes);
}
}
Set after zkclient is created
ZkClient zkClient = new ZkClient("10.7.2.55:883");
// Set custom serialization type
zkClient.setZkSerializer(new ZkStringSerializer());
Done!
Similar Posts:
- java.io.StreamCorruptedException: invalid stream header: 00000000
- Object deserialization, throw java.io.streamcorruptedexception: invalid type code: AC exception
- Postman passing Josn field error: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
- Java – Stream has already been operated upon or closed
- Java – Convert bytes to unsigned bytes
- java.net.SocketException: socket closed
- IDEA compile error: sun.misc.Base64decoder upgrade processing
- Failed to deserialize — local class incompatible: stream classdesc serialVersionUID [Solved]
- Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘userDaoImpl’ defined in file
- Chrome failed to download word