In version 3.3, based on the existing HTTP protocol stack, triple implements comprehensive REST-style service export capabilities without the need for generic or gateway layer protocol conversion. Users can directly access backend Triple protocol services via HTTP protocol in a decentralized manner without configuration. It provides rich annotations and SPI extension support for advanced REST usage such as path customization, output format customization, and exception handling. Its main features include:
package org.apache.dubbo.rest.demo;
import org.apache.dubbo.remoting.http12.rest.Mapping;
import org.apache.dubbo.remoting.http12.rest.Param;
// Service Interface
public interface DemoService {
String hello(String name);
@Mapping(path = "/hi", method = HttpMethods.POST)
String hello(User user, @Param(value = "c", type = ParamType.Header) int count);
}
// Service Implementation
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String hello(String name) {
return "Hello " + name;
}
@Override
public String hello(User user, int count) {
return "Hello " + user.getTitle() + ". " + user.getName() + ", " + count;
}
}
// Model
@Data
public class User {
private String title;
private String name;
}
# Get the example code
git clone --depth=1 https://github.com/apache/dubbo-samples.git
cd dubbo-samples/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-basic
# Run
mvn spring-boot:run
curl -v "http://127.0.0.1:8081/org.apache.dubbo.rest.demo.DemoService/hello?name=world"
# Output
#> GET /org.apache.dubbo.rest.demo.DemoService/hello?name=world HTTP/1.1
#>
#< HTTP/1.1 200 OK
#< content-type: application/json
#< content-length: 13
#<
#"Hello world"
#
# Code Explanation
# You can see the output "Hello world", the double quotes are because the default output content-type is application/json.
# This example shows that Triple defaults to exporting services to /{serviceInterface}/{methodName} paths, supporting parameter passing via URL.
curl -v -H "c: 3" -d 'name=Yang' "http://127.0.0.1:8081/org.apache.dubbo.rest.demo.DemoService/hi.txt?title=Mr"
# Output
#> POST /org.apache.dubbo.rest.demo.DemoService/hi.txt?title=Mr HTTP/1.1
#> c: 3
#> Content-Length: 9
#> Content-Type: application/x-www-form-urlencoded
#>
#< HTTP/1.1 200 OK
#< content-type: text/plain
#< content-length: 17
#<
#Hello Mr. Yang, 3
#
# Code Explanation
# You can see the output Hello Mr. Yang, 3, without double quotes because the suffix txt requires a text/plain output.
# This example demonstrates how to customize paths with the Mapping annotation and the source of parameters with the Param annotation, which supports parameter passing through post body or URL.
Please refer to the user manual: Tripe Rest Manual
In version 3.3, reusable Spring Boot servlet listening ports can access HTTP traffic without Netty listening on new ports, simplifying deployment and reducing maintenance costs. By reducing reliance on external ports, it helps easily pass through corporate firewalls and gateways, simplifying network deployment and enhancing the maintainability and security of enterprise applications.
# Get example code
git clone --depth=1 https://github.com/apache/dubbo-samples.git
cd dubbo-samples/2-advanced/dubbo-samples-triple-servlet
# Run directly
mvn spring-boot:run
curl --http2-prior-knowledge -v 'http://localhost:50052/org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world'
# Output
#* [HTTP/2] [1] OPENED stream for http://localhost:50052/org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world
#* [HTTP/2] [1] [:method: GET]
#* [HTTP/2] [1] [:scheme: http]
#* [HTTP/2] [1] [:authority: localhost:50052]
#* [HTTP/2] [1] [:path: /org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world]
#>
#* Request completely sent off
#< HTTP/2 200
#< content-type: application/json
#< date: Sun, 25 Aug 2024 03:38:12 GMT
#<
#"Hello world"
Please visit: how-to-enable-servlet-support-for-triple to learn how to configure and enable servlet support.
In version 3.3, triple implements support for the HTTP/3 protocol, allowing both RPC requests and REST requests to be transmitted via the HTTP/3 protocol. Using HTTP/3 offers the following benefits:
Since HTTP/3 is based on the QUIC protocol (UDP), it may be blocked by firewalls or gateways. Therefore, triple implements HTTP/3 negotiation capabilities and enables them by default. Connections are first established via HTTP/2, and if successful and the server returns a header indicating support for HTTP/3, the client will automatically switch to HTTP/3.
# Get example code
git clone --depth=1 https://github.com/apache/dubbo-samples.git
cd dubbo-samples/2-advanced/dubbo-samples-triple-http3
# Run directly
mvn spring-boot:run
Note that curl needs to be updated to a new version that supports HTTP/3, see: https://curl.se/docs/http3.html
curl --http3 -vk 'https://localhost:50052/org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world'
# Output
#* QUIC cipher selection: TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_CCM_SHA256
#* Skipped certificate verification
#* using HTTP/3
#* [HTTP/3] [0] OPENED stream for https://localhost:50052/org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world
#* [HTTP/3] [0] [:method: GET]
#* [HTTP/3] [0] [:scheme: https]
#* [HTTP/3] [0] [:authority: localhost:50052]
#* [HTTP/3] [0] [:path: /org.apache.dubbo.demo.GreeterService/sayHelloAsync?request=world]
#>
#* Request completely sent off
#< HTTP/3 200
#< content-type: application/json
#<
#"Hello world"
Please visit: how-to-enable-http3-support-for-triple to learn how to configure and enable HTTP/3 support.