Practice exercise: Develop an xrpc service using MOSN mecha

更新时间:
复制 MD 格式

xrpc protocol specification

The standard xrpc protocol message format is as follows:

The message consists of a fixed message length of 10 bytes, followed by XML text. If the string for the message length is shorter than 10 digits, it is padded with leading zeros. The format of the XML text is as follows:

<Service>
  <Header>
    <ServiceCode> ... </ServiceCode>
    <ExternalReferenceId> ... </ExternalReferenceId>
    <RequestFlag>...</RequestFlag>
    <Response>
      <ReturnCode>...</ReturnCode>
      <ReturnMessage>...</ReturnMessage>
    </Response>
  </Header>
  <Body>
    <key> ... </key>
  </Body>
</Service>

The <Service> tag consists of a <Header> and a <Body>. The <Header> can contain any number of keys.

Note

If an abnormal request is intercepted by MOSN, a <Response> tag is inserted into the <Header> to specify the reason for the failure.

The xrpc header contains information about service invocation:

No.

Contains key

Description

Example

1

ServiceCode

The service invocation interface.

CIMT000080

2

ExternalReferenceId

The service request ID (string).

2022-03-31,19:35:1648726547

3

RequestFlag

Identifies the message type:

  • 0: Request

  • 1: Response

0

The following example shows a request message:

0000000238<Service>
  <Header>
    <ServiceCode>CIMT000080</ServiceCode>
    <ExternalReferenceId>2022-03-31,19:35:1648726547</ExternalReferenceId>
    <RequestFlag>0</RequestFlag>
  </Header>
  <Body>
    <userId>yiji</userId>
  </Body>
</Service>

The following is a sample normal response:

0000000299<Service>
  <Header>
    <ServiceCode>CIMT000080</ServiceCode>
    <ExternalReferenceId>2022-03-31,19:35:1648726547</ExternalReferenceId>
    <RequestFlag>1</RequestFlag>
  </Header>
  <Body>
    <userId>yiji</userId>
    <title>developer</title>
    <address>hangzhou</address>
  </Body>
</Service>

xrpc protocol invocation guide

The source code for the xrpc server is available at:
https://github.com/mosn/extensions/tree/master/java-quickstart/java-plugin-quickstart
  • xrpc-server is a server-side program that starts and listens on port 7755.

  • xrpc does not have a client application. You can use a shell script to simulate a client and send requests.

The following is the request template for the xrpc client shell (request_template.txt):

<Service>
  <Header>
    <ServiceCode>CIMT000080</ServiceCode>
    <ExternalReferenceId>EXT_REF</ExternalReferenceId>
    <RequestFlag>0</RequestFlag>
  </Header>
  <Body>
    <userId>yiji</userId>
  </Body>
</Service>

A script is automatically generated to allow you to manually publish and subscribe to services after MOSN starts. Run this script the first time you start MOSN:

cd ~/go/src/quick-start-practice/configs/codecs/xrpc/
bash ./auto_pub_sub.sh

Practice exercise

Use the information from the preceding sections to develop xrpc with MOSN mecha.

Note: When you implement the xrpc protocol, focus on the fields that are decoded and inserted into the header:

No.

Protocol element

Corresponding field

Example

1

Service identity

ServiceCode

CIMT000080

2

Request type

RequestFlag

0

Watch the following video to learn about the steps and key considerations for xrpc protocol development: