For a description of how to proxy triple protocol services with a gateway, please refer to the section on HTTP Gateway Access.
This article demonstrates how to use the combination of Apache APISIX + triple protocol + Nacos registry
to proxy Dubbo services.
The complete source code and deployment resource files for this example can be found at dubbo-samples-gateway-triple-apisix, with the architecture diagram as follows:
In this example, a triple service org.apache.dubbo.samples.gateway.apisix.DemoService
has been defined and published, with the interface defined as:
public interface DemoService {
String sayHello(String name);
}
The interface implementation is as follows:
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
Dubbo service-related configuration:
dubbo:
application:
name: gateway-apisix-triple
registry:
address: nacos://${nacos.address:127.0.0.1}:8848
username: nacos
password: nacos
protocol:
name: tri
port: 50052
Download and start Nacos locally.
Run the following command to start the Dubbo application.
Download the source code:
$ git clone -b main --depth 1 https://github.com/apache/dubbo-samples
$ cd dubbo-samples/2-advanced/dubbo-samples-gateway/dubbo-samples-gateway-apisix/dubbo-samples-gateway-apisix-triple
In the dubbo-samples-gateway-apisix-triple
directory, run the following command to start the application:
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.gateway.apisix.ProviderApplication"
Run the following command to test if the service has started normally:
curl \
--header "Content-Type: application/json" \
--data '["dubbo"]' \
http://localhost:50052/org.apache.dubbo.samples.gateway.apisix.DemoService/sayHello/
This document uses Docker to install APISIX. Ensure that Docker and Docker Compose are installed locally.
First, download the apisix-docker repository.
$ git clone https://github.com/apache/apisix-docker.git
$ cd apisix-docker/example
Since this example needs to connect to the Nacos registry, the docker-compose.yaml
in the apisix-docker/example
directory needs to be modified to add the following Docker Compose configuration:
nacos:
image: nacos/nacos-server:v2.1.1
container_name: nacos-standalone
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
ports:
- "8848:8848"
- "9848:9848"
networks:
apisix:
Before starting APISIX, add the following configuration to the conf/config.yaml
file to connect APISIX to the Nacos registry:
discovery:
nacos:
host:
- "http://192.168.33.1:8848"
Finally, enable APISIX using docker-compose
: docker-compose -p docker-apisix up -d
.
Configure Nacos upstream and routes in APISIX to achieve automatic discovery of backend instance addresses (assuming the APISIX port is 9080):
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/org.apache.dubbo.samples.gateway.apisix.DemoService/sayHello/",
"upstream": {
"service_name": "gateway-apisix-triple",
"type": "roundrobin",
"discovery_type": "nacos"
}
}'
In the above command, the request header X-API-KEY is the access token for the Admin API, which can be found in the apisix.admin_key.key in the conf/config.yaml file.
Use the following command to send a request to the route that needs to be configured:
curl -i http://127.0.0.1:9080/org.apache.dubbo.samples.gateway.apisix.DemoService/sayHello/
If you find the HTTP port /org.apache.dubbo.samples.gateway.apisix.DemoService/sayHello/
not friendly enough for gateway access, refer to Publishing REST Style HTTP Interfaces for Triple Protocol.