HelloWorld示例项目

用Biz-SIP业务中台开发一个最简单的Hello world项目

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

Biz-SIP服务整合中间件是一套基于领域驱动设计(DDD),用于快速构建金融级云原生架构的服务整合中间件,包含了在金融场景里锤炼出来的最佳实践。
Biz-SIP是基于领域驱动设计(DDD)的分层结构:

image.png

  • 适配层(Adapter Layer):也叫用户接口层,负责对前端展示(web,wireless,wap)的路由和适配,对于传统B/S系统而言,adapter就相当于MVC中的controller;
  • 应用层(Application Layer):主要负责获取输入,组装上下文,参数校验,调用领域层做业务处理,如果需要的话,发送消息通知等。层次是开放的,应用层也可以绕过领域层,直接访问基础实施层;
  • 领域层(Domain Layer):主要是封装了核心业务逻辑,并通过领域服务(Domain Service)和领域对象(Domain Entity)的方法对App层提供业务实体和业务逻辑计算。领域是应用的核心,不依赖任何其他层次;
  • 基础设施层(Infrastructure Layer):主要负责技术细节问题的处理,比如数据库的CRUD、搜索引擎、文件系统、分布式服务的RPC等。此外,领域防腐的重任也落在这里,外部依赖需要通过gateway的转义处理,才能被上面的App层和Domain层使用。

Biz-SIP中间件,主要关注上面三层的构建:适配层、应用层和领域层,在Biz-SIP中间件中,分别称为:Source、App、Sink。
本文介绍的Biz-SIP中间件HelloWorld应用,将在Source、App、Sink这三层上进行原型系统的搭建,源码请参见:https://gitee.com/szhengye/biz-sip-helloworld.git
HelloWorld应用的整体构架如下图所示:

包括以下层和模块:

  • 开放OpenAPI接口:Biz-SIP中间件App层的标准开放平台接口。
  • Source:Biz-SIP的所有服务接入模块,支持各种外部对系统的调用发起,类似DDD中的适配层。目前有SampleRestSource一个应用。
  • App:Biz-SIP的所有整合服务,用于对Sink服务的编排,类似DDD中的应用层。包括/sink/sample-sink-bean-service、/app/sample-app-bean-service、/app/sample-bean-service共三个服务。
  • Sink:Biz-SIP的所有服务调用模块,负责领域服务的处理和外部第三方应用的调用,类似DDD中的领域层。包括sample-sink-bean-sink、sample-bean-sink二个同步微服务应用,以及sample-rabbitmq-bean-sink异步RabbitMQ应用。

一、实现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类,该类实现SinkBeanInterface接口,并实现process()方法,实现对传入标准JSON报文的处理:
@Service
public class SampleSinkBeanService implements SinkBeanInterface {
    @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
  processor: sink-bean
  class-name: com.sample.sink.samplesinkbean.service.SampleSinkBeanService

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

- app-service-id: /sink/sample-sink-service
  type: sink-service
  sink-id: 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"
  }
}

二、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
  processor: bean
  class-name: com.sample.sink.samplebean.service.SampleBeanService

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

  • 在biz-sip-app模块中,增加app-bean类型的服务类,该类需要继承AppBeanInterface接口,并实现process()方法,是JSON接口调用。另外,对于sink层sample-bean-sink服务的调用,是通过AppClientFactory.getSinkClient(HelloInterface.class,“sample-bean-sink”)来获得调用接口类的:
@Service
public class SampleAppBeanService implements AppBeanInterface {
    private HelloInterface helloInterface = AppClientFactory
            .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;
    }
}
  • 在app.yml中把实现/app/sample-app-bean-service服务的定义和服务类的挂接:
- app-service-id: /app/sample-app-bean-service
  type: app-bean-service
  class-name: 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"
  }
}

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

