rest
requests.When discussing the triple protocol example, we mentioned that the triple protocol supports direct access in application/json
format:
If you find the above http://localhost:50052/org.apache.dubbo.samples.api.GreetingsService/sayHi
format of path request not user-friendly, you can customize HTTP request paths and methods through annotations.
Currently, three annotation formats are supported: built-in, Spring Web, and JAX-RS. For the complete code of the following example, refer to dubbo-samples-triple-rest.
Of course, you can also import the project directly with an IDE and execute org.apache.dubbo.rest.demo.BasicRestApplication#main
to run it, and use breakpoints to debug and deepen your understanding of the principles.
Code Explanation:
You can see “Hello world” was outputted, and the double quotes indicate the default output content-type was application/json.
From this example, you can understand that Triple exports the service to
/{serviceInterface}/{methodName}
path by default and supports passing parameters through the URL.
Code Explanation:
The output “Hello Mr. Yang, 3” shows no double quotes because the output is requested as text/plain
by specifying the suffix txt.
This example illustrates how to customize paths via the Mapping annotation and customize parameter sources via the Param annotation, supporting parameters passed through the post body or URL; for detailed instructions, see: Basic User Guide
You can open debug logs to understand the startup and response request process of REST.
Once enabled, you can observe the Rest mapping registration and request-response process.
Next, let’s look at what real problems can be solved with triple protocol supporting REST format access.
First, the initial scenario is to enable interoperability between the Dubbo system and HTTP microservice systems.
Imagine you are in charge of a business line, and you have a microservice cluster developed based on Dubbo, where services communicate using the triple binary protocol. There is another important business within the company running on a microservice cluster developed based on Spring Cloud, where the services communicate using HTTP+JSON. Now you want to enable communication between these two businesses; how can services interact? The triple protocol supporting REST format access can solve this problem, allowing the Dubbo microservice cluster to communicate internally using the triple binary protocol while externally using the REST request format provided by triple.
Another very valuable scenario for supporting REST format access is that it facilitates gateway traffic access. Accessing binary-format RPC protocols has always been a challenge. Previously, Dubbo provided generic calls
to solve this problem; gateways could implement HTTP -> Dubbo
protocol conversion to access backend microservice clusters.
Now, with support for REST format, decentralized access can be achieved without any gateway protocol conversion. For more details, see HTTP Gateway Traffic Access.