Biz-SIP业务中台案例实战(19)——App服务的域级校验和服务级检验

发布一下 0 0

Biz-SIP金融级业务中台(http://bizsip.bizmda.com)是一套基于领域驱动设计(DDD)架构,能快速构建金融级云原生架构的服务整合中间件,整合了在金融场景里锤炼出来的最佳实践。

案例要求:

通过Biz-SIP的开放API接口发送请求,在App服务调用前,会根据配置进行域级校验和服务级校验,校验出错会返回出错信息,校验通过后,会调用App服务后直接返回。:

Biz-SIP业务中台案例实战(19)——App服务的域级校验和服务级检验

具体代码和配置可以查看Biz-SIP源代码中的Sample相关测试案例(https://gitee.com/szhengye/biz-sip)

一、App服务的开发和配置

首先,我们需要编写一个App服务类(Sample1AppService.java):

@Slf4j@Servicepublic class Sample1AppService implements AppBeanInterface {    @Override    public JSONObject process(JSONObject message) throws BizException {        log.debug("收到请求数据:\n{}", BizUtils.buildJsonLog(message));        return message;    }}

Sample1AppService类继承了AppBeanInterface接口,实现了process()方法,这个方法的输入输出参数,都是平台统一的JSONObject对象。

可以看到在process()方法中,对输入报文没有做任何修改,是直接把原报文返回的。

然后,在Biz-SIP统一配置目录中的app.yml中,配置对应的App服务:

- app-service-id: /bean/sample1-check-rule  type: app-bean-service  class-name: com.bizmda.bizsip.sample.app.service.Sample1AppService

二、App服务的域级检验和服务级检验配置

在Biz-SIP统一配置目录下创建“/check-rule/bean/sample1-check-rule.yml”:

field-check-rules:  - field: email    rule: isEmail    message: '不是邮箱地址:{}'  - field: sex    rule: notEmpty    message: '不能为空'  - field: mobile    rule: isMatchRegex    args:      - '^[1][3,4,5,6,7,8,9][0-9]{9}$'    message: '不是手机号{}'field-check-mode: oneservice-check-rules:  - script: |      if (data.age == null) {        return;      }      if (data.sex == "1" && data.age >= 60) {        return '男性年龄大于60岁';      }      if (data.sex == "0" && data.age >= 50) {        return '女性年龄大于50岁!';      }service-check-mode: one

可以看到在sample1-chekc-rule.yml中配置了针对email、sex、mobile共三个域的域级检验规则,以及针对age年龄的服务级检验规则。

三、启动应用进行测试

启动SampleAppApplication应用,通过开放平台接口发起请求,进行一系列的测试:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232@163.com","mobile":"18601872345"}' http://localhost:8888/api|jq{  "code": 0,  "message": "success",  "extMessage": null,  "appServiceId": "/bean/sample1-check-rule",  "traceId": "ad6765833c2847a3912cf836e25c91d9",  "parentTraceId": null,  "timestamp": 1647845790993,  "data": {    "sex": "0",    "mobile": "18601872345",    "accountNo": "62001818",    "email": "123232@163.com"  }}$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232","mobile":"18601872345"}' http://localhost:8888/api|jq{  "code": 307,  "message": "域校验出错",  "extMessage": "[{\"message\":\"不是邮箱地址:123232\",\"field\":\"email\"}]",  "appServiceId": "/bean/sample1-check-rule",  "traceId": "b11f60ef602b475daea510bcfffea2ba",  "parentTraceId": null,  "timestamp": 1647845791036,  "data": null}$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232@163.com","mobile":"021-34554345"}' http://localhost:8888/api|jq{  "code": 307,  "message": "域校验出错",  "extMessage": "[{\"message\":\"不是手机号021-34554345\",\"field\":\"mobile\"}]",  "appServiceId": "/bean/sample1-check-rule",  "traceId": "41a2013c94f84339922891cbf65b8954",  "parentTraceId": null,  "timestamp": 1647845791073,  "data": null}$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","email":"123232@163.com","mobile":"18601872345"}' http://localhost:8888/api|jq{  "code": 307,  "message": "域校验出错",  "extMessage": "[{\"message\":\"不能为空\",\"field\":\"sex\"}]",  "appServiceId": "/bean/sample1-check-rule",  "traceId": "83f21fd288c6474c953b2531a68737fb",  "parentTraceId": null,  "timestamp": 1647845791105,  "data": null}$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232@163.com","mobile":"18601872345","age":20}' http://localhost:8888/api|jq{  "code": 0,  "message": "success",  "extMessage": null,  "appServiceId": "/bean/sample1-check-rule",  "traceId": "06bb9d1d714c4ee78cd309eecd216b20",  "parentTraceId": null,  "timestamp": 1648699339934,  "data": {    "sex": "0",    "mobile": "18601872345",    "accountNo": "62001818",    "email": "123232@163.com",    "age": 20  }}$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1-check-rule" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232@163.com","mobile":"18601872345","age":55}' http://localhost:8888/api|jq{  "code": 312,  "message": "服务规则校验出错",  "extMessage": "[{\"message\":\"女性年龄大于50岁!\"}]",  "appServiceId": "/bean/sample1-check-rule",  "traceId": "67b19f640c3b4a6d962d97e55f95e368",  "parentTraceId": null,  "timestamp": 1648699555359,  "data": null}

Biz-SIP网站:http://bizsip.bizmda.com
Gitee代码库:https://gitee.com/szhengye/biz-sip

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/83765.html