dubbo进行PRC调用,例如消费者请求提供者的数据,使用hessian序列化时,对于Set,Map,List集合,PersistentSet,PersistentMap,PersistentCollection等对象无法识别,所以会报错。
例如,实体类中包含Set,Map等数据:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "person", joinColumns = {@JoinColumn(name = "company_id")},
inverseJoinColumns = {@JoinColumn(name = "person_id")})
private Set<Person> personSet;
消费者调用时,错误如下:
com.alibaba.com.caucho.hessian.io.HessianFieldException: com.test.core.entity.Company.personSet: java.util.Set cannot be assigned from null
at com.alibaba.com.caucho.hessian.io.JavaDeserializer.logDeserializeError(JavaDeserializer.java:173)
at com.alibaba.com.caucho.hessian.io.JavaDeserializer$ObjectFieldDeserializer.deserialize(JavaDeserializer.java:410)
at com.alibaba.com.caucho.hessian.io.JavaDeserializer.readObject(JavaDeserializer.java:276)
at com.alibaba.com.caucho.hessian.io.JavaDeserializer.readObject(JavaDeserializer.java:203)
at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObjectInstance(Hessian2Input.java:2818)
at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2145)
at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2074)
at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2118)
at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2074)
at org.apache.dubbo.common.serialize.hessian2.Hessian2ObjectInput.readObject(Hessian2ObjectInput.java:92)
at org.apache.dubbo.common.serialize.hessian2.Hessian2ObjectInput.readObject(Hessian2ObjectInput.java:97)
at org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcResult.handleValue(DecodeableRpcResult.java:134)
at org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcResult.decode(DecodeableRpcResult.java:92)
at org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcResult.decode(DecodeableRpcResult.java:112)
at org.apache.dubbo.rpc.protocol.dubbo.DubboCodec.decodeBody(DubboCodec.java:92)
at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.decode(ExchangeCodec.java:122)
at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.decode(ExchangeCodec.java:82)
at org.apache.dubbo.rpc.protocol.dubbo.DubboCountCodec.decode(DubboCountCodec.java:48)
at org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter$InternalDecoder.decode(NettyCodecAdapter.java:90)
使用dubbo序列化扩展,自定义序列化,增加对持久化对象的识别判断,具体配置可查看dubbo官网 序列化扩展
import com.alibaba.com.caucho.hessian.io.*;
import org.hibernate.Hibernate;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.collection.internal.PersistentSet;
import java.io.IOException;
import java.util.*;
public class PersistentSerializerFactory extends SerializerFactory{
public static final SerializerFactory TESTSERIALIZER_FACTORY = new PersistentSerializerFactory();
public PersistentSerializerFactory() {
}
public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
private HibernateListSerializer listSerializer = new HibernateListSerializer();
private HibernateMapSerializer mapSerializer = new HibernateMapSerializer();
private HibernateBeanSerializer hibernateBeanSerializer = new HibernateBeanSerializer();
private HibernateSetSerializer setSerializer = new HibernateSetSerializer();
@SuppressWarnings("rawtypes")
//序列化时,判断persistent相关类型,添加持久化对象的序列化方式,如果不是,则走父类的序列化方法
public Serializer getSerializer(Class cl) throws HessianProtocolException {
if (PersistentMap.class.isAssignableFrom(cl)) {
return mapSerializer;
} else if(PersistentSet.class.isAssignableFrom(cl)){
return setSerializer;
} else if (AbstractPersistentCollection.class.isAssignableFrom(cl)) {
return listSerializer;
} else if (cl.getSimpleName().contains("_$$_javassist_")) {
return hibernateBeanSerializer;
}
return super.getSerializer(cl);
}
private static class HibernateBeanSerializer implements Serializer {
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
boolean init = Hibernate.isInitialized(obj);
out.writeObject(init ? obj : null);
out.flush();
return;
}
}
private static class HibernateSetSerializer implements Serializer{
private CollectionSerializer delegate = new CollectionSerializer();
@SuppressWarnings({"unchecked", "rawtypes"})
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (Hibernate.isInitialized(obj)) {
delegate.writeObject(new ArrayList((Set) obj), out);
} else {
delegate.writeObject(new ArrayList(), out);
}
}
}
private static class HibernateListSerializer implements Serializer {
private CollectionSerializer delegate = new CollectionSerializer();
@SuppressWarnings({"unchecked", "rawtypes"})
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (Hibernate.isInitialized(obj)) {
delegate.writeObject(new ArrayList((Collection) obj), out);
} else {
delegate.writeObject(new ArrayList(), out);
}
}
}
private static class HibernateMapSerializer implements Serializer {
private MapSerializer delegate = new MapSerializer();
@SuppressWarnings({"unchecked", "rawtypes"})
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (Hibernate.isInitialized(obj)) {
delegate.writeObject(new HashMap((Map) obj), out);
} else {
delegate.writeObject(new HashMap(), out);
}
}
}
}
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 org.apache.dubbo.common.serialize.hessian2.Hessian2ObjectInput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class PersistentSerialization implements Serialization {
@Override
//注意,由于dubbo有很多序列化方式的扩展,每种方式有自己的id,自定义id时注意冲突,重复时会有ERROR提示
public byte getContentTypeId() {
return 22;
}
@Override
public String getContentType() {
return "persistent";
}
@Override
public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
return new PersistentObjectOutput(out);
}
@Override
public ObjectInput deserialize(URL url, InputStream is) throws IOException {
return new Hessian2ObjectInput(is);
}
}
import com.alibaba.com.caucho.hessian.io.Hessian2Output;
import org.apache.dubbo.common.serialize.ObjectOutput;
import java.io.IOException;
import java.io.OutputStream;
public class PersistentObjectOutput implements ObjectOutput{
private final Hessian2Output mH2o;
//注意:设置Output的Factory为上面自己定义的序列化工厂类
public PersistentObjectOutput(OutputStream os) {
this.mH2o = new Hessian2Output(os);
this.mH2o.setSerializerFactory(PersistentSerializerFactory.TESTSERIALIZER_FACTORY);
}
public void writeBool(boolean v) throws IOException {
this.mH2o.writeBoolean(v);
}
public void writeByte(byte v) throws IOException {
this.mH2o.writeInt(v);
}
public void writeShort(short v) throws IOException {
this.mH2o.writeInt(v);
}
public void writeInt(int v) throws IOException {
this.mH2o.writeInt(v);
}
public void writeLong(long v) throws IOException {
this.mH2o.writeLong(v);
}
public void writeFloat(float v) throws IOException {
this.mH2o.writeDouble((double)v);
}
public void writeDouble(double v) throws IOException {
this.mH2o.writeDouble(v);
}
public void writeBytes(byte[] b) throws IOException {
this.mH2o.writeBytes(b);
}
public void writeBytes(byte[] b, int off, int len) throws IOException {
this.mH2o.writeBytes(b, off, len);
}
public void writeUTF(String v) throws IOException {
this.mH2o.writeString(v);
}
public void writeObject(Object obj) throws IOException {
this.mH2o.writeObject(obj);
}
public void flushBuffer() throws IOException {
this.mH2o.flushBuffer();
}
}
src
|-main
|-java
|-com
|-xxx
|-XxxSerialization.java (实现Serialization接口)
|-XxxObjectInput.java (实现ObjectInput接口)
|-XxxObjectOutput.java (实现ObjectOutput接口)
|-resources
|-META-INF
|-dubbo
|-org.apache.dubbo.common.serialize.Serialization (纯文本文件,内容为:xxx=com.xxx.XxxSerialization)
META-INF/dubbo/org.apache.dubbo.common.serialize.Serialization文件内容:
persistent=com.xxx.persistentSerialization
dubbo:
protocol:
serialization: persistent
消费者进行调用时,会进入自定义的序列化工厂方法中,判断是否为持久化对象,如果是,则通过自定义序列化返回相应对象,如果不是,则进入父类的原生方法中进行序列化。