0
点赞
收藏
分享

微信扫一扫

Biz-SIP业务中台案例实战(16)——Sink服务调用http-post通讯适配器

Biz-SIP业务中台支持多种连接方式的通讯适配器(Connector),包括TCP、RabbitMQ、HTTP、调用Spring服务等通讯适配方式。
http-post Connector作为Sink服务可以调用的通讯适配器,基于HTTP POST方式进行通讯交互。
本节案例中是在Sink服务中,通过http-post,和httpbin.org进行交互(httpbin.org是一个使用 Python + Flask 编写的 HTTP请求和响应服务网站):
在这里插入图片描述

其中,App层是通过Sink透传App服务,直接让调用方通过OpenAPI接口调用Sink服务(http-connector-sink),Sink服务调用simple-json类型的格式转换器,把平台内部标准格式(JSONObject对象,内部即JSON报文)简单打包成JSON报文,传给http-post Connector,通过HTTP POST方式把请求上送给httpbin.org网站进行通讯交互,返回后再把网站返回的响应报文解包成平台内部标准格式返回。

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

首先,在Biz-SIP配置目录的sink.yml中,配置对应的Sink服务:

- id: http-connector-sink
  type: rest
  url: http://bizsip-sample-sink/http-connector-sink
  converter:
    type: simple-json
  connector:
    type: http-post
    url: http://httpbin.org/post
    headers:
      Content-Type: application/json

可以看到http-connector-sink这个Sink服务,connector类型为http-post,并设置了相关参数(url为提交http post请求的url地址,headers为上送http请求报文的http headers)。格式转换器converter,设置为“type: simple-json”,采用最简单解包打包机制(直接打包成JSON,并对JSON直接解包成平台内部标准报文)。
这个Sink服务没有设置processor属性,即为缺省default类型,采用默认的缺省Sink服务流程来处理的,处理步骤依次为:

  1. 调用格式转换器converter对传入报文进行打包;
  2. 对上步打包后的报文作为调用请求报文,调用通讯适配器connector进行处理,并收到并返回响应报文;
  3. 调用格式转换器converter对响应报文进行解包,并返回解包后的报文。

最后,还需要在SampleSinkApplication的应用配置文件application-local.yml中,在bizsip.sink-id配置项中,增加http-connector-sink以便启动Sink服务:

bizsip:
  config-path: /var/bizsip/config
  sink-id: hello-sink,echo-sink,simple-xml-sink,velocity-json-sink,velocity-xml-sink,fixed-length-sink,velocity-split-sink,iso-8583-sink,sample17-sink,netty-sink,rabbitmq-connector-sink,http-connector-sink

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

对于Sink透传App服务,只需要在app.yml中配置即可:

- app-service-id: /sink/http-connector-sink
  type: sink-service
  sink-id: http-connector-sink

可以看到在app.yml中,配置了App服务“/sink/http-connector-sink”,类型为Sink透传App服务(sink-service),透传调用的Sink服务为“http-connector-sink”。

三、启动应用进行测试

启动SampleSinkApplication、SampleAppApplication应用,通过OpenAPI接口进行测试:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/sink/http-connector-sink" -X POST --data '{"accountNo":"003"}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "appServiceId": "/sink/http-connector-sink",
  "traceId": "7fe37d688ec14415b32b0ad913df28f0",
  "parentTraceId": null,
  "timestamp": 1648279939290,
  "data": {
    "headers": {
      "Accept": "text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 Hutool",
      "Host": "httpbin.org",
      "Accept-Encoding": "gzip, deflate",
      "Pragma": "no-cache",
      "X-Amzn-Trace-Id": "Root=1-623ec183-2886791f5cf46db777b11e6a",
      "Cache-Control": "no-cache",
      "Accept-Language": "zh-CN,zh;q=0.8",
      "Content-Length": "19",
      "Content-Type": "application/json"
    },
    "data": "{\"accountNo\":\"003\"}",
    "origin": "101.80.178.194",
    "url": "http://httpbin.org/post",
    "args": {},
    "form": {},
    "files": {},
    "json": {
      "accountNo": "003"
    }
  }
}
举报

相关推荐

0 条评论