服务接口JSON兼容性检测
Rest协议下对服务接口进行JSON兼容性检测
特性说明
Dubbo目前支持使用Rest协议进行服务调用,Rest协议默认会使用JSON作为序列化方式,但JSON并不支持Java的一些特殊用法,如接口和抽象类等。
Dubbo 3.3版本在服务发布流程中增加了服务接口JSON兼容性检测功能, 可以确保服务接口传输对象是否可以被JSON序列化, 进一步提升Rest服务接口的正确性。
使用场景
使用Rest作为通信协议,JSON作为序列化方式时,对服务接口进行兼容性检查,确保服务接口传输对象可以正确地被JSON序列化。
使用方式
当使用Rest协议作为通信协议,JSON作为序列化方式时,可以在xml文件中通过配置protocol的json-check-level属性来配置JSON兼容性检查的级别。
目前有3种级别,每种级别的具体含义如下:
disabled:表示不开启JSON兼容性检查,此时不会对接口进行兼容性检查。warn:表示开启JSON兼容性检查,如果出现不兼容的情况,将会以warn级别的日志形式将不兼容的接口名称打印输出到终端。strict:表示开启JSON兼容性检查,如果出现不兼容的情况,将会在启动时抛出IllegalStateException异常,终止启动流程,同时会将不兼容的接口名称存放在异常信息中。
如果没有通过
json-check-level指定兼容性检查级别,则默认是warn告警级别。
使用示例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example.rest"/>
<bean name="dubboConfig" class="com.example.rest.config.DubboConfig"></bean>
<dubbo:application name="rest-provider" owner="programmer" organization="dubbo"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2180"/>
<!-- 将JSON兼容性检查级别设为disabled -->
<dubbo:protocol name="rest" port="8880" threads="300" json-check-level="disabled"/>
<!-- 将JSON兼容性检查级别设为warn -->
<dubbo:protocol name="rest" port="8880" threads="300" json-check-level="warn"/>
<!-- 将JSON兼容性检查级别设为strict -->
<dubbo:protocol name="rest" port="8880" threads="300" json-check-level="strict"/>
</beans>
最后修改 August 29, 2023: Add doc of json-compatibility-check (#2799) (dee92fbb76b)
