线上事故回顾
前段时间同事新增了一个特别简单的功能,晚上上线前review
代码时想到公司拼搏进取的价值观临时他加一行 log 日志,觉得就一行简单的日志基本上没啥问题,结果刚上完线后一堆报警,赶紧回滚了代码,找到问题删除了添加日志的代码,重新上线完毕。
基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
情景还原
❝
定义了一个
CountryDTO
❞
publicclassCountryDTO{
privateStringcountry;
publicvoidsetCountry(Stringcountry){
this.country=country;
}
publicStringgetCountry(){
returnthis.country;
}
publicBooleanisChinaName(){
returnthis.country.equals("中国");
}
}
❝
定义测试类
FastJonTest
❞
publicclassFastJonTest{
@Test
publicvoidtestSerialize(){
CountryDTOcountryDTO=newCountryDTO();
Stringstr=JSON.toJSONString(countryDTO);
System.out.println(str);
}
}
运行时报空指针
错误:
![64775822-5d7b-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2KAZ0SRAAEcfD-zziE766.png)
通过报错信息可以看出来是序列化的过程中执行了isChinaName()
方法,这时候this.country
变量为空,那么问题来了:
-
序列化为什么会执行
isChinaName()
呢? - 引申一下,序列化过程中会执行那些方法呢?
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
源码分析
通过 debug 观察调用链路的堆栈信息
![652d8e80-5d7b-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2KAdxi7AAJB_eaS50g991.png)
![65920e46-5d7b-11ed-a3b6-dac502259ad0.jpg](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2KAWCTHAABc_vN5WP0323.jpg)
调用链中的ASMSerializer_1_CountryDTO.write
是FastJson
使用asm
技术动态生成了一个类ASMSerializer_1_CountryDTO
。
❝
asm技术其中一项使用场景就是通过到动态生成类用来代替
java
反射,从而避免重复执行时的反射开销❞
JavaBeanSerizlier序列化原理
通过下图看出序列化的过程中,主要是调用JavaBeanSerializer
类的write()
方法。
![65b3b3de-5d7b-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2KASviHAAE5yfaqKko951.png)
而JavaBeanSerializer
主要是通过getObjectWriter()
方法获取,通过对getObjectWriter()
执行过程的调试,找到比较关键的com.alibaba.fastjson.serializer.SerializeConfig#createJavaBeanSerializer
方法,进而找到 com.alibaba.fastjson.util.TypeUtils#computeGetters
publicstaticListcomputeGetters(Class>clazz,//
JSONTypejsonType,//
MapaliasMap,//
MapfieldCacheMap,//
booleansorted,//
PropertyNamingStrategypropertyNamingStrategy//
) {
//省略部分代码....
Method[]methods=clazz.getMethods();
for(Methodmethod:methods){
//省略部分代码...
if(method.getReturnType().equals(Void.TYPE)){
continue;
}
if(method.getParameterTypes().length!=0){
continue;
}
//省略部分代码...
JSONFieldannotation=TypeUtils.getAnnotation(method,JSONField.class);
//省略部分代码...
if(annotation!=null){
if(!annotation.serialize()){
continue;
}
if(annotation.name().length()!=0){
//省略部分代码...
}
}
if(methodName.startsWith("get")){
//省略部分代码...
}
if(methodName.startsWith("is")){
//省略部分代码...
}
}
}
从代码中大致分为三种情况:
-
@JSONField(.serialize = false, name = "xxx")
注解 -
getXxx()
: get开头的方法 -
isXxx()
:is开头的方法
序列化流程图
![65d61064-5d7b-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2OAZQjoAAGa8nGqgM8942.png)
示例代码
/**
*case1:@JSONField(serialize=false)
*case2:getXxx()返回值为void
*case3:isXxx()返回值不等于布尔类型
*case4:@JSONType(ignores="xxx")
*/
@JSONType(ignores="otherName")
publicclassCountryDTO{
privateStringcountry;
publicvoidsetCountry(Stringcountry){
this.country=country;
}
publicStringgetCountry(){
returnthis.country;
}
publicstaticvoidqueryCountryList(){
System.out.println("queryCountryList()执行!!");
}
publicBooleanisChinaName(){
System.out.println("isChinaName()执行!!");
returntrue;
}
publicStringgetEnglishName(){
System.out.println("getEnglishName()执行!!");
return"lucy";
}
publicStringgetOtherName(){
System.out.println("getOtherName()执行!!");
return"lucy";
}
/**
*case1:@JSONField(serialize=false)
*/
@JSONField(serialize=false)
publicStringgetEnglishName2(){
System.out.println("getEnglishName2()执行!!");
return"lucy";
}
/**
*case2:getXxx()返回值为void
*/
publicvoidgetEnglishName3(){
System.out.println("getEnglishName3()执行!!");
}
/**
*case3:isXxx()返回值不等于布尔类型
*/
publicStringisChinaName2(){
System.out.println("isChinaName2()执行!!");
return"isChinaName2";
}
}
运行结果为:
isChinaName()执行!!
getEnglishName()执行!!
{"chinaName":true,"englishName":"lucy"}
代码规范
可以看出来序列化的规则还是很多的,比如有时需要关注返回值,有时需要关注参数个数,有时需要关注@JSONType
注解,有时需要关注@JSONField
注解;当一个事物的判别方式有多种的时候,由于团队人员掌握知识点的程度不一样,这个方差很容易导致代码问题,所以尽量有一种推荐方案。
这里推荐使用@JSONField(serialize = false)
来显式的标注方法不参与序列化,下面是使用@JSONField
注解后的代码,是不是一眼就能看出来哪些方法不需要参与序列化了。
publicclassCountryDTO{
privateStringcountry;
publicvoidsetCountry(Stringcountry){
this.country=country;
}
publicStringgetCountry(){
returnthis.country;
}
@JSONField(serialize=false)
publicstaticvoidqueryCountryList(){
System.out.println("queryCountryList()执行!!");
}
publicBooleanisChinaName(){
System.out.println("isChinaName()执行!!");
returntrue;
}
publicStringgetEnglishName(){
System.out.println("getEnglishName()执行!!");
return"lucy";
}
@JSONField(serialize=false)
publicStringgetOtherName(){
System.out.println("getOtherName()执行!!");
return"lucy";
}
@JSONField(serialize=false)
publicStringgetEnglishName2(){
System.out.println("getEnglishName2()执行!!");
return"lucy";
}
@JSONField(serialize=false)
publicvoidgetEnglishName3(){
System.out.println("getEnglishName3()执行!!");
}
@JSONField(serialize=false)
publicStringisChinaName2(){
System.out.println("isChinaName2()执行!!");
return"isChinaName2";
}
}
三个频率高的序列化的情况
![65fbca66-5d7b-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/A8/wKgaomTnM2OAZWvlAAF0_wYBCPs037.png)
以上流程基本遵循,发现问题 --> 原理分析 --> 解决问题 --> 升华(编程规范)。
- 围绕业务上:解决问题 -> 如何选择一种好的额解决方案 -> 好的解决方式如何扩展 n 个系统应用;
- 围绕技术上:解决单个问题,顺着单个问题掌握这条线上的原理。
但其实这段代码我并不满意,原因是和 FastJson 依赖太高了。我想要的效果是,不依赖任何特定的 JSON 序列化框架。当我需要替换掉它的时候,随时可以替换掉。
并且在写代码时,不要过于依赖日志。打日志只需要打紧要且关键的信息即可,不要什么日志都打,我曾见过一个系统,一个小时,把 128G 磁盘跑满的管理系统。几乎没啥并发,但几乎每个请求都输出几 M 的日志,这件事我后面会单独拿出来讲讲。
关于@JSONField
和@JSONType
等特性注解,后面我会在团队内规范并给出新的解耦方案,把它们移除掉。
审核编辑 :李倩
-
代码
+关注
关注
30文章
4841浏览量
69196 -
日志
+关注
关注
0文章
139浏览量
10690
原文标题:一行log日志,引发了P1的线上事故
文章出处:【微信号:芋道源码,微信公众号:芋道源码】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
Linux实时查看日志的四种命令详解
![Linux实时查看<b class='flag-5'>日志</b>的四种命令详解](https://file1.elecfans.com/web3/M00/05/B8/wKgZPGeEft6AfYW9AAAvPO3y9mw633.png)
丰田汽车一行到访中汽中心
玩转Nginx日志管理:高效排查问题的终极指南
TFP401AMP第一行丢失4个像素是什么原因导致的?
泰国国家石油一行到访商汤科技
政府关怀 | 省、市、区领导干部一行莅临鑫金晖进行参观调研
![政府关怀 | 省、市、区领导干部<b class='flag-5'>一行</b>莅临鑫金晖进行参观调研](https://file1.elecfans.com/web2/M00/8A/8D/wKgZomSXkWOAIOXWAAAyyy96BQY337.png)
软通动力领导一行访问福州大学
RIMAC与IMD一行来访声扬科技,共话AI语音赋能产业升级
![RIMAC与IMD<b class='flag-5'>一行</b>来访声扬科技,共话AI语音赋能产业升级](https://file1.elecfans.com/web2/M00/09/9E/wKgaomb6EvWAM_0lAAAZ7K9FAYo060.png)
评论