前言:kryo是個高效的java序列化/反序列化庫,目前Twitter、yahoo、Apache、strom等等在使用該技術,好比Apache的spark、hive等大數據領域用的較多。java
爲何使用kryo而不是其餘?
由於性能足夠好。比kyro更高效的序列化庫就只有google的protobuf了(並且二者性能很接近),protobuf有個缺點就是要傳輸的每個類的結構都要生成對應的proto文件(也能夠都放在同一個proto文件中,若是考慮到擴展性的話,不建議放在一個proto文件中),若是某個類發生修改,還得從新生成該類對應的proto文件;另外考慮到項目中用的所有是java技術棧,不存在不一樣編程語言間的兼容性問題,所以最終採用了kryo做爲序列化庫。redis
使用場景:(數據交換或數據持久化)好比使用kryo把對象序列化成字節數組發送給消息隊列或者放到redis等nosql中等等應用場景。sql
注意:因爲kryo不是線程安全的,針對多線程狀況下的使用,要對kryo進行一個簡單的封裝設計,從而能夠多線程安全的使用序列化和反序列化編程
序列化和反序列化接口設計
-
-
-
-
-
-
public interface Serializer{
-
-
-
-
-
-
-
public void serialize(Object t,byte[] bytes);
-
-
-
-
-
-
-
-
-
public void serialize(Object obj, byte[] bytes, int offset, int count);
-
-
-
-
-
-
-
public <T>T deserialize(byte[] bytes);
-
-
-
-
-
-
-
-
-
-
public <T>T deserialize(byte[] bytes, int offset, int count);
-
-
使用kryo實現上面的接口
-
-
-
-
-
-
-
public class kryoSerializer implements Serializer {
-
-
-
final ThreadLocal<Kryo> kryoLocal = new ThreadLocal<Kryo>() {
-
-
protected Kryo initialValue() {
-
-
kryo.register(ct,
new BeanSerializer<>(kryo, ct));
-
-
-
-
final ThreadLocal<Output> outputLocal = new ThreadLocal<Output>();
-
final ThreadLocal<Input> inputLocal = new ThreadLocal<Input>();
-
private Class<?> ct = null;
-
-
public kryoSerializer(Class<?> ct) {
-
-
-
-
public Class<?> getCt() {
-
-
-
-
public void setCt(Class<?> ct) {
-
-
-
-
-
public void serialize(Object obj, byte[] bytes) {
-
-
Output output = getOutput(bytes);
-
kryo.writeObjectOrNull(output, obj, obj.getClass());
-
-
-
-
-
public void serialize(Object obj, byte[] bytes, int offset, int count) {
-
-
Output output = getOutput(bytes, offset, count);
-
kryo.writeObjectOrNull(output, obj, obj.getClass());
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private Output getOutput(byte[] bytes) {
-
-
if ((output = outputLocal.get()) == null) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
private Output getOutput(byte[] bytes, int offset, int count) {
-
-
if ((output = outputLocal.get()) == null) {
-
-
-
-
-
output.writeBytes(bytes, offset, count);
-
-
-
-
-
-
-
-
-
-
-
-
-
private Input getInput(byte[] bytes, int offset, int count) {
-
-
if ((input = inputLocal.get()) == null) {
-
-
-
-
-
input.setBuffer(bytes, offset, count);
-
-
-
-
-
@SuppressWarnings("unchecked")
-
-
public <T> T deserialize(byte[] bytes, int offset, int count) {
-
-
Input input = getInput(bytes, offset, count);
-
return (T) kryo.readObjectOrNull(input, ct);
-
-
-
-
public <T> T deserialize(byte[] bytes) {
-
return deserialize(bytes, 0, bytes.length);
-
測試一下kryo的序列化和反序列化
爲何使用納秒,而不用毫秒?與java原生的序列化反序列化要耗時幾毫秒不一樣,kryo序列化和反序列化太快了,單個對象的序列化反序列化速度都在0.0x毫秒左右(若是電腦性能更好的話,會更快)數組
-
Serializer ser =
new kryoSerializer(Msg.class);
-
for (int i = 0; i < 10; i++) {
-
-
-
-
msg.setVersion_flag(
new byte[] { 1, 2, 3 });
-
msg.setCrc_code((
short) 1);
-
msg.setMsg_body(
new byte[] { 123, 123, 123, 43, 42, 1, 12, 45, 57, 98 });
-
byte[] bytes = new byte[300];
-
long start = System.nanoTime();
-
ser.serialize(msg, bytes);
-
System.err.println(
"序列化耗時:" + (System.nanoTime() - start));
-
-
System.out.println(Arrays.toString(bytes));
-
-
-
start = System.nanoTime();
-
newmsg = ser.deserialize(bytes);
-
System.err.println(
"反序列化耗時:" + (System.nanoTime() - start));
-
System.out.println(newmsg);
-
----end----安全