Biz-SIP中间件之xbank项目实战(7)——独立使用Converter和Connector

xbank项目版本库:https://gitee.com/szhengye/xbank.git

项目实践:独立使用Converter和Connector

1. 在Sink中使用Converter和Connector

在前面的开发中,Sink服务是直接调用Sink类的process()方法来处理的,process()方法主要完成了以下工作:

  1. 消息打包:消息从内部JSON对象转换成sink.yml配置约定的消息格式;
  2. 消息通讯交互 :消息用sink.yml配置约定的connector来和外部或内部系统进行交互;
  3. 消息解包:消息从sink.yml配置约定的消息格式,转换成内部JSON对象。

但在具体开发中,开发人员需要更精细的控制,直接在代码中灵活调用格式转换器Converter和通讯连接器Connector,下面以payment1-sink为例,直接调用Converter和Connector,实现和调用Sink类的process()方法同样的功能:

@RestController
public class Sink1Controller {
    /* Sink.process()原有方式
    @Autowired
    private Sink sink;
    */
    private Converter converter = Converter.getSinkConverter("payment1-sink");
    private Connector connector = Connector.getSinkConnector("payment1-sink");

    /* Sink.process()原有方式
    @PostConstruct
    public void init() {
        try {
            this.sink.init("payment1-sink");
        } catch (BizException e) {
            log.error("Sink1Controller初始化失败!",e);
        }
    }
     */
    
    @PostMapping(value = "/sink1", consumes = "application/json", produces = "application/json")
    public BizMessage<JSONObject> doService(@RequestBody BizMessage<JSONObject> inMessage, HttpServletResponse response) {
        try {
            byte[] inData = this.converter.pack(inMessage.getData());
            log.debug("打包后消息:\n{}", BizUtils.buildHexLog(inData));
            byte[] outData = this.connector.process(inData);
            log.debug("connector返回消息:\n{}", BizUtils.buildHexLog(outData));
            JSONObject jsonObject = this.converter.unpack(outData);
            log.debug("解包后消息:\n{}", BizUtils.buildJsonLog(jsonObject));
/* Sink.process()原有方式
            jsonObject = this.sink.process(inMessage.getData());
 */
            return BizMessage.buildSuccessMessage(inMessage,jsonObject);
        } catch (BizException e) {
            log.debug("服务端适配器执行出错:{},{}",e.getCode(),e.getMessage());
            return BizMessage.buildFailMessage(inMessage,e);
        }
    }
}

2. 在Source中使用Converter

在前面的开发中,Source服务是直接调用Source类的process()方法来处理的,process()方法主要完成了以下工作:
1.消息解包

  • 2.根据解包消息执行聚合服务定位断言规则,定位聚合服务id
  • 3.发送消息给聚合整合器处理
  • 4.消息打包
  1. 消息解包:消息从sink.yml配置约定的消息格式,转换成内部JSON对象;
  2. 根据解包消息执行聚合服务定位断言规则,定位聚合服务id;
  3. 发送消息给聚合整合器处理;
  4. 消息打包:消息从内部JSON对象转换成sink.yml配置约定的消息格式。

但在具体开发中,开发人员需要更精细的控制,直接在代码中灵活调用格式转换器Converter,下面的例子,直接调用Converter,实现和调用Source类的process()方法同样的功能:

@RestController
@RequestMapping("/personal")
public class PersonalController  {
    private PersonalAppInterface personalAppInterface = SourceClientFactory
            .getBizServiceClient(PersonalAppInterface.class,"app/personal");
    private BizMessageInterface payment1SinkInterface = SourceClientFactory
            .getBizServiceClient(BizMessageInterface.class,"sink/payment1");
    private Converter converter = Converter.getSourceConverter("xml-source");

    @PostMapping(value = "/getCustomerAndAccountList", consumes = "application/xml", produces = "application/xml")
    public String getCustomerAndAccountList(@RequestBody String inMessage) throws BizException {
        JSONObject jsonObject = this.converter.unpack(inMessage.getBytes());
        String customerId = (String)jsonObject.get("customerId");
        CustomerAndAccountList customerAndAccountList = this.personalAppInterface.getCustomerAndAccountList(customerId);
        jsonObject = JSONUtil.parseObj(customerAndAccountList);
        return new String(this.converter.pack(jsonObject));
    }

    @PostMapping(value = "/getAccountListByCustomerId", consumes = "application/xml", produces = "application/xml")
    public String getAccountListByCustomerId(@RequestBody String inMessage) throws BizException {
        JSONObject jsonObject = this.converter.unpack(inMessage.getBytes());
        String customerId = (String)jsonObject.get("customerId");
        List<Account> accountList = this.personalAppInterface.getAccountListByCustomerId(customerId);
        jsonObject = new JSONObject();
        jsonObject.set("result",JSONUtil.parseArray(accountList));
        return new String(this.converter.pack(jsonObject));
    }
}

调用结果:

$ curl -H "Content-Type:application/xml" -X POST --data '<customerId>001</customerId>' http://localhost:9002/personal/getCustomerAndAccountList

<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><accountList><accountId>0001</accountId><balance>24</balance><customerId>001</customerId></accountList><accountList><accountId>0002</accountId><balance>200</balance><customerId>001</customerId></accountList><customer><sex>1</sex><customerName>张三</customerName><customerId>001</customerId><age>30</age></customer></root>

$ curl -H "Content-Type:application/xml" -X POST --data '<customerId>001</customerId>' http://localhost:9002/personal/getAccountListByCustomerId

<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><result><accountId>0001</accountId><balance>24</balance><customerId>001</customerId></result><result><accountId>0002</accountId><balance>200</balance><customerId>001</customerId></result></root>

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

posted @ 2021-10-10 16:34  抽游烟鸡  阅读(52)  评论(0)    收藏  举报