Actual combat of xbank project of Biz SIP Middleware -- independent use of Converter and Connector

xbank project version Library: https://gitee.com/szhengye/xbank.git

Project practice: use Converter and Connector independently

1. Use Converter and Connector in Sink

In the previous development, the Sink service is processed by directly calling the process() method of the Sink class. The process() method mainly completes the following work:

  1. Message packaging: the message is converted from the internal JSON object to the message format of sink.yml configuration convention;
  2. Message communication interaction: messages interact with external or internal systems by using the connector of sink.yml configuration convention;
  3. Message unpacking: the message is converted from the message format of the sink.yml configuration convention to an internal JSON object.

However, in the specific development, developers need more precise control and flexibly call the format Converter and communication Connector directly in the code. Take Payment1 Sink as an example, directly call the Converter and Connector to realize the same function as calling the process() method of Sink class:

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

    /* Sink.process()Original mode
    @PostConstruct
    public void init() {
        try {
            this.sink.init("payment1-sink");
        } catch (BizException e) {
            log.error("Sink1Controller Initialization failed! ",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("Post package message:\n{}", BizUtils.buildHexLog(inData));
            byte[] outData = this.connector.process(inData);
            log.debug("connector Return message:\n{}", BizUtils.buildHexLog(outData));
            JSONObject jsonObject = this.converter.unpack(outData);
            log.debug("Message after unpacking:\n{}", BizUtils.buildJsonLog(jsonObject));
/* Sink.process()Original mode
            jsonObject = this.sink.process(inMessage.getData());
 */
            return BizMessage.buildSuccessMessage(inMessage,jsonObject);
        } catch (BizException e) {
            log.debug("Server adapter execution error:{},{}",e.getCode(),e.getMessage());
            return BizMessage.buildFailMessage(inMessage,e);
        }
    }
}

2. Use Converter in Source

In the previous development, the Source service is processed by directly calling the process() method of the Source class. The process() method mainly completes the following work:
1. Message unpacking

  • 2. Execute the aggregation service location assertion rule according to the unpacking message to locate the aggregation service id
  • 3. Send a message to the aggregation integrator for processing
  • 4. Message packaging
  1. Message unpacking: the message is converted from the message format agreed in the sink.yml configuration to an internal JSON object;
  2. Executing the aggregation service location assertion rule according to the unpacking message to locate the aggregation service id;
  3. Send a message to the aggregation integrator for processing;
  4. Message packaging: the message is converted from the internal JSON object to the message format of the sink.yml configuration convention.

However, in specific development, developers need more detailed control. They can flexibly call the format Converter directly in the code. In the following example, they can call the Converter directly to realize the same function as calling the process() method of Source class:

@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));
    }
}

Call result:

$ 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>Zhang San</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 official website: http://bizsip.bizmda.com
Gitee: https://gitee.com/szhengye/biz-sip

Tags: Kubernetes Spring Boot Spring Cloud gateway mesh

Posted on Sun, 10 Oct 2021 04:29:02 -0400 by jiehuang001