dubbo-serialization是dubbo中实现序列化相关的代码。
共5种序列化方式,可从名字直接看出含义,这里不再赘述。
dubbo-serialization-api是底层实现,上述5种序列化方式均封装dubbo-serialization-api。
其中SerializableClassRegistry和SerializationOptimizer两个接口,未在源码中找到实现和引用。
Cleanable是供Kryo序列化方式使用,源码如下:
package org.apache.dubbo.common.serialize;
public interface Cleanable {
void cleanup();
}
在KryoObjectInput中实现如下:
@Override
public void cleanup() {
KryoUtils.release(kryo);
kryo = null;
}
package org.apache.dubbo.common.serialize;
import java.io.IOException;
/**
* Data input.
*/
public interface DataInput {
/**
* Read boolean.
*
* @return boolean.
* @throws IOException
*/
boolean readBool() throws IOException;
/**
* Read byte.
*
* @return byte value.
* @throws IOException
*/
byte readByte() throws IOException;
/**
* Read short integer.
*
* @return short.
* @throws IOException
*/
short readShort() throws IOException;
/**
* Read integer.
*
* @return integer.
* @throws IOException
*/
int readInt() throws IOException;
/**
* Read long.
*
* @return long.
* @throws IOException
*/
long readLong() throws IOException;
/**
* Read float.
*
* @return float.
* @throws IOException
*/
float readFloat() throws IOException;
/**
* Read double.
*
* @return double.
* @throws IOException
*/
double readDouble() throws IOException;
/**
* Read UTF-8 string.
*
* @return string.
* @throws IOException
*/
String readUTF() throws IOException;
/**
* Read byte array.
*
* @return byte array.
* @throws IOException
*/
byte[] readBytes() throws IOException;
}
package org.apache.dubbo.common.serialize;
import java.io.IOException;
/**
* Data output.
*/
public interface DataOutput {
/**
* Write boolean.
*
* @param v value.
* @throws IOException
*/
void writeBool(boolean v) throws IOException;
/**
* Write byte.
*
* @param v value.
* @throws IOException
*/
void writeByte(byte v) throws IOException;
/**
* Write short.
*
* @param v value.
* @throws IOException
*/
void writeShort(short v) throws IOException;
/**
* Write integer.
*
* @param v value.
* @throws IOException
*/
void writeInt(int v) throws IOException;
/**
* Write long.
*
* @param v value.
* @throws IOException
*/
void writeLong(long v) throws IOException;
/**
* Write float.
*
* @param v value.
* @throws IOException
*/
void writeFloat(float v) throws IOException;
/**
* Write double.
*
* @param v value.
* @throws IOException
*/
void writeDouble(double v) throws IOException;
/**
* Write string.
*
* @param v value.
* @throws IOException
*/
void writeUTF(String v) throws IOException;
/**
* Write byte array.
*
* @param v value.
* @throws IOException
*/
void writeBytes(byte[] v) throws IOException;
/**
* Write byte array.
*
* @param v value.
* @param off offset.
* @param len length.
* @throws IOException
*/
void writeBytes(byte[] v, int off, int len) throws IOException;
/**
* Flush buffer.
*
* @throws IOException
*/
void flushBuffer() throws IOException;
}
package org.apache.dubbo.common.serialize;
import java.io.IOException;
import java.lang.reflect.Type;
/**
* Object input.
*/
public interface ObjectInput extends DataInput {
/**
* read object.
*
* @return object.
*/
Object readObject() throws IOException, ClassNotFoundException;
/**
* read object.
*
* @param cls object type.
* @return object.
*/
T readObject(Class cls) throws IOException, ClassNotFoundException;
/**
* read object.
*
* @param cls object type.
* @return object.
*/
T readObject(Class cls, Type type) throws IOException, ClassNotFoundException;
}
package org.apache.dubbo.common.serialize;
import java.io.IOException;
/**
* Object output.
*/
public interface ObjectOutput extends DataOutput {
/**
* write object.
*
* @param obj object.
*/
void writeObject(Object obj) throws IOException;
}
package org.apache.dubbo.common.serialize;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Adaptive;
import org.apache.dubbo.common.extension.SPI;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Serialization. (SPI, Singleton, ThreadSafe)
*/
@SPI("hessian2")
public interface Serialization {
/**
* get content type id
*
* @return content type id
*/
byte getContentTypeId();
/**
* get content type
*
* @return content type
*/
String getContentType();
/**
* create serializer
*
* @param url
* @param output
* @return serializer
* @throws IOException
*/
@Adaptive
ObjectOutput serialize(URL url, OutputStream output) throws IOException;
/**
* create deserializer
*
* @param url
* @param input
* @return deserializer
* @throws IOException
*/
@Adaptive
ObjectInput deserialize(URL url, InputStream input) throws IOException;
}
用最简单的dubbo-serialization-fastjson来实例分析。
package org.apache.dubbo.common.serialize.fastjson;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FastJsonSerialization implements Serialization {
@Override
public byte getContentTypeId() {
return 6;
}
@Override
public String getContentType() {
return "text/json";
}
// 序列化
@Override
public ObjectOutput serialize(URL url, OutputStream output) throws IOException {
// 返回FastJsonObjectOutput实例
return new FastJsonObjectOutput(output);
}
// 反序列化
@Override
public ObjectInput deserialize(URL url, InputStream input) throws IOException {
// 返回FastJsonObjectInput实例
return new FastJsonObjectInput(input);
}
}
@Override
public Object readObject() throws IOException, ClassNotFoundException {
String json = readLine();
return JSON.parse(json);
}
@Override
@SuppressWarnings("unchecked")
public T readObject(Class cls, Type type) throws IOException, ClassNotFoundException {
Object value = readObject(cls);
return (T) PojoUtils.realize(value, cls, type);
}
private T read(Class cls) throws IOException {
String json = readLine();
return JSON.parseObject(json, cls);
}
@Override
public void writeObject(Object obj) throws IOException {
SerializeWriter out = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(out);
serializer.config(SerializerFeature.WriteEnumUsingToString, true);
serializer.write(obj);
out.writeTo(writer);
out.close(); // for reuse SerializeWriter buf
writer.println();
writer.flush();
}