开发者社区 > 云原生 > 正文

Sentinel客户端应用程序将json格式的规则解析为对象时出错

组件信息

Sentinel

描述错误

通过Sentinel Dashboard向应用程序添加流控制规则后,应用程序正在将相应的json数据反序列化为[FlowRule] (https://github.com/alibaba/Sentinel/blob/master/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java) 在使用对象时出错。

再现行为的步骤:

1.首先,客户端应用程序和Sentinel Dashboard使用相同的Zookeeper

2.使用Arthas工具诊断客户端应用程序。监控com.fasterxml.jackson.databind.ObjectMapper#readValue方法的执行。

watch com.fasterxml.jackson.databind.ObjectMapper readValue '{params, returnObj, throwExp}' -x 2

3.在Sentinel仪表板上添加流量控制规则。

4.观察Arthas的输出以查看错误消息

ts=2019-04-18 15:51:09; [cost=0.324774ms] result=@ArrayList[ @Object[][ @String[{"app":"xxx","gmtModified":1555572942154,"resource":"com.xx.yy.ZZZZService:function(com.xx.yy.Ttt)","ip":"172.18.0.9","controlBehavior":0,"count":10.0,"gmtCreate":1555572942154,"limitApp":"default","clusterMode":false,"port":8719,"grade":1,"id":13,"strategy":0}], @Class[class com.alibaba.csp.sentinel.slots.block.flow.FlowRule], ], null, com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "app" (class com.alibaba.csp.sentinel.slots.block.flow.FlowRule), not marked as ignorable (12 known properties: "warmUpPeriodSec", "refResource", "rater", "strategy", "limitApp", "grade", "maxQueueingTimeMs", "clusterConfig", "clusterMode", "resource", "count", "controlBehavior"]) at [Source: {"app":"xxx","gmtModified":1555572942154,"resource":"com.xx.yy.ZZZZService:function(com.xx.yy.Ttt)","ip":"172.18.0.9","controlBehavior":0,"count":10.0,"gmtCreate":1555572942154,"limitApp":"default","clusterMode":false,"port":8719,"grade":1,"id":13,"strategy":0}; line: 1, column: 9] (through reference chain: com.alibaba.csp.sentinel.slots.block.flow.FlowRule["app"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62) at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:834) at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1093) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1489) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1467) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:282) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858) at org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter.convertFlowRule(JsonConverter.java:115) at org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter.convert(JsonConverter.java:70) at org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter.convert(JsonConverter.java:38) at com.alibaba.csp.sentinel.datasource.AbstractDataSource.loadConfig(AbstractDataSource.java:48) at com.alibaba.csp.sentinel.datasource.AbstractDataSource.loadConfig(AbstractDataSource.java:44) at com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource$1.nodeChanged(ZookeeperDataSource.java:108) at org.apache.curator.framework.recipes.cache.NodeCache$4.apply(NodeCache.java:310) at org.apache.curator.framework.recipes.cache.NodeCache$4.apply(NodeCache.java:304) at org.apache.curator.framework.listen.ListenerContainer$1.run(ListenerContainer.java:93) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) , ]

5.最终,流量控制规则没有生效。

预期行为 Json可以成功解析为 [FlowRule] (https://github.com/alibaba/Sentinel/blob/master/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRule.java) 对象。

流量控制规则生效。

屏幕截图 【提问36】

附加上下文 springboot 1.5.3 java8 docker Sentinel Dashboard 1.4.0 sentinel-datasource-zookeeper 1.5.1 sentinel-transport-simple-http 1.5.1 spring-cloud-starter-alibaba-sentinel 0.1.1.RELEASE

以下是sentinel的配置,应用程序是基于docker编写的。

sentinel

spring.cloud.sentinel.transport.dashboard = sentinel-dashboard:8181

flow

spring.cloud.sentinel.datasource.flow.zk.server-addr = zookeeper:2181 spring.cloud.sentinel.datasource.flow.zk.path = /sentinel/rules/${spring.application.name}/flow

degrade

spring.cloud.sentinel.datasource.degrade.zk.server-addr = zookeeper:2181 spring.cloud.sentinel.datasource.degrade.zk.path = /sentinel/rules/${spring.application.name}/degrade

param

spring.cloud.sentinel.datasource.param-flow.zk.server-addr = zookeeper:2181 spring.cloud.sentinel.datasource.param-flow.zk.path = /sentinel/rules/${spring.application.name}/param

system

spring.cloud.sentinel.datasource.system.zk.server-addr = zookeeper:2181 spring.cloud.sentinel.datasource.system.zk.path = /sentinel/rules/${spring.application.name}/system

authority

spring.cloud.sentinel.datasource.authority.zk.server-addr = zookeeper:2181 spring.cloud.sentinel.datasource.authority.zk.path = /sentinel/rules/${spring.application.name}/authority

Json数据如下

[{"app":"app","clusterMode":false,"controlBehavior":0,"count":1.0,"gmtCreate":1555990566403,"gmtModified":1555990566403,"grade":1,"id":1,"ip":"172.18.0.7","limitApp":"default","port":8719,"resource":"/api/v1/asset/userAssetInfo","strategy":0}]

原提问者GitHub用户zhangtianhao

展开
收起
白夜行fighting 2023-06-11 10:40:36 139 0
1 条回答
写回答
取消 提交回答
  • 默认转换器无法转换id、ip、app、port、gmtCreate、gmtModified等属性。您可以修改sentinel仪表板或使用自定义转换器。

    原回答者GitHub用户fangjian0423

    2023-06-11 11:31:15
    赞同 展开评论 打赏

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
阿里云容器 AHAS Sentinel 网关流控揭秘 立即下载
workshop专场-微服务专场-开发者动手实践营-微服务-使用Sentinel进行微服务流量控制 立即下载
Sentinel分布式系统下的流量防卫兵 立即下载

相关镜像