在Biz-SIP中间件中,app层不仅能实现sink服务个性化处理,还能对sink服务服务聚合和服务编排。
这里的例子是通过app层的/app/sample-bean-service,实现对3个sink服务(sample-sink-bean-sink、sample-bean-sink、sample-rabbitmq-bean-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);
    public String callAllSink(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层服务的调用,是通过AppClientFactory.getSinkClient()来获得调用接口类的,其中sample-sink-bean-sink是JSON接口,采用BizMessageInterface来作为调用接口,而sample-bean-sink和sample-rabbitmq-bean-sink是基于HelloInterface实现的bean-sink服务,是采用HelloInterface来作为调用接口的:
@Service
public class SampleBeanService implements SampleBeanServiceInterface {
    private BizMessageInterface sampleSinkBeankSinkInterface = AppClientFactory
            .getSinkClient(BizMessageInterface.class,"sample-sink-bean-sink");
    private HelloInterface sampleBeanSinkInterface = AppClientFactory
            .getSinkClient(HelloInterface.class,"sample-bean-sink");
    private HelloInterface sampleRabbitmqBeanSinkInterface = AppClientFactory
            .getSinkClient(HelloInterface.class,"sample-rabbitmq-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.sampleBeanSinkInterface.hello(message);
    }

    @Override
    public String callAllSink(String message) {
        String result = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("message",message);
        BizMessage<JSONObject> bizMessage;
        try {
            bizMessage = sampleSinkBeankSinkInterface.call(jsonObject);
        } catch (BizException e) {
            e.printStackTrace();
            return null;
        }
        this.sampleRabbitmqBeanSinkInterface.hello(message);
        result = (String)bizMessage.getData().get("message") + "\n";
        result = result + this.sampleBeanSinkInterface.hello(message);
        return result;
    }
}
  • 在app.yml中把实现app/sample-bean-service服务的定义和服务类的挂接:
- app-service-id: /app/sample-bean-service
  type: bean-service
  class-name: 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"
  }
}

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

{
  "code": 0,
  "message": "success",
  "extMessage": null,
  "traceId": "4d5c2d6a256e46ba910e4e2f1e5420de",
  "parentTraceId": null,
  "timestamp": 1635663684042,
  "data": {
    "result": "sample-sink-bean-sink: Hello,world\nsample-bean-sink: Hello,world"
  }
}

四、source层对app服务的通讯接入封装

在Biz-SIP中间件中,source层是类似DDD中的适配层存在,支持各种通讯接入方式。
这里的例子是通过source层实现的RestController,实现对app服务(/app/sample-bean-service)的接口封装:

source层SampleRestController封装了RESTful接口:

@RestController
@RequestMapping("/rest")
public class SampleRestController {
    private SampleBeanServiceInterface sampleBeanServiceInterface = SourceClientFactory
            .getBizServiceClient(SampleBeanServiceInterface.class,"/app/sample-bean-service");

    @GetMapping(value ="/callSampleBeanSink")
    public String callSampleBeanSink(String message) {
        return this.sampleBeanServiceInterface.callSampleBeanSink(message);
    }

    @GetMapping(value ="/callSampleSinkBeanSink")
    public String callSampleSinkBeanSink(String message) {
        return this.sampleBeanServiceInterface.callSampleSinkBeanSink(message);
    }

    @GetMapping(value ="/callAllSink")
    public String callAllSink(String message) {
        return this.sampleBeanServiceInterface.callAllSink(message);
    }
}

其中,app层/app/sample-bean-service服务,是通过SourceClientFactory.getAppServiceClient()方法来封装的,这样就能支持RESTful接口的访问:

$ curl http://localhost:9001/rest/callSampleBeanSink\?message=world

sample-bean-sink: Hello,world

$ curl http://localhost:9001/rest/callSampleSinkBeanSink\?message=world

sample-sink-bean-sink: Hello,world

$ curl http://localhost:9001/rest/callAllSink\?message=world

sample-sink-bean-sink: Hello,world
sample-bean-sink: Hello,world
上一页
下一页