1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > dubbo源码分析-dubbo-serialization

dubbo源码分析-dubbo-serialization

时间:2022-01-25 06:07:15

相关推荐

dubbo源码分析-dubbo-serialization

dubbo-serialization

dubbo-serialization是dubbo中实现序列化相关的代码。

共5种序列化方式,可从名字直接看出含义,这里不再赘述。

dubbo-serialization-fastjsondubbo-serialization-fstdubbo-serialization-hessian2dubbo-serialization-jdkdubbo-serialization-kryo

dubbo-serialization-api

dubbo-serialization-api是底层实现,上述5种序列化方式均封装dubbo-serialization-api。

其中SerializableClassRegistry和SerializationOptimizer两个接口,未在源码中找到实现和引用。

Cleanable

Cleanable是供Kryo序列化方式使用,源码如下:

package org.mon.serialize;public interface Cleanable {void cleanup();}

在KryoObjectInput中实现如下:

@Overridepublic void cleanup() {KryoUtils.release(kryo);kryo = null;}

DataInput

DataInput实现了基本类型的读取。

package org.mon.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;

}

DataOutput

DataOutput实现了基本类型的写入。

package org.mon.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;

}

ObjectInput

ObjectInput实现了对象的读取,继承DataInput。

package org.mon.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;

}

ObjectOutput

ObjectOutput实现了对象的写入,继承DataOutput。

package org.mon.serialize;

import java.io.IOException;

/**

Object output.

*/

public interface ObjectOutput extends DataOutput {

/**

write object.@param obj object.

*/

void writeObject(Object obj) throws IOException;

}

Serialization

Serialization序列化接口。

package org.mon.serialize;

import org.mon.URL;

import org.mon.extension.Adaptive;

import org.mon.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

用最简单的dubbo-serialization-fastjson来实例分析。

FastJsonSerialization

package org.mon.serialize.fastjson;

import org.mon.URL;

import org.mon.serialize.ObjectInput;

import org.mon.serialize.ObjectOutput;

import org.mon.serialize.Serialization;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class FastJsonSerialization implements Serialization {

@Overridepublic byte getContentTypeId() {return 6;}@Overridepublic String getContentType() {return "text/json";}// 序列化@Overridepublic ObjectOutput serialize(URL url, OutputStream output) throws IOException {// 返回FastJsonObjectOutput实例return new FastJsonObjectOutput(output);}// 反序列化@Overridepublic ObjectInput deserialize(URL url, InputStream input) throws IOException {// 返回FastJsonObjectInput实例return new FastJsonObjectInput(input);}

}

FastJsonObjectInput

FastJsonObjectInput,文件读取,主要方法如下:

@Override

public Object readObject() throws IOException, ClassNotFoundException {

String json = readLine();

return JSON.parse(json);

}

@Override@SuppressWarnings("unchecked")public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {Object value = readObject(cls);return (T) PojoUtils.realize(value, cls, type);}private <T> T read(Class<T> cls) throws IOException {String json = readLine();return JSON.parseObject(json, cls);}

FastJsonObjectOutput

FastJsonObjectOutput,文件写入,主要方法如下:

@Overridepublic 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 bufwriter.println();writer.flush();}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。