Biz-SIP中间件之HelloWorld(2)——app服务实现对sink服务的处理

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

二、app服务实现对sink服务的处理

在Biz-SIP中间件中,app层不仅能实现sink服务的透传,还能对sink服务进行个性化处理。
这里的例子是通过app层的app/sample-app-bean-service,实现对sink服务的处理:
在这里插入图片描述

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

  • 在“biz-sip-client”模块下,创建“sample-bean-sink-client”子模块;
  • 在“sample-bean-sink-client”子模块中,创建接口类:
public interface HelloInterface {
    public String hello(String message);
}

2、创建sink层的sample-bean-sink服务:

  • 在“biz-sip-sink”模块下,创建“sample-bean-sink”子模块,新建SpringBoot启动类SampleSinkBeanSinkApplication和配置文件application.yml,具体参见源码;
  • 创建SampleBeanService类,该类实现HelloInterface接口,并实现hello()方法:
@Service
public class SampleBeanService implements HelloInterface {
    @Override
    public String hello(String message) {
        return "sample-bean-sink: Hello," + message;
    }
}
  • 在sink.yml中,定义sample-bean-sink服务:
- id: sample-bean-sink
  type: rest
  url: http://sample-bean-sink/sink
  connector:
    type: bean
    class-name: com.sample.sink.samplebean.service.SampleBeanService
    spring-bean: true

3、创建app层的app/sample-app-bean-service服务:

  • 在biz-sip-app模块中,增加app-bean类型的服务类,该类需要继承AppBeanInterface接口,并实现process()方法,是JSON接口调用。另外,对于sink层sample-bean-sink服务的调用,是通过IntegratorClientFactory.getSinkClient(HelloInterface.class,"sample-bean-sink”)来获得调用接口类的:
@Service
public class SampleAppBeanService implements AppBeanInterface {
    private HelloInterface helloInterface = IntegratorClientFactory
            .getSinkClient(HelloInterface.class,"sample-bean-sink");

    @Override
    public JSONObject process(JSONObject jsonObject) throws BizException {
        String message = (String)jsonObject.get("message");
        jsonObject.set("message","sample-app-bean-service: Hello,"+message+";"
                + this.helloInterface.hello(message));
        return jsonObject;
    }
}
  • 在service.yml中把实现app/sample-app-bean-service服务的定义和服务类的挂接:
- bizServiceId: app/sample-app-bean-service
  type: app-bean-service
  className: com.sample.app.service.SampleAppBeanService

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

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

{
  "code": 0,
  "message": null,
  "extMessage": null,
  "traceId": "ac3f584099604789873b9e3b52d7a391",
  "parentTraceId": null,
  "timestamp": 1633602492867,
  "data": {
    "message": "sample-app-bean-service: Hello,world;sample-bean-sink: Hello,world"
  }
}

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

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