Biz-SIP业务中台案例实战(13)——Source调用格式转换器Converter

2023-05-06 08:31:40

 

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

Biz-SIP业务中台支持多种报文类型的格式转换器(Converter),包括XML、JSON、定长、有分隔符、ISO-8583等报文类型的解包和打包。

在Source模块中,可以通过Java API接口调用Converter,在Source模块代码中实现报文解包和打包。

本节案例中是在通过RestController接入的Source模块代码中,通过Java API调用模板XML格式转换器(simple-xml),实现XML报文的打包和解包:

SampleSource4Controller是一个RestController,通过RESTful接受XML报文请求后,通过调用格式转换器Converter进行报文解包成平台标准JSON报文,然后调用App服务。

App服务(/bean/sample1)对输入报文没有做任何修改,是直接把原报文返回的。

SampleSource4Controller在收到App服务返回报文后,会再调用格式转换器进行报文打包成XML报文,作为RESTful请求的响应返回。

具体代码和配置可以查看Biz-SIP源代码中的Sample相关测试案例(

https://gitee.com/szhengye/biz-sip)

一、Source层Source服务的开发和配置

首先,在Biz-SIP配置目录的source.yml中,配置对应的Source:

- id: source4 converter: type: simple-xml

可以看到source4这个Source,格式转换器converter,设置为“type: simple-xml”,表示是采用基于简单XML格式转换器。

SampleSource4Controller是一个RestController,负责外部调用的接入,相关代码如下:

@Slf4j @RestController public class SampleSource4Controller { private Converter converter = Converter.getSourceConverter("source4"); private BizMessageInterface appInterface = SourceClientFactory .getAppServiceClient(BizMessageInterface.class,"/bean/sample1"); @PostMapping(value = "/source4", consumes = "application/xml", produces = "application/xml") public Object doService(@RequestBody String inMessage, HttpServletResponse response) { BizMessage outMessage = null; try { JSONObject jsonObject = this.converter.unpack(inMessage.getBytes()); log.debug("解包后消息:\n{}", BizUtils.buildJsonLog(jsonObject)); BizMessage<JSONObject> bizMessage = this.appInterface.call(jsonObject); log.debug("调用服务返回消息:\n{}",BizUtils.buildBizMessageLog(bizMessage)); byte[] outData = this.converter.pack(bizMessage.getData()); log.debug("打包后消息:\n{}",BizUtils.buildHexLog(outData)); return new String(outData); } catch (BizException e) { return "Source API执行出错:" + "\ncode:" + e.getCode() + "\nmessage:" + e.getMessage() + "\nextMessage:" + e.getExtMessage(); } } }

可以看到SampleSource4Controller类实现了“/source4”的URL接口,申明了和source.yml中的source4所绑定的格式转换器——converter,以及调用“/bean/sample1”App服务的调用接口——appInterface。在doService()方法中,主要有以下处理步骤:

调用this.converter.unpack(),对传入的XML报文进行解包;把打包后的XML报文,通过this.appInterface.call()方法调用App服务;调用this.converter.pack(),把调用App服务返回的报文打包成XML报文返回。

二、App层App服务的开发和配置

App服务,是采用《Biz-SIP业务中台案例实战(1)——开发最简单的App服务》一节中介绍的“/bean/sample1”,这个App服务是收到请求后,不做任何处理,直接把请求返回。具体的app.yml配置和相关的Sample1AppService类代码,这里就不再细述,具体参考前面的介绍。

三、启动应用进行测试

启动SampleSourceApplication、SampleAppApplication应用,通过curl发起测试报文:

$ curl -H "Content-Type:application/xml" -X POST --data <?xml version="1.0" encoding="UTF-8" standalone="no"?><root><accountName>王五</accountName><balance>500</balance><accountNo>005</accountNo></root> http://localhost:8080/source4 <?xml version="1.0" encoding="UTF-8" standalone="no"?><root><accountName>王五</accountName><balance>500</balance><accountNo>005</accountNo></root>shizhengyedeMacBoo

SampleSourceApplication应用打印日志:

[bizsip-sample-source:192.169.1.107:8080] 12:54:52 DEBUG 8409 [] [http-nio-8080-exec-1] c.b.b.s.s.c.SampleSource4Controller 解包后消息: { "accountName": "王五", "balance": 500, "accountNo": "005" } [bizsip-sample-source:192.169.1.107:8080] 12:54:52 DEBUG 8409 [] [http-nio-8080-exec-1] c.b.b.source.api.AppServiceClientMethod 调用App服务: /bean/sample1 [bizsip-sample-source:192.169.1.107:8080] 12:54:52 TRACE 8409 [] [http-nio-8080-exec-1] c.b.b.source.api.AppServiceClientMethod 调用App服务请求报文: { "accountName": "王五", "balance": 500, "accountNo": "005" } [bizsip-sample-source:192.169.1.107:8080] 12:54:53 DEBUG 8409 [] [nioEventLoopGroup-2-1] c.b.b.s.s.l.NettyClientHandler 写空闲超时,发送心跳检测包! [bizsip-sample-source:192.169.1.107:8080] 12:54:53 TRACE 8409 [e1fd243478df4f119d6c3cfa5f615671] [http-nio-8080-exec-1] c.b.b.source.api.AppServiceClientMethod App服务响应报文: traceId: e1fd243478df4f119d6c3cfa5f615671 appServiceId: /bean/sample1 code: 0 message: success { "accountName": "王五", "balance": 500, "accountNo": "005" } [bizsip-sample-source:192.169.1.107:8080] 12:54:53 DEBUG 8409 [e1fd243478df4f119d6c3cfa5f615671] [http-nio-8080-exec-1] c.b.b.s.s.c.SampleSource4Controller 调用服务返回消息: traceId: e1fd243478df4f119d6c3cfa5f615671 appServiceId: /bean/sample1 code: 0 message: success { "accountName": "王五", "balance": 500, "accountNo": "005" } [bizsip-sample-source:192.169.1.107:8080] 12:54:53 DEBUG 8409 [e1fd243478df4f119d6c3cfa5f615671] [http-nio-8080-exec-1] c.b.b.s.s.c.SampleSource4Controller 打包后消息: ====+ 01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17-18-19-20 + ====== ASCII ====== + 0000: 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 | <?xml version="1.0" | 0020: 65 6E 63 6F 64 69 6E 67 3D 22 55 54 46 2D 38 22 20 73 74 61 | encoding="UTF-8" sta | 0040: 6E 64 61 6C 6F 6E 65 3D 22 6E 6F 22 3F 3E 3C 72 6F 6F 74 3E | ndalone="no"?><root> | 0060: 3C 61 63 63 6F 75 6E 74 4E 61 6D 65 3E E7 8E 8B E4 BA 94 3C | <accountName>王.五.< | 0080: 2F 61 63 63 6F 75 6E 74 4E 61 6D 65 3E 3C 62 61 6C 61 6E 63 | /accountName><balanc | 0100: 65 3E 35 30 30 3C 2F 62 61 6C 61 6E 63 65 3E 3C 61 63 63 6F | e>500</balance><acco | 0120: 75 6E 74 4E 6F 3E 30 30 35 3C 2F 61 63 63 6F 75 6E 74 4E 6F | untNo>005</accountNo | 0140: 3E 3C 2F 72 6F 6F 74 3E | ></root>............ |

Biz-SIP网站:http://bizsip.bizmda.com

Gitee代码库:

https://gitee.com/szhengye/biz-sip


以上就是关于《Biz-SIP业务中台案例实战(13)——Source调用格式转换器Converter》的全部内容,本文网址:https://www.7ca.cn/baike/23558.shtml,如对您有帮助可以分享给好友,谢谢。
标签:
声明

排行榜