Biz-SIP中间件之HelloWorld(3)——app服务实现对sink服务的聚合和编排

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

三、app服务实现对sink服务的聚合和编排

在Biz-SIP中间件中,app层不仅能实现sink服务个性化处理,还能对sink服务服务聚合和服务编排。
这里的例子是通过app层的app/sample-bean-service,实现对2个sink服务(sample-sink-bean-sink、sample-bean-sink)的聚合:
Biz-SIP中间件之HelloWorld(3)——app服务实现对sink服务的聚合和编排

app层的app/sample-bean-service服务,是bean-service类型服务,bean-service类型是基于Java接口的调用,和app-bean-service类型基于JSON接口不同。
sink层的sample-sink-bean-sink和sample-bean-sink服务,分别是sink-bean类型和bean类型服务,调用接口分别是基于JSON接口和Java接口的。
主要实现步骤如下:
1、创建Java接口:

  • 在“biz-sip-client”模块下,创建“app-client”子模块;
  • 在“app-client”子模块中,创建接口类:
public interface SampleBeanServiceInterface {
    public String callSampleSinkBeanSink(String message);
    public String callSampleBeanSink(String message);
}

2、sink层的sample-sink-bean-sink服务和sample-bean-sink服务,在前面的二个例子中已经创建,这里直接引用即可。
3、创建app层的app/sample-bean-service服务:

  • 在biz-sip-app模块中,增加bean类型的服务类,该类需要继承前面创建的SampleBeanServiceInterface接口,实现callSampleSinkBeankSink()和callSampleBeanSink()方法。另外,对于sink层服务的调用,是通过IntegratorClientFactory.getSinkClient()来获得调用接口类的,其中sample-sink-bean-sink是JSON接口,采用BizMessageInterface来作为调用接口,而sample-bean-sink是基于HelloInterface实现的bean-sink服务,是采用HelloInterface来作为调用接口的:
@Service
public class SampleBeanService implements SampleBeanServiceInterface {
    private BizMessageInterface sampleSinkBeankSinkInterface = IntegratorClientFactory
            .getSinkClient(BizMessageInterface.class,"sample-sink-bean-sink");
    private HelloInterface helloInterface = IntegratorClientFactory
            .getSinkClient(HelloInterface.class,"sample-bean-sink");
    @Override
    public String callSampleSinkBeanSink(String message) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("message",message);
        BizMessage<JSONObject> bizMessage;
        try {
            bizMessage = sampleSinkBeankSinkInterface.call(jsonObject);
        } catch (BizException e) {
            e.printStackTrace();
            return null;
        }
        return (String)bizMessage.getData().get("message");
    }

    @Override
    public String callSampleBeanSink(String message) {
        return this.helloInterface.hello(message);
    }
}
  • 在service.yml中把实现app/sample-bean-service服务的定义和服务类的挂接:
- bizServiceId: app/sample-bean-service
  type: bean-service
  className: com.sample.app.service.SampleBeanService

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

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/sample-bean-service" -X POST --data '{"methodName":"callSampleSinkBeanSink","params":["world"]}' http://localhost:8888/api|jq 

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

$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:app/sample-bean-service" -X POST --data '{"methodName":"callSampleBeanSink","params":["world"]}' http://localhost:8888/api|jq

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "69f5e8c9a9384814b8d49774f848a088",
  "parentTraceId": null,
  "timestamp": 1633604304089,
  "data": {
    "result": "sample-bean-sink: Hello,world"
  }
}

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

上一篇:快读快写


下一篇:[sip]搭建opensips:ubuntu+x86+docker