就加了一行log日志,结果引发了 P1 线上事故...
作者:老鹰汤
来源:https://juejin.cn/post/7156439842958606349
线上事故回顾
前段时间新增一个特别简单的功能,晚上上线前review
代码时想到公司拼搏进取的价值观临时加一行log日志,觉得就一行简单的日志基本上没啥问题,结果刚上完线后一堆报警,赶紧回滚了代码,找到问题删除了添加日志的代码,重新上线完毕。
情景还原
定义了一个
CountryDTO
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
public Boolean isChinaName() {
return this.country.equals("中国");
}
}
复制代码
定义测试类
FastJonTest
public class FastJonTest {
@Test
public void testSerialize() {
CountryDTO countryDTO = new CountryDTO();
String str = JSON.toJSONString(countryDTO);
System.out.println(str);
}
}
复制代码
运行时报空指针
错误:
通过报错信息可以看出来是 序列化的过程中执行了 isChinaName()
方法,这时候this.country
变量为空, 那么问题来了:
- 序列化为什么会执行
isChinaName()
呢? - 引申一下,序列化过程中会执行那些方法呢?
源码分析
通过debug观察调用链路的堆栈信息
调用链中的ASMSerializer_1_CountryDTO.write
是FastJson
使用asm
技术动态生成了一个类ASMSerializer_1_CountryDTO
,
asm技术其中一项使用场景就是通过到动态生成类用来代替
java
反射,从而避免重复执行时的反射开销
JavaBeanSerizlier序列化原理
通过下图看出序列化的过程中,主要是调用JavaBeanSerializer
类的write()
方法。
而JavaBeanSerializer
主要是通过 getObjectWriter()
方法获取,通过对getObjectWriter()
执行过程的调试,找到比较关键的com.alibaba.fastjson.serializer.SerializeConfig#createJavaBeanSerializer
方法,进而找到 com.alibaba.fastjson.util.TypeUtils#computeGetters
public static List<FieldInfo> computeGetters(Class<?> clazz, //
JSONType jsonType, //
Map<String,String> aliasMap, //
Map<String,Field> fieldCacheMap, //
boolean sorted, //
PropertyNamingStrategy propertyNamingStrategy //
){
//省略部分代码....
Method[] methods = clazz.getMethods();
for(Method method : methods){
//省略部分代码...
if(method.getReturnType().equals(Void.TYPE)){
continue;
}
if(method.getParameterTypes().length != 0){
continue;
}
//省略部分代码...
JSONField annotation = 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开头的方法
序列化流程图
序列化.png
示例代码
/**
* case1: @JSONField(serialize = false)
* case2: getXxx()返回值为void
* case3: isXxx()返回值不等于布尔类型
* case4: @JSONType(ignores = "xxx")
*/
@JSONType(ignores = "otherName")
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
public static void queryCountryList() {
System.out.println("queryCountryList()执行!!");
}
public Boolean isChinaName() {
System.out.println("isChinaName()执行!!");
return true;
}
public String getEnglishName() {
System.out.println("getEnglishName()执行!!");
return "lucy";
}
public String getOtherName() {
System.out.println("getOtherName()执行!!");
return "lucy";
}
/**
* case1: @JSONField(serialize = false)
*/
@JSONField(serialize = false)
public String getEnglishName2() {
System.out.println("getEnglishName2()执行!!");
return "lucy";
}
/**
* case2: getXxx()返回值为void
*/
public void getEnglishName3() {
System.out.println("getEnglishName3()执行!!");
}
/**
* case3: isXxx()返回值不等于布尔类型
*/
public String isChinaName2() {
System.out.println("isChinaName2()执行!!");
return "isChinaName2";
}
}
复制代码
运行结果为:
isChinaName()执行!!
getEnglishName()执行!!
{"chinaName":true,"englishName":"lucy"}
复制代码
代码规范
可以看出来序列化的规则还是很多的,比如有时需要关注返回值,有时需要关注参数个数,有时需要关注@JSONType
注解,有时需要关注@JSONField
注解;当一个事物的判别方式有多种的时候,由于团队人员掌握知识点的程度不一样,这个方差很容易导致代码问题,所以尽量有一种推荐方案。 这里推荐使用@JSONField(serialize = false)
来显式的标注方法不参与序列化,下面是使用推荐方案后的代码,是不是一眼就能看出来哪些方法不需要参与序列化了。
public class CountryDTO {
private String country;
public void setCountry(String country) {
this.country = country;
}
public String getCountry() {
return this.country;
}
@JSONField(serialize = false)
public static void queryCountryList() {
System.out.println("queryCountryList()执行!!");
}
public Boolean isChinaName() {
System.out.println("isChinaName()执行!!");
return true;
}
public String getEnglishName() {
System.out.println("getEnglishName()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public String getOtherName() {
System.out.println("getOtherName()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public String getEnglishName2() {
System.out.println("getEnglishName2()执行!!");
return "lucy";
}
@JSONField(serialize = false)
public void getEnglishName3() {
System.out.println("getEnglishName3()执行!!");
}
@JSONField(serialize = false)
public String isChinaName2() {
System.out.println("isChinaName2()执行!!");
return "isChinaName2";
}
}
复制代码
三个频率高的序列化的情况
以上流程基本遵循 发现问题 --> 原理分析 --> 解决问题 --> 升华(编程规范)。
- 围绕业务上:解决问题 -> 如何选择一种好的额解决方案 -> 好的解决方式如何扩展n个系统应用;
- 围绕技术上:解决单个问题,顺着单个问题掌握这条线上的原理。
------
我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。另外,如果你最近想跳槽的话,年前我花了2周时间收集了一波大厂面经,节后准备跳槽的可以点击这里领取!
推荐阅读
··································
你好,我是程序猿DD,10年开发老司机、阿里云MVP、腾讯云TVP、出过书创过业、国企4年互联网6年。从普通开发到架构师、再到合伙人。一路过来,给我最深的感受就是一定要不断学习并关注前沿。只要你能坚持下来,多思考、少抱怨、勤动手,就很容易实现弯道超车!所以,不要问我现在干什么是否来得及。如果你看好一个事情,一定是坚持了才能看到希望,而不是看到希望才去坚持。相信我,只要坚持下来,你一定比现在更好!如果你还没什么方向,可以先关注我,这里会经常分享一些前沿资讯,帮你积累弯道超车的资本。
相关文章
- 【MySQL系列】- Redo log知多少
- idea tomcat catalina log乱码_xshell查看日志乱码怎么解决
- Nginx Error_log 错误日志配置和解释
- 图文结合带你搞懂MySQL日志之Redo Log(重做日志)
- 微信小程序console.log无法打印问题
- Nginx 解决error.log和access.log日志的改善总结
- Qt::Qt Log日志模块
- MySQL bin-log 日志过大造成空间不足问题的解决方法
- ORA-32412: encrypted column “string” not allowed in the materialized view log ORACLE 报错 故障修复 远程处理
- ORA-42028: materialized view log “string”.”string” does not exist ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010445; Symbol: ER_RPL_UNEXPECTED_COMMIT_ROLLBACK_OR_XID_LOG_EVENT_IN_STREAM; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-010819; Symbol: ER_BINLOG_EVENTS_READ_FROM_RELAY_LOG_INFO; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-12031: cannot use primary key columns from materialized view log on “string”.”string” ORACLE 报错 故障修复 远程处理
- ORA-16242: Processing log file (thread# string, sequence# string) ORACLE 报错 故障修复 远程处理
- ORA-16311: materialized view log already exists ORACLE 报错 故障修复 远程处理
- postgresql 切换 log、xlog日志的实现
- MySQL Error number: MY-013122; Symbol: ER_SLAVE_RELAY_LOG_WRITE_FAILURE; SQLSTATE: HY000 报错 故障修复 远程处理
- 探索Linux下查看Log的方法(linux查看log命令)
- 深入探索MySQL查询日志(mysql查询log)
- MySQL Variables log_queries_not_using_indexes 数据库 参数变量解释及正确配置使用
- 掌握 MySQL 日志查看技巧(mysql查看log)
- 文件Redis日志记录:记录系统运行信息(redis的log)
- :考勤机事件记录挖掘:MSSQL Log分析(考勤机mssql中log)
- 掌握 Linux 输出 Log 的技巧(linux输出log)
- mssql 日志清空操作:一步到位(mssql 清空log)
- MySQL日志详解掌握log文件管理,优化数据库性能(mysql 中log)
- 查看Redis日志让你快速了解系统状态的必备技能(查看redis log)
- MySQL Close Log Printing 如何关闭MySQL日志打印(mysql不打印日志)
- Oracle 无需记录Log就可实现安全运行(oracle 不记log)
- 利用Oracle日志解析业务异常(oracle log解析)
- Redis访问日志解析隐秘之旅(redis 访问 log)
- IIS6.0中修改注册表自定义Httperr.Log