In distributed microservices architectures, backend services often use an RPC framework, such as Dubbo. To expose a Dubbo service as a RESTful API, the Cloud-native Gateway provides HTTP-to-Dubbo protocol conversion. This topic shows you how to configure protocol conversion to access a Dubbo service by using an HTTP request.
Currently, only the Dubbo framework for Java is supported.
Prerequisites
Procedure
-
Log on to the MSE console. In the top navigation bar, select a region.
-
In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the ID of the gateway.
In the left-side navigation pane, click Routes.
Click Add Route and configure the parameters. The following table describes only the parameters for protocol conversion. For more information about how to create a route, see Create a route.
NoteThese parameters appear only when the backend service protocol is Dubbo.
Parameter
Description
Dubbo service name
The fully qualified name of the Dubbo service.
service version
The version of the Dubbo service. If the backend service does not have a version, this parameter defaults to 0.0.0.
service group
The group of the Dubbo service. You can leave this parameter empty if the backend service does not have a group.
method mapping
NoteA method mapping is a rule that maps an HTTP request to a Dubbo method. Click + Add method mapping to add multiple method mappings. A method mapping includes the following parameters.
Dubbo method name
The full name of the Dubbo method.
HTTP method
The HTTP method of the request.
method match path
To set a matching path, you must start with the path that is configured for Prefix matching. For example, if the prefix path is
/dubboDemo, the full matching path for a method must also start with/dubboDemo, such as/dubboDemo/hello.header pass-through type
Determines whether to pass the HTTP request headers to the backend Dubbo service as an implicit parameter (Attachment). Valid values:
Pass all headers
Do not pass headers
Pass specified headers
Separate multiple keys with commas (,), for example, content-length,content-type.
parameter mapping
Configure parameter mapping rules for the Dubbo method to extract its parameters from the HTTP request as
key-valuepairs. You can add multiple parameter mapping rules by clicking +Parameter Mapping.input parameter location: Specifies where in the HTTP request to extract the parameter.
Request parameter: Extracts the parameter from the query parameters of the request.
Request header: Extracts the parameter from the headers of the HTTP request.
Request path: Extracts the parameter from the path of the HTTP request.
Request body: Extracts the parameter from the body of the HTTP request.
extract key: The key that corresponds to the current parameter.
backend parameter type: The fully qualified type name of the current parameter. The following Java types are supported:
java.lang.String
java.lang.Long
java.lang.Double
java.lang.Boolean
java.util.List
java.util.Map
Custom type, for example, org.apache.dubbo.samples.basic.api.DubboTest
ImportantParameters of the
java.util.List,java.util.Map, and custom types can be extracted only from the request body.
Examples
The following examples show how to configure protocol conversion to access a Dubbo service by using HTTP requests. In the sample curl commands, replace http://xxx.xxx.xxx with the public endpoint of your gateway.
Dubbo service interface
The following code shows the interface of the backend Dubbo service.
package com.alibaba.nacos.example.dubbo.service;
import java.util.List;
import java.util.Map;
public interface DemoService {
String sayHello(String name);
String echoList(List<String> input);
String echoMap(Map<String, String> map);
String echoPerson(Person p);
}Person is a custom type defined as follows:
package com.alibaba.nacos.example.dubbo.service;
import java.io.Serializable;
public class Person implements Serializable {
public String name;
public String second_name;
public int age;
}Example 1: String from request parameters
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | sayHello |
HTTP method | GET |
method match path | /dubboDemo/hello |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request parameter |
extract key: param1 | |
backend parameter type: java.lang.String | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/hello?param1=abcd"Example 2: String from request headers
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | sayHello |
HTTP method | GET |
method match path | /dubboDemo/hello |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request header |
extract key: param1 | |
backend parameter type: java.lang.String | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/hello" -H "param1: abcd"Example 3: String from request path
If you set input parameter location to Request path, you must configure a corresponding template in method match path.
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | sayHello |
HTTP method | GET |
method match path | /dubboDemo/hello/{param1=*} |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request path |
extract key: param1 | |
backend parameter type: java.lang.String | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/hello/abcd"Example 4: String from request body
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | sayHello |
HTTP method | POST |
method match path | /dubboDemo/hello |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request body |
extract key: param1 | |
backend parameter type: java.lang.String | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/hello/" -X POST -d '{"param1": "abcd"}'Example 5: List from request body
Parameters of the java.util.List type can be extracted only from the request body.
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | echoList |
HTTP method | POST |
method match path | /dubboDemo/echolist |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request body |
extract key: param1 | |
backend parameter type: java.util.List | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/echolist/" -X POST -d '{"param1": ["abc", "def", "ghi"]}'Example 6: Map from request body
Parameters of the java.util.Map type can be extracted only from the request body.
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | echoMap |
HTTP method | POST |
method match path | /dubboDemo/echomap |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request body |
extract key: param1 | |
backend parameter type: java.util.Map | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/echomap/" -X POST -d '{"param1": {"key1": "value1", "key2": "value2", "key3": "value3"}}'Example 7: Custom type from request body
Parameters of a custom type can be extracted only from the request body.
Parameter | Description |
Dubbo service name | com.alibaba.nacos.example.dubbo.service.DemoService |
service version | 1.0.0 |
method mapping | |
Dubbo method name | echoPerson |
HTTP method | POST |
method match path | /dubboDemo/echoperson |
header pass-through type | Pass all headers |
parameter mapping | input parameter location: Request body |
extract key: param1 | |
backend parameter type: com.alibaba.nacos.example.dubbo.service.Person | |
Run the following curl command in a terminal to send the request and verify the result.
curl "http://xxx.xxx.xxx/dubboDemo/echoperson/" -X POST -d '{"param1": {"name": "Tom", "second_name": "John", "age": 21}}'