Getting started with HTTP to SOFARPC
This topic describes how to create and publish an HTTP API service. API Gateway converts the protocol and forwards requests to a backend service cluster that uses the SOFARPC protocol.
Procedure
Read Quick Start overview to understand the role of API Gateway and the complete workflow.

Sample project
To quickly try the API Gateway HTTP to SOFARPC API service, you can download the sample project for this topic.
Step 1: Write the SOFARPC server
Writing an SOFARPC server has no special requirements. You can follow the standard SOFARPC tutorial. This example shows how to write a simple UserService.
For more information about how to create a SOFABoot project, see Create a project.
public interface SampleService {
User message();
}
public class SampleServiceImpl implements SampleService {
@Override
public User message() {
User user = new User();
user.setAge(18);
user.setName("tom");
return user;
}
}
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}Publish this service as a SOFARPC service:
<bean id="sampleService" class="com.alipay.gateway.endpoint.impl.SampleServiceImpl"/>
<!-- Publish the RPC service -->
<sofa:service ref="sampleService" interface="com.alipay.xxxxxxxx.SampleService">
<sofa:binding.bolt/>
</sofa:service>API Gateway now supports passing HTTP headers to SOFARPC baggage. In an HTTP-to-SOFARPC conversion, you can add the parameters from the HTTP header to the baggage parameter in the agreed-upon format:
request.getHeaderParams().put("baggage_context.pass_through_parameter_name","pass_through_data")Then, in the SOFARPC service, you can retrieve the parameter value using the Baggage function.
RpcInvokeContext context = RpcInvokeContext.getContext();
context.getRequestBaggage("pass_through_parameter_name")Step 2: Create a system cluster
Log on to the Scalable Open Financial Architecture (SOFA) console.
In the navigation pane on the left, choose Unified API Gateway > API Publishing > System Clusters.
On the System Clusters page, click Create System Cluster in the upper-right corner.
In the pop-up window, configure the following parameters:
System Cluster Name: Required. This parameter is used to identify the system cluster. For this example, enter
sofarpc.Protocol Type: HTTP and SOFARPC are supported. For this example, select
SOFARPC.Address Configuration Method: Select Manual Configuration to manually configure the IP addresses or domain names of the system cluster.
IP Address/Domain Name: The format is
Address:Port. For this example, enter the IP address127.0.0.1and port number12200.IP address or domain name:
The IP address format is
(1-255).(0-255).(0-255).(0-255).A domain name can contain letters, numbers, or hyphens (-). The total length cannot exceed 200 characters.
Port: The default port for HTTP is 80. The default port for SOFARPC is 12200. The port number must be between 1 and 65535.
Description: Optional. This parameter describes the purpose of the system cluster. The description can be up to 64 characters long and can be left empty.

Click OK.
Step 3: Create an API group
In the navigation pane on the left, choose API Publishing > API Groups to go to the group list.
Click Create Group in the upper-right corner of the API group list.
In the pop-up window, enter the API group information:
Group Name: Required. This parameter is used to identify the API group. The name can contain English letters, Chinese characters, numbers, underscores (_), and hyphens (-). The name can be up to 32 characters long. For this example, enter
sofarpc.Description: Optional. This parameter describes the purpose of the API group. The description can be up to 64 characters long and can be left empty.

Click OK.
Step 4: Create and publish an API
In the navigation pane on the left, go to the API Publishing > API Management page and click Create API in the upper-right corner.
On the new page, select HTTP API as the type and click Create.
In the Define API step, configure the following parameters:
API Group: Required. Select the API group that you created in the previous step.
API Name: Required. This parameter is used to identify the API. The name can contain English letters, Chinese characters, numbers, underscores (_), and hyphens (-). The name can be up to 32 characters long. For this example, enter
sofarpc.NoteAPI names must be unique within the same API group.
Description: Optional. This parameter describes the purpose of the API. The description can be up to 64 characters long.
API Authorization Application Type: Required. Specifies the type of application that can subscribe to and call this API. For this example, select Application.
URI of the request: Required. The URL for the request that locates the resource. For this example, enter
/sofarpc.Path Matching Rule: Select Exact Match. The path used in the call must exactly match the path that you entered. For more information, see Path matching rules.
Method: Required. The backend call to the SOFARPC interface must pass parameters. Therefore, select
POST. Do not select theGETmethod.NoteSOFARPC requires payload information to deserialize data. GET requests cannot carry a valid payload. Therefore, converting HTTP GET requests to SOFARPC requests is not supported.
Message Type: The media type information in the request and response. Select
application/json.Message Encoding: The character set of the text content that the client accepts. Select
UTF-8.Security Authentication: Select Key Authentication. This means that the subscribing application must use a key for identity authentication when accessing the authorized API.
Request Parameters: Optional. For more information, see API property description > Request parameters.
Response Parameters: Optional. For more information, see API property description > Response parameters.

