This Quick Start shows you how to register and publish an MPC API service that mobile clients can call. The process consists of six steps:
Server-side development
Import gateway packages
Import the following packages into your project's main pom.xml file. You can skip this step if your project already has these dependencies. Use the latest version for the mobilegw-unify series dependencies. The latest version is 1.0.5.20201010.
<!-- mobilegw unify dependency-->
<dependency>
<groupId>com.alipay.gateway</groupId>
<artifactId>mobilegw-unify-spi-mpc</artifactId>
<version>${the-latest-version}</version>
</dependency>
<dependency>
<groupId>com.alipay.gateway</groupId>
<artifactId>mobilegw-unify-spi-adapter</artifactId>
<version>${the-latest-version}</version>
</dependency>
<dependency>
<groupId>com.alipay.gateway</groupId>
<artifactId>mobilegw-unify-log</artifactId>
<version>${the-latest-version}</version>
</dependency>
<dependency>
<groupId>com.alipay.hybirdpb</groupId>
<artifactId>classparser</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.alipay.mpaaschannel</groupId>
<artifactId>common</artifactId>
<version>2.4.2019040801</version>
</dependency>
<dependency>
<groupId>com.alipay.mpaaschannel</groupId>
<artifactId>tenant-client</artifactId>
<version>2.4.2019040801</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72_noneautotype</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>hessian</artifactId>
<version>3.3.6</version>
</dependency>Define and implement the service interface
Define the service interface, for example,
com.alipay.xxxx.MockRpc.NoteDefine input parameters in the method definition as Value Objects (VOs). This lets you add parameters later by modifying the VO without changing the method's signature.
For more information about service interface definition specifications, see Service interface definition specifications.
com.alipay.xxxx.MockRpcImplprovides the implementation for the interface.
Define the OperationType
Add the @OperationType annotation to the service interface method to define the published service. The @OperationType annotation has three parameters. For easier maintenance, we recommend that you specify all of them:
value: The unique identifier of the interface. This value must be globally unique within the gateway. To avoid conflicts with other services that could prevent service registration, use the following detailed format:
Organization.ProductDomain.Product.Subproduct.Operation.name: The name of the interface.
desc: The description of the interface.
The following is an example:
public interface MockRpc {
@OperationType(value="com.alipay.mock", name="MPC mock interface", desc="Complex mock interface")
Resp mock(Req s);
@OperationType(value="com.alipay.mock2", name="xxx", desc="xxx")
String mock2(String s);
}
public static class Resp {
private String msg;
private int code;
// ignore getter & setter
}
public static class Req {
private String name;
private int age;
// ignore getter & setter
}Declare the API service
In this step, declare the RPC service as an externally available API using the Service Provider Interface (SPI) package provided by the gateway. The following five parameters are required:
registryUrl: The address of the registry center. For the shared financial technology platform, the address is
mpcpub.mpaas.cn-hangzhou.aliyuncs.com.appName: The application name.
workspaceId: The ID of the workspace where the application is located.
projectName: The project name of the tenant to which the application belongs.
privateKeyPath: The ClassPath of the RSA private key. This key is used for authentication when establishing a connection with mpaaschannel.
ImportantThe private key must correspond to the application public key that is configured in the console. If you have not generated a key or configured the application public key in the console, see Configure the application public key.
Place the private key in
/META-INF/config/rsa-mpc-pri-key-{env}.der, where{env}represents different environments, such asdev,sit, andprod.
You can declare the API service using Spring or Spring Boot.
Spring declaration method
In the Spring configuration file of the corresponding bundle, declare the Spring Bean for the service. The following is an example:
<bean id="mockRpc" class="com.alipay.gateway.spi.mpc.test.MockRpcImpl"/>In the Spring configuration file of the corresponding bundle, declare a Spring Bean of the
com.alipay.gateway.spi.mpc.MpcServiceStartertype.MpcServiceStarterregisters all beans that have the@OperationTypeannotation to the specified registry center using the mpaaschannel protocol. The following is an example:<bean id="mpcServiceStarter" class="com.alipay.gateway.spi.mpc.MpcServiceStarter"> <property name="registryUrl" value="${registry_url}"/> <property name="appName" value="${app_name}"/> <property name="workspaceId" value="${workspace_id}"/> <property name="projectName" value="${project_name}"/> <property name="privateKeyPath" value="${privatekey_path}"/> </bean>
Spring Boot declaration method
Declare the Spring Bean for the service using an annotation. The following is an example:
@Service public class MockRpcImpl implements MockRpc{ }Declare a Spring Bean of the
com.alipay.gateway.spi.mpc.MpcServiceStartertype using an annotation.MpcServiceStarterregisters all beans that have the@OperationTypeannotation to the specified registry center using the mpaaschannel protocol. The following is an example:@Configuration public class MpaaschannelDemo { @Bean(name="mpcServiceStarter") public MpcServiceStarter mpcServiceStarter(){ MpcServiceStarter mpcServiceStarter = new MpcServiceStarter(); mpcServiceStarter.setWorkspaceId("${workspace_id}"); mpcServiceStarter.setAppName("${app_name}"); mpcServiceStarter.setRegistryUrl("${registry_url}"); mpcServiceStarter.setProjectName("${project_name}"); mpcServiceStarter.setPrivateKeyPath("${privatekey_path}"); return mpcServiceStarter; } }
Configure MPC logs
To facilitate troubleshooting, you can configure MPC-related logs. The following example shows a log4j configuration.
<!-- [MPC Logger] tenant link, records connection establishment and settings information -->
<appender name="MPC-TENANT-LINK-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${log_root}/mpaaschannel/tenant-link.log"/>
<param name="append" value="true"/>
<param name="encoding" value="${file.encoding}"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%X{remoteAddr}][%X{uniqueId}] %-5p %c{2} - %m%n"/>
</layout>
</appender>
<!-- [MPC Logger] records stream-related data (including a pair of tenant stream <-> component stream) -->
<appender name="MPC-STREAM-DATA-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${log_root}/mpaaschannel/stream-data.log"/>
<param name="append" value="true"/>
<param name="encoding" value="${file.encoding}"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%X{remoteAddr}][%X{uniqueId}] %-5p %c{2} - %m%n"/>
</layout>
</appender>
<!-- [MPC Logger] tenant log -->
<logger name="TENANT-LINK-DIGEST" additivity="false">
<level value="INFO" />
<appender-ref ref="MPC-TENANT-LINK-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
</logger>
<!-- [MPC Logger] component log -->
<logger name="STREAM-DATA-DIGEST" additivity="false">
<level value="INFO" />
<appender-ref ref="MPC-STREAM-DATA-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
</logger>Configure the application public key
Go to the Interface Key configuration page in the console.
Log on to the console. Under Products & Services, select Mobile PaaS to open the Mobile PaaS homepage.
Switch to the correct workspace, and then click the name of the application that requires access to the API service.
In the navigation pane on the left, select Interface Key to open the interface key configuration page.
Click the Configure button. Enter the public key content, remove any trailing spaces or blank lines, and then click Submit.
NoteThe RSA key must be 2048 bits and Base64 encoded.
To generate an RSA key:
Install the
OpenSSLtool.Run the following commands to generate the private key file
private_key_base64.derand the public key filepublic_key_base64.der.* ### 1. Generate a 2048-bit RSA private key * $ openssl genrsa -out private_key.pem 2048 * * ### 2. Convert private Key to PKCS#8 format (so Java can read it) * $ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der -nocrypt * * ### 3. Output public key portion in DER format (so Java can read it) * $ openssl rsa -in private_key.pem -pubout -outform DER -out public_key.der * * ### 4. change to base64: * ## Generated private key, to be configured in the backend application * $ openssl base64 -in private_key.der -out private_key_base64.der * ## Generated public key * $ openssl base64 -in public_key.der -out public_key_base64.der * * ### remember to clear the whitespace chars and line breaks before submit!!!
Register an API group
In the console, choose Mobile Gateway from the navigation pane on the left to open the mobile gateway management page.
Select the API Group tab to open the API group list page, and then click the Create API Group button.
In the dialog box that appears, fill in the form.
Group Type: Select MPC.
API Group: Required. The English name of the business system that provides the service. The API group name must be the same as the application name of the registered API service.
Project Name: Required. By default, the project name of the current environment is used.
Timeout: Optional. The timeout period for sending requests to the business system, in milliseconds. The default value is
3000 ms.
Click OK to submit. For more information about how to refine the API group configuration, see Configure a group.
Create an API
Select the API Management tab to open the API list page, and then click the Create API button.
In the dialog box that appears, set API Type to MPC, select an API Group, select the required services from the operationType list, and then click Confirm.
Configure the API service
In the Operation column of the API list, click Configure to open the API configuration page.
In the API configuration area, click Modify to edit the parameters. When you are finished, click Save.
NoteFor a quick start, you can disable Signature Verification in the Advanced Configuration section. For more information about signature verification, see Signature verification.
For more information about API configuration, see API configuration.
In the upper-right corner, make sure that the switch is in the Enabled state. Only enabled API services can be called.
Test the API service
For more information, see API testing.
Generate a client SDK
For more information, see Code generation.
Results
After you complete these steps, the API service is ready to be called by clients. For more information about client-side development, see the following Client Development Guides: