dubbo-proxy

Description

The dubbo-proxy Plugin allows you to proxy HTTP requests to Apache Dubbo.

dubbo-proxy - 图1IMPORTANT

If you are using OpenResty, you need to build it with Dubbo support. See How do I build the APISIX runtime environment for details.

Runtime Attributes

NameTypeRequiredDefaultDescription
service_namestringTrueDubbo provider service name.
service_versionstringTrueDubbo provider service version.
methodstringFalseThe path of the URI.Dubbo provider service method.

Static Attributes

NameTypeRequiredDefaultValid valuesDescription
upstream_multiplex_countnumberTrue32>= 1Maximum number of multiplex requests in an upstream connection.

Enable Plugin

To enable the dubbo-proxy Plugin, you have to add it in your configuration file (conf/config.yaml):

conf/config.yaml

  1. plugins:
  2. - ...
  3. - dubbo-proxy

Now, when APISIX is reloaded, you can add it to a specific Route as shown below:

dubbo-proxy - 图2note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/upstreams/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "nodes": {
  4. "127.0.0.1:20880": 1
  5. },
  6. "type": "roundrobin"
  7. }'
  8. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  9. {
  10. "uris": [
  11. "/hello"
  12. ],
  13. "plugins": {
  14. "dubbo-proxy": {
  15. "service_name": "org.apache.dubbo.sample.tengine.DemoService",
  16. "service_version": "0.0.0",
  17. "method": "tengineDubbo"
  18. }
  19. },
  20. "upstream_id": 1
  21. }'

Example usage

You can follow the Quick Start guide in Tengine with the configuration above for testing.

APISIX dubbo plugin uses hessian2 as the serialization protocol. It supports only Map<String, Object> as the request and response data type.

Application

Your dubbo config should be configured to use hessian2 as the serialization protocol.

  1. dubbo:
  2. ...
  3. protocol:
  4. ...
  5. serialization: hessian2

Your application should implement the interface with the request and response data type as Map<String, Object>.

  1. public interface DemoService {
  2. Map<String, Object> sayHello(Map<String, Object> context);
  3. }

Request and Response

If you need to pass request data, you can add the data to the HTTP request header. The plugin will convert the HTTP request header to the request data of the Dubbo service. Here is a sample HTTP request that passes user information:

  1. curl -i -X POST 'http://localhost:9080/hello' \
  2. --header 'user: apisix'
  3. HTTP/1.1 200 OK
  4. Date: Mon, 15 Jan 2024 10:15:57 GMT
  5. Content-Type: text/plain; charset=utf-8
  6. ...
  7. hello: apisix
  8. ...
  9. Server: APISIX/3.8.0

If the returned data is:

  1. {
  2. "status": "200",
  3. "header1": "value1",
  4. "header2": "value2",
  5. "body": "body of the message"
  6. }

The converted HTTP response will be:

  1. HTTP/1.1 200 OK
  2. ...
  3. header1: value1
  4. header2: value2
  5. ...
  6. body of the message

Delete Plugin

To remove the dubbo-proxy Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uris": [
  5. "/hello"
  6. ],
  7. "plugins": {
  8. },
  9. "upstream_id": 1
  10. }
  11. }'

To completely disable the dubbo-proxy Plugin, you can remove it from your configuration file (conf/config.yaml):

conf/config.yaml

  1. plugins:
  2. # - dubbo-proxy