Click Next. On the backend configuration page, select a Backend Configuration Type and enter the backend configuration information. For this example, select System Cluster and configure the cluster details.
Protocol Type: Required. The communication protocol used by the backend service to which API Gateway forwards requests. For this example, select
SOFARPC.Interface Name: The API interface name. The name can contain English letters, numbers, periods (.), colons (:), and at signs (@), and must start with a lowercase letter. For this example, enter
com.alipay.xxxxxxxx.SampleService.Method Name: The API service method name. The name can contain letters and numbers and must start with a letter. For this example, enter
message.Timeout: Required. The API request timeout period in milliseconds (ms). You can keep the default value.
Routing Policy: Required. The routing policy used by API Gateway after it receives the request. For this example, select Route by Request Path, which means direct forwarding.
System Cluster: Required. Select the system cluster that you created in Step 2, which is
sofarpc.
Click Create.
After the API is created, click Publish Now to publish it.
Step 5: Create an application
In the navigation pane on the left, go to the API Subscription > Application Management page and click Create Application in the upper-right corner.
In the Create Application window, set Application Type to Application.
Enter an Application Name to identify the application. For this example, enter
hello.
Click OK.
After the application is added, the subscriber must retrieve the application's APPID from the application product page. After retrieving the APPID, provide it to the publisher of the API that you want to subscribe to. This grants you access to the API.
Step 6: Create an authorization object
In the navigation pane on the left, go to the API Publishing > Authorization Management page and click Create Authorization Object in the upper-right corner.
In the Create Authorization Object window, configure the authorization information for the subscribing application.
Application Source: Required. Select Internal System. This grants subscription authorization to applications in the current environment of the current tenant.
Application Name: Enter the name of the application that you created in the previous step, such as
hello. The system automatically retrieves its APPID and type.Company/Department: This parameter identifies the company or department that the application belongs to. This can be left empty.
Description: Enter notes for this authorization object. This can be left empty.

Click OK.
Step 7: Attach an authorization object
On the API Management page, find the API you want to subscribe to and go to its product page.
On the Authorization Objects tab, click Attach Authorization Object.
In the Attach Authorization Object window, find the authorization application that you just created.

Select the application and click OK to complete the API authorization.
Step 8: Write the API call code
Retrieve the following service configuration information:
Retrieve the application's keys (AccessKey/Secret Key) from the subscriber application's product page.

Retrieve the API's domain name (host), request path (path), and method from the API product page.

Write the call code. The following is an example:
NoteIf the API has key authentication enabled, you must also configure the corresponding AccessKey and Secret Key in your project. You can use startup parameters and environment variables.
# The ak and sk for the client request are the key information of the application. gateway.accessKey=<SOFA_ACCESS_KEY_ID> gateway.secretKey=<SOFA_ACCESS_KEY_SECRET> apigateway.url=<yourGatewayURL>public class GatewayTest { private String subAppAccessKey = "${gateway.accessKey}"; private String subAppSecretKey = "${gateway.secretKey}"; private String gatewayUrl = "http://mvwmni87dqwc****.apigateway.inc.alipay.net"; private ApiClient apiClient; @Before public void initClient() { // Initialize the request client ApiSecretKey apiSecretKey = new ApiSecretKey(subAppAccessKey, subAppSecretKey); List<ApiSecretKey> secretKeys = new ArrayList<>(); secretKeys.add(apiSecretKey); apiClient = new DefaultApiClient(gatewayUrl, secretKeys); } @Test public void testNoSignHttp() { ParamPostRequest request = new ParamPostRequest(); request.setPath("sofarpc"); // Specifies whether to verify the signature of the response request.setClientCheckSign(false); ApiResponse response = apiClient.execute(request); System.out.println(JSON.toJSONString(response)); } }After you make the call, the following result is returned:
{"body":"{\"name\":\"tom\",\"age\":18}"}




