Biz-SIP金融级业务中台(http://bizsip.bizmda.com))是一套基于领域驱动设计(DDD)架构,能快速构建金融级云原生架构的服务整合中间件,整合了在金融场景里锤炼出来的最佳实践。
前面的Sink服务的type属性都是“rest”,表明App服务都是通过RESTful服务来进行同步调用的。但是App服务通过还支持RabbitMQ的方式(type属性为“rabbitmq”),异步调用Sink服务,在这种情况下,App服务不会Sink服务的返回结果。
下面是App服务通过RabbitMQ异步调用Sink服务的案例流程:
在以上案例中,有2个Sink服务:
- hello-bean-sink:采用RESTful同步调用的Sink服务;
- rabbitmq-hello-bean-sink:采用RabbitMQ异步调用的Sink服务。
App服务(/bean/rabbitmq-hello)会调用上面2个Sink服务,共调用了6次,调用顺序依次为:
- hello-bean-sink
- rabbitmq-hello-bean-sink
- hello-bean-sink
- rabbitmq-hello-bean-sink
- hello-bean-sink
- rabbitmq-hello-bean-sink
具体代码和配置可以查看Biz-SIP源代码中的Sample相关测试案例(https://gitee.com/szhengye/biz-sip)
一、Sink层Sink服务的开发和配置
首先,我们需要编写一个Sink服务类(HelloBeanSinkService.java):
@Slf4j
@Service
public class HelloBeanSinkService extends AbstractSinkService implements HelloInterface {
@Override
public String hello(String message) {
log.info("hello({})",message);
return "hello,"+message;
}
}
HelloBeanSinkService类继承了AbstractSinkService类(Sink服务类一般都要继承AbstractSinkService类),实现了HelloInterface接口中的hello()方法,这个方法是在传入的message参数前,加上“hello,”串。
其次,在在Biz-SIP配置目录的sink.yml中,配置对应的Sink服务:
- id: hello-bean-sink
type: rest
processor: bean
url: http://bizsip-sample-sink/hello-bean-sink
class-name: com.bizmda.bizsip.sample.sink.service.HelloBeanSinkService
- id: rabbitmq-hello-bean-sink
type: rabbitmq
processor: bean
url: http://bizsip-sample-sink/hello-bean-sink
class-name: com.bizmda.bizsip.sample.sink.service.HelloBeanSinkService
可以看到hello-bean-sink、rabbitmq-hello-bean-sink这二个Sink服务,都关联了上面编写的HelloBeanSinkService类,是通过接口类方法来实现的Sink服务,配置中processor应设置为“bean”。二个Sink服务,一个type为rest,是通过RESTful同步调用Sink服务的,另一个type为rabbitmq,是通过RabbitMQ异步调用Sink服务的。
最后,还需要在SampleSinkApplication的应用配置文件application-local.yml中,在bizsip.sink-id配置项中,增加hello-bean-sink、rabbitmq-hello-bean-sink以便启动Sink服务:
bizsip:
config-path: /var/bizsip/config
sink-id: hello-sink,hello-bean-sink,rabbitmq-hello-bean-sink
二、App层App服务的开发和配置
首先,我们需要编写一个App服务类(RabbitmqHelloAppService.java):
@Slf4j
@Service
public class RabbitmqHelloAppService implements HelloInterface {
private HelloInterface restHelloInterface = AppClientFactory.getSinkClient(HelloInterface.class,"hello-bean-sink");;
private HelloInterface rabbitmqHelloInterface = AppClientFactory.getSinkClient(HelloInterface.class,"rabbitmq-hello-bean-sink");;
@Override
public String hello(String message) {
log.info("1:{}",this.restHelloInterface.hello("1"));
log.info("2:{}",this.rabbitmqHelloInterface.hello("2"));
log.info("3:{}",this.restHelloInterface.hello("3"));
log.info("4:{}",this.rabbitmqHelloInterface.hello("4"));
log.info("5:{}",this.restHelloInterface.hello("5"));
log.info("6:{}",this.rabbitmqHelloInterface.hello("6"));
return "hello," + message;
}
}
HelloAppService类继承了HelloInterface接口,实现了hello()方法。
HelloAppService类中还申请了分别调用“hello-bean-sink”和“rabbitmq-hello-bean-seink”这二个Sink服务的调用接口,在hello()方法中,通过这个二个接口,来调用Sink服务。
然后,在app.yml中,需要配置对应的App服务:
- app-service-id: /bean/rabbitmq-hello
type: bean-service
class-name: com.bizmda.bizsip.sample.app.service.RabbitmqHelloAppService
三、启动应用进行测试
启动SampleSinkApplication、SampleAppApplication应用,通过OpenAPI接口进行测试:
$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/rabbitmq-hello" -X POST --data '{"methodName":"hello","params":["world"]}' http://localhost:8888/api|jq
{
"code": 0,
"message": "success",
"extMessage": null,
"appServiceId": "/bean/rabbitmq-hello",
"traceId": "4da99c8f35f341e38ab03deea5974c78",
"parentTraceId": null,
"timestamp": 1648303982556,
"data": {
"result": "hello,world"
}
}
SampleAppApplication中的RabbitmqHelloAppService类打印日志:
[bizsip-integrator:192.169.1.103:8888] 22:13:02 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 1:hello,1
[bizsip-integrator:192.169.1.103:8888] 22:13:02 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 2:null
[bizsip-integrator:192.169.1.103:8888] 22:13:03 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 3:hello,3
[bizsip-integrator:192.169.1.103:8888] 22:13:03 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 4:null
[bizsip-integrator:192.169.1.103:8888] 22:13:03 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 5:hello,5
[bizsip-integrator:192.169.1.103:8888] 22:13:03 INFO 67141 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8888-exec-1] c.b.b.s.a.s.RabbitmqHelloAppService 6:null
SampleSinkApplication打印日志:
[bizsip-sample-sink:192.169.1.103:8001] 22:13:02 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8001-exec-1] c.b.b.s.s.service.HelloBeanSinkService hello(1)
[bizsip-sample-sink:192.169.1.103:8001] 22:13:02 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8001-exec-2] c.b.b.s.s.service.HelloBeanSinkService hello(3)
[bizsip-sample-sink:192.169.1.103:8001] 22:13:03 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [http-nio-8001-exec-3] c.b.b.s.s.service.HelloBeanSinkService hello(5)
[bizsip-sample-sink:192.169.1.103:8001] 22:13:03 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [SimpleAsyncTaskExecutor-1] c.b.b.s.s.service.HelloBeanSinkService hello(2)
[bizsip-sample-sink:192.169.1.103:8001] 22:13:03 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [SimpleAsyncTaskExecutor-2] c.b.b.s.s.service.HelloBeanSinkService hello(4)
[bizsip-sample-sink:192.169.1.103:8001] 22:13:03 INFO 67149 [4da99c8f35f341e38ab03deea5974c78] [SimpleAsyncTaskExecutor-3] c.b.b.s.s.service.HelloBeanSinkService hello(6)
可以看到先执行了第1、3、5步同步调用的hello-bean-sink,然后再执行第2、4、6步异步调用的rabbitmq-hello-bean-sink。
Biz-SIP官方网站:http://bizsip.bizmda.com
Gitee:https://gitee.com/szhengye/biz-sip