Biz-SIP中间件之HelloWorld(1)——实现sink服务的穿透式访问

HelloWorld项目版本库:https://gitee.com/szhengye/biz-sip-helloworld.git

一、实现sink服务的穿透式访问

在Biz-SIP中间件中,外部应用不管通过source模块还是开放平台接口,都无法直接访问基于app层背后的sink层服务,但可以通过app层的sink/sample-sink-service,把sink服务实现开放访问。
sink服务的穿透式访问,调用关系如下图所示:
在这里插入图片描述

sink层的sample-sink-bean-sink服务,是sink-beank类型服务,该服务的调用是基于JSON接口的。
主要实现步骤如下:
1、我们先创建sink层的sample-sink-bean-sink服务:

  • 在“biz-sip-sink”模块下,创建“sample-sink-bean-sink”子模块,新建SpringBoot启动类SampleSinkBeanSinkApplication和配置文件application.yml,具体参见源码;
  • 创建SampleSinkBeanService类,该类实现JSONObjectSinkBeanInterface接口,并实现process()方法,实现对传入标准JSON报文的处理:
@Service
public class SampleSinkBeanService implements JSONObjectSinkBeanInterface {
    @Override
    public JSONObject process(JSONObject packMessage) throws BizException {
        String message = (String)packMessage.get("message");
        packMessage.set("message","sample-sink-bean-sink: Hello,"+message);
        return packMessage;
    }
}
  • 在sink.yml中,定义sample-sink-bean-sink服务:
- id: sample-sink-bean-sink
  type: rest
  url: http://sample-sink-bean-sink/sink
  connector:
    type: sink-bean
    class-name: com.sample.sink.samplesinkbean.service.SampleSinkBeanService
    spring-bean: true

2、在app层中,在service.yml中把sample-sink-bean-sink服务,通过sink-bean方式直接开放出去:

- bizServiceId: sink/sample-sink-service
  type: sink-service
  sinkId: sample-sink-bean-sink

3、通过app层的开放平台OpenAPI接口,可以直接进行测试访问:

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:sink/sample-sink-service" -X POST --data '{"message":"world"}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "c6a374d195f3450e8c99da23c8a2077b",
  "parentTraceId": null,
  "timestamp": 1633593193550,
  "data": {
    "message": "sample-sink-bean-sink: Hello,world"
  }
}

Biz-SIP官方网站:http://bizsip.bizmda.com
Gitee:[https://gitee.com/szhengye/biz-sip]

posted @ 2021-10-08 11:17  抽游烟鸡  阅读(58)  评论(0)    收藏  举报