线上事故回顾 前段时间同事新增了一个特别简单的功能,晚上上线前review代码时想到公司拼搏进取的价值观临时他加一行 log 日志,觉得就一行简单的日志基本上没啥问题,结果刚上完线后一堆报警,赶紧回滚了代码,找到问题删除了添加日志的代码,重新上线完毕。
基于 spring boot + mybatis plus + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/ruoyi-vue-pro 视频教程:https://doc.iocoder.cn/video/ 情景还原 ❝
定义了一个 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()呢? 引申一下,序列化过程中会执行那些方法呢? 基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/yudao-cloud 视频教程:https://doc.iocoder.cn/video/ 源码分析 通过 debug 观察调用链路的堆栈信息
调用链中的asmserializer_1_countrydto.write是fastjson使用asm技术动态生成了一个类asmserializer_1_countrydto。
❝
asm技术其中一项使用场景就是通过到动态生成类用来代替java反射,从而避免重复执行时的反射开销
❞
javabeanserizlier序列化原理 通过下图看出序列化的过程中,主要是调用javabeanserializer类的write()方法。
objectserializer实现类javabeanserializer 而javabeanserializer主要是通过getobjectwriter()方法获取,通过对getobjectwriter()执行过程的调试,找到比较关键的com.alibaba.fastjson.serializer.serializeconfig#createjavabeanserializer方法,进而找到 com.alibaba.fastjson.util.typeutils#computegetters
public static list computegetters(class clazz, // jsontype jsontype, // map aliasmap, // map 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开头的方法 序列化流程图 序列化流程图 示例代码 /** * 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)来显式的标注方法不参与序列化,下面是使用@jsonfield注解后的代码,是不是一眼就能看出来哪些方法不需要参与序列化了。
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 个系统应用; 围绕技术上:解决单个问题,顺着单个问题掌握这条线上的原理。 但其实这段代码我并不满意,原因是和 fastjson 依赖太高了。我想要的效果是,不依赖任何特定的 json 序列化框架。当我需要替换掉它的时候,随时可以替换掉。
并且在写代码时,不要过于依赖日志。打日志只需要打紧要且关键的信息即可,不要什么日志都打,我曾见过一个系统,一个小时,把 128g 磁盘跑满的管理系统。几乎没啥并发,但几乎每个请求都输出几 m 的日志,这件事我后面会单独拿出来讲讲。
关于@jsonfield和@jsontype等特性注解,后面我会在团队内规范并给出新的解耦方案,把它们移除掉。
比小米6还要惊艳,红米Pro 2惊人曝光!全面屏 售价1999 屏占比极夸张!
台湾半导体产业明年将超韩国仅此美国
电路板上的字母的含义是什么代号你知道吗
STM32Cube.AI将神经网络转换为STM32的优化代码
贯流风机是怎样的存在
一行log日志,引发了P1的线上事故
基于LM387的单声道前置放大器电路图
为什么芬兰银行认为所有的加密货币都是谬论
WiMAX在IPTV接入中的优势分析
D类放大器的多级EMI消减技术
LED光源与LED灯具效能的区别
微雪电子STM32 QFP176测试座介绍
如何保障基于 NB-IoT 等物联网技术数据的私有性?
如何提高smt贴片机贴片速度
日月光扩大封测布局,计划多家子公司合二为一
你们安森美公司靠哪项技术每小时拯救9条生命吗
用TL494驱动12V到24V的CCFL电源
基于Transform的神经网络结构FlowFormer用于光流量估计
泰克TEKTRONIX交直流电流探头TCP0150的安装及使用方法
基于CMOS的图像传感器如何在不影响分辨率的情况下扩大模具尺寸