Apache APISIX is a top-level open-source project of the Apache Software Foundation and the most active open-source gateway project currently. As a dynamic, real-time, high-performance open-source API gateway, Apache APISIX provides rich traffic management features such as load balancing, dynamic upstream, gray release, service circuit breaking, authentication, and observability.
Apache APISIX equips Apache Dubbo services with HTTP gateway capabilities based on the open-source project tengine/mod_dubbo module. With the dubbo-proxy plugin, you can easily publish Dubbo services as HTTP services.
This document uses Docker to install APISIX. Make sure to install Docker and Docker Compose locally first.
First, download the apisix-docker repository.
Since this example will connect to the Nacos registry, add the following content in the docker-compose.yaml
file located in the apisix-docker/example
directory:
Add Nacos registry configuration in the config.yaml
file:
Enable the dubbo-proxy plugin in the config.yaml
file:
If you use the Apache APISIX version 2.11 image, you can skip the
dubbo-proxy
configuration step since the Dubbo module is already compiled in the APISIX-Base version and can be used directly.
Finally, use docker-compose
to start APISIX: docker-compose -p docker-apisix up -d
In the following operations, we will demonstrate using the dubbo-samples-gateway-triple-apisix project.
Before diving into the main operations, let’s briefly look at the definition, configuration, and relevant implementation of the Dubbo interface.
As shown above, the definition of the Dubbo interface is fixed. The Map
in the method parameter represents some information regarding the HTTP request (such as header, body…) passed from APISIX to the Dubbo Provider. The return value Map
represents some information about how the Dubbo Provider conveys the HTTP response to APISIX.
After the above configuration, the Consumer can access the apisixDubbo
method through org.apache.dubbo.samples.gateway.apisix.dubbo.api.ApisixService
. The specific implementation of the interface is as follows:
In the above code, ApisixServiceImpl
will print the received httpRequestContext
, and return a Map object containing the specified Key to describe the HTTP response of the Dubbo request.
In the dubbo-samples-gateway-apisix-dubbo
directory, run the following command to start the application (or choose to start the application using an IDE):
Start the consumer process to verify that the service has started normally and can be called:
:::note Explanation
The above response contains the test: 123
Header, and the string dubbo success
as the Body. This is consistent with the expected effect we coded in DemoServiceImpl
.
:::
:::note Explanation
You can obtain the HTTP request’s Header and Body through httpRequestContext
. The Header will be a Map element, while the Key in the Body is a fixed string “body”, and the Value represents the Byte array.
:::
In the above simple use case, we indeed published the Dubbo Service as an HTTP service through Apache APISIX, but the limitations during use are also very obvious. For example, the parameters and return values of the interface must both be Map<String, Object>
.
So, what if there is an already defined interface in the project that does not conform to the above constraints? How can we expose an HTTP service through Apache APISIX?
For the above scenario, we can describe the Service and Method to be called and corresponding parameters in the HTTP Request Body, and then utilize Java’s reflection mechanism to invoke the target method. Finally, the return value can be serialized as JSON and written into the HTTP Response Body.
This way, we can further enhance Apache APISIX’s “HTTP to Dubbo” capability and apply it to all existing Dubbo Services. The specific operations can reference the following:
HTTP2DubboService
service (this step is omitted). Next, you can initiate a call to the backend Dubbo service in a manner similar to the following:This article introduced how to proxy Dubbo services using Apache APISIX, allowing for the construction of simpler and more efficient traffic links for the backend system of the Dubbo framework by introducing the dubbo-proxy
plugin.
We hope that through the above operation steps and use case sharing, you can gain insights for relevant scenarios. For more information about the dubbo-proxy
plugin, please refer to the official documentation.
For more examples related to this section, you can also refer to https://github.com/chickenlj/APISIX-Dubbo-Nacos