Call dubbo interfaces and return to serialize objects, not the result I want it, why & how to do?

Case story:

(Hereinafter referred to as dubbo service provider as a "platform", dubbo caller to the service as "business")

identify the problem:

1, the phenomenon: calls the platform of this kind com.x.api.service.UserApiService of this interface, the resulting list is a result set, the result set with fastjson alibaba sequence into the string when there is a problem.

 2, the cause of the problem:

Platform development when changes to the old port, where there is a time assignment is not new a new object, causing the object before the List was repeated reference.

Dubbo single data returned from the interface to see there is no problem, just check the platform to test the correctness of the data returned, not concerned about whether the duplicate reference.

Business development call interface is used after FastJson serialization, "fastjson serialized object, if the situation repeated references to the existence of a sequence of results may not be the results we expected."

3, the solution:

Platform development - to avoid duplication of reference;

Business Development - closed circular reference detection parameters, JSON.toJSONString (res, SerializerFeature.DisableCircularReferenceDetect);

Platform testing - simulation of the business side of the operation sequence of test code, such problems can be found in advance.

problem analysis:

Dubbo as a highly scalable distributed service framework, when implementing rpc characteristics to provide a variety of serialization (Hessian2, Dubbo, the JSON ( FASTJSON implemented), JAVA, SOAP, kryo fst and the like) to achieve extended for business developers to choose according to the actual scene. (Sequence to achieve a variety of ways - in-depth understanding of the sequence of RPC chapter - summary article )

Q1: dubbo which pits the service consumer may encounter when using serialization? (Lessons learned can be shared to various business parties)

answer:

●“重复引用,序列化的结果往往不是我们预期的”,可以通过关闭循环引用检测参数的方法从业务端解决。(案例中的问题点即是该问题)。FastJson 反序列化注意事项 中还提到了其他可能的问题。

●如果用的是Hessian2序列化方式,可能会遇到的问题有浮点数反序列化问题子类覆盖父类字段hession反序列化获取不到

●ArrayList的子类在进行序列化时丢失属性的问题,可以通过对序列化类CollectionSerializer进行修改,同步修改相应的反序列化类解决问题

●Dubbo Hessian序列化参数为NULL问题,dubbo在采用hessian序列化时,一定要注意子类和父类不能有同名字段

●在使用dubbo时,HashMap不能作为Serializable的实例

小结:使用方在使用某种序列化方式时,应该了解该方式的特性,并考虑相关问题。比如,循环引用会不会出现问题 等等等等…. 

Q2:dubbo服务提供方在接口兼容性升级的时候有什么可以改进的地方?

答:

微服务接口兼容性升级之序列化

RPC接口设计中的考虑

Q3:平台测试能做些什么?

答:

序列化性能分析与测试dubbo典型协议、序列化方式组合性能对比测试——“在做性能分析和测试的时候我们并不单独处理每种序列化方式,而是把它们放到dubbo RPC中加以对比,因为这样更有现实意义。”

了解现在各个业务方的序列化方式,评估在测试代码中模拟哪种序列化操作(RPC为了追求可扩展性、性能等诸多因素,会支持多种序列化方式,测试时需要覆盖适当常用的序列化方式)

在测试代码中模拟业务的序列化操作(举例如下)

/***序列方式一:用 alibaba 的 fastjson **/

//在pom中添加依赖

<dependency>

    com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.2.31</version>

</dependency>

//
import com.alibaba.fastjson.JSON;

// List 转 json string

String jsonStr = JSON.toJSONString( list );

// Map 转 json string

String jsonStr = JSON.toJSONString( map );

 

发布了11 篇原创文章 · 获赞 7 · 访问量 1万+

Guess you like

Origin blog.csdn.net/sinat_16683257/article/details/82911517