welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ群:162333776 邮箱:admin@xlongwei.com

dubbo返回结果不序列化会是怎样


分类 Java   关键字 分享   标签 java   web   dubbo   发布 hongwei  1446185368273
注意 转载须保留原文链接,译文链接,作者译者等信息。  
针对Result dubbo接口返回类型的设计有人回复返回值不必可序列化,笔者这里就姑且测试一下发现确实会出异常。异常消 息:java.lang.IllegalStateException: Serialized class com.itecheast.ite.domain.entity.TestEntity must implement java.io.Serializable
    at com.alibaba.com.caucho.hessian.io.SerializerFactory.getDefaultSerializer(SerializerFactory.java:261)

先来看Entity类型,添加不可序列化的source来引用另一个同类对象,覆盖toString友好输出自己
public class TestEntity {
private TestEntity source;
public TestEntity getSource() {
return source;
}
public void setSource(TestEntity source) {
this.source = source;
}
@Override
public String toString() {
return source!=null ? super.toString()+"[source="+source.toString()+"]" : super.toString();
}
}

再来看下接口和实现
public interface TestService {
TestEntity testMethod(TestEntity testEntity);
}
@Service("testService")
public class TestServiceImpl implements TestService {
@Override
public TestEntity testMethod(TestEntity testEntity) {
TestEntity result = new TestEntity();
result.setSource(testEntity);
return result;
}
}

提供方配置及注册服务,详细配置见:provider.xml
<dubbo:service ref="testService" interface="com.itecheast.ite.domain.service.TestService" />
<dubbo:registry address="redis://127.0.0.1:6379"/>
@ContextConfiguration(locations = { "classpath:provider.xml" })
public class Provider extends BaseRepositoryTester {
@Test
public void provide() throws IOException {
System.in.read();
System.out.println("Provider Exit");
}
}

消费方配置及订阅服务,详见:consumer.xmlConsumer
<dubbo:reference id="testService" interface="com.itecheast.ite.domain.service.TestService" />
<dubbo:registry address="redis://127.0.0.1:6379"/>
public class TestServiceTester extends Consumer {
@Autowired TestService testService;
@Test public void test() {
TestEntity testEntity = testService.testMethod(null);
System.out.println(testEntity);
testEntity = testService.testMethod(testEntity);
System.out.println(testEntity);
}
}

注册中心:zookeeper比较麻烦,还是redis方便,http://yunpan.cn/cF5jGmqLWIzuq  访问密码 186c,搜索redis_win.zip,解压后运行redis-server.exe。异常:



改造一下再看:
1,去掉TestEntity source字段,同样的异常
2,实现Serializable接口,测试通过

3,dubbo提供的TestService里的返回值:void,可以没有返回值;int、double,这些会自动包装;String、Enum,可序列化的;总之:返回值和接口参数,需要在client和server之间传递,总会有序列化和编码解码。