Biz-SIP金融级业务中台(http://bizsip.bizmda.com))是一套基于领域驱动设计(DDD)架构,能快速构建金融级云原生架构的服务整合中间件,整合了在金融场景里锤炼出来的最佳实践。
Biz-SIP业务中台在运行时,会把相关的交易日志通过RabbitMQ消息中间件发送出来,开发人员可以实现这些交易日志的接收和处理。
案例要求:
发送不同类型的Biz-SIP交易,交易日志接收模块会收到不同类型的交易日志:
具体代码和配置可以查看Biz-SIP源代码中的Sample相关测试案例(https://gitee.com/szhengye/biz-sip)
一、App层的配置
在application-local.yml配置文件中,设置bizsip.rabbitmq-log属性为“success”,表示发送成功、挂起、失败类型的交易日志,即所有的交易日志:
bizsip:
config-path: /var/bizsip/config
rabbitmq-log: success
二、交易日志处理应用的开发和配置
首先,编写交易日志侦听器:
@Slf4j
@Service
public class AppLogQueueListener {
public static final String APP_LOG_QUEUE = "queue.bizsip.applog";
private static final String[] APP_LOG_TYPE={"0-App服务成功 ","1-App服务失败 ","2-App服务挂起 ","3-Sink服务成功","4-Sink服务失败"};
private Jackson2JsonMessageConverter jackson2JsonMessageConverter =new Jackson2JsonMessageConverter();
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = APP_LOG_QUEUE, durable = "true", autoDelete = "false"),
exchange = @Exchange(value = BizConstant.BIZSIP_LOG_EXCHANGE, type = ExchangeTypes.DIRECT, durable = "true", autoDelete = "false"),
key = BizConstant.BIZSIP_LOG_ROUTING_KEY))
public void onMessage(Message message) {
try {
this.process(message);
}
catch (Exception e) {
log.error("App服务日志侦听器出错!",e);
}
}
private void process(Message message) {
Map<String,Object> map = (Map<String,Object>)jackson2JsonMessageConverter.fromMessage(message);
int type = (int)map.get("type");
BizMessage<JSONObject> inBizMessage = new BizMessage<>((Map) map.get("request"));
BizMessage<JSONObject> outBizMessage = new BizMessage<>((Map) map.get("response"));
log.info("\n{} App服务ID:{} 返回码:{}-{} 平台流水号:{}\n请求报文:{}\n响应报文:{}",
APP_LOG_TYPE[type],inBizMessage.getAppServiceId(),
outBizMessage.getCode(),outBizMessage.getMessage(),inBizMessage.getTraceId(),
inBizMessage.getData(),outBizMessage.getData());
return;
}
}
接着,编写应用启动类SampleAppLogApplication.java,以及相关的application*.yml配置文件,特别注意application-local.yml中相关RabbitMQ的部分配置:
rabbitmq:
virtual-host: /
host: rabbitmq.bizmda.com
port: 5672
username: admin
password: admin
listener:
simple:
concurrency: 5
max-concurrency: 15
prefetch: 10
三、启动应用进行测试
启动SampleAppApplication应用、SampleSinkApplication应用、SampleSourceApplication应用,以及SampleAppLogApplication应用,通过开放平台接口发起请求,进行一系列的测试:
1、发起最简单的App服务
$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample1" -X POST --data '{"accountNo":"62001818","sex":"0","email":"123232@163.com","mobile":"18601872345"}' http://localhost:8888/api|jq
{
"code": 0,
"message": "success",
"extMessage": null,
"appServiceId": "/bean/sample1",
"traceId": "4fde178ff7b040f5adfc7a2c1b4b18b6",
"parentTraceId": null,
"timestamp": 1649128499806,
"data": {
"sex": "0",
"mobile": "18601872345",
"accountNo": "62001818",
"email": "123232@163.com"
}
}
SampleAppLogApplication应用输出日志:
[sample-app-log:0.0.0.0:8100] 11:14:59 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.b.b.s.a.listener.AppLogQueueListener
0-App服务成功 App服务ID:/bean/sample1 返回码:0-success 平台流水号:4fde178ff7b040f5adfc7a2c1b4b18b6
请求报文:{"sex":"0","mobile":"18601872345","accountNo":"62001818","email":"123232@163.com"}
响应报文:{"sex":"0","mobile":"18601872345","accountNo":"62001818","email":"123232@163.com"}
2、通过RabbitMQ异步调用Sink服务
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": "005bacd128624473a08f3d0eb725baf1",
"parentTraceId": null,
"timestamp": 1649128731865,
"data": {
"result": "hello,world"
}
}
SampleAppLogApplication应用输出日志:
[sample-app-log:0.0.0.0:8100] 11:18:52 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.b.b.s.a.listener.AppLogQueueListener
0-App服务成功 App服务ID:/bean/rabbitmq-hello 返回码:0-success 平台流水号:005bacd128624473a08f3d0eb725baf1
请求报文:{"methodName":"hello","params":["world"]}
响应报文:{"result":"hello,world"}
[sample-app-log:0.0.0.0:8100] 11:18:52 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.b.b.s.a.listener.AppLogQueueListener
3-Sink服务成功 App服务ID:/bean/rabbitmq-hello 返回码:0-success 平台流水号:005bacd128624473a08f3d0eb725baf1
请求报文:{"methodName":"hello","className":"com.bizmda.bizsip.sample.sink.api.HelloInterface","params":["4"]}
响应报文:{"result":"hello,4"}
[sample-app-log:0.0.0.0:8100] 11:18:52 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.b.b.s.a.listener.AppLogQueueListener
3-Sink服务成功 App服务ID:/bean/rabbitmq-hello 返回码:0-success 平台流水号:005bacd128624473a08f3d0eb725baf1
请求报文:{"methodName":"hello","className":"com.bizmda.bizsip.sample.sink.api.HelloInterface","params":["6"]}
响应报文:{"result":"hello,6"}
[sample-app-log:0.0.0.0:8100] 11:18:52 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.b.b.s.a.listener.AppLogQueueListener
3-Sink服务成功 App服务ID:/bean/rabbitmq-hello 返回码:0-success 平台流水号:005bacd128624473a08f3d0eb725baf1
请求报文:{"methodName":"hello","className":"com.bizmda.bizsip.sample.sink.api.HelloInterface","params":["2"]}
响应报文:{"result":"hello,2"}
3、调用App延迟服务
$ curl -H "Content-Type:application/json" -H "Biz-Service-Id:/bean/sample15" -X POST --data '{"maxRetryCount":3,"result":"fail"}' http://localhost:8888/api|jq
{
"code": 0,
"message": "success",
"extMessage": null,
"appServiceId": "/bean/sample15",
"traceId": "368fdbfa6db440b3bb3f2f5bef1363b7",
"parentTraceId": null,
"timestamp": 1649128965634,
"data": {
"maxRetryCount": 3,
"result": "fail"
}
}
SampleAppLogApplication应用输出日志:
[sample-app-log:0.0.0.0:8100] 11:22:45 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-1] c.b.b.s.a.listener.AppLogQueueListener
0-App服务成功 App服务ID:/bean/sample15 返回码:0-success 平台流水号:368fdbfa6db440b3bb3f2f5bef1363b7
请求报文:{"maxRetryCount":3,"result":"fail"}
响应报文:{"maxRetryCount":3,"result":"fail"}
[sample-app-log:0.0.0.0:8100] 11:22:45 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-5] c.b.b.s.a.listener.AppLogQueueListener
2-App服务挂起 App服务ID:/bean/sample15 返回码:0-null 平台流水号:368fdbfa6db440b3bb3f2f5bef1363b7
请求报文:{"maxRetryCount":3,"result":"fail"}
响应报文:{"maxRetryCount":3,"result":"fail"}
[sample-app-log:0.0.0.0:8100] 11:22:45 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-2] c.b.b.s.a.listener.AppLogQueueListener
2-App服务挂起 App服务ID:/bean/sample15-delay 返回码:8-重试App延迟服务 平台流水号:0697aca84a61439581bf75271453d191
请求报文:{"maxRetryCount":3,"result":"fail"}
响应报文:null
[sample-app-log:0.0.0.0:8100] 11:22:46 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-4] c.b.b.s.a.listener.AppLogQueueListener
2-App服务挂起 App服务ID:/bean/sample15-delay 返回码:8-重试App延迟服务 平台流水号:0697aca84a61439581bf75271453d191
请求报文:{"maxRetryCount":3,"result":"fail"}
响应报文:null
[sample-app-log:0.0.0.0:8100] 11:22:48 INFO 60720 [] [org.springframework.amqp.rabbit.RabbitListenerEndpointContainer#0-3] c.b.b.s.a.listener.AppLogQueueListener
1-App服务失败 App服务ID:/bean/sample15-delay 返回码:899-其它错误 平台流水号:0697aca84a61439581bf75271453d191
请求报文:{"maxRetryCount":3,"result":"fail"}
响应报文:null
Biz-SIP网站:http://bizsip.bizmda.com
Gitee代码库:https://gitee.com/szhengye/biz-sip