This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-website.git
The following commit(s) were added to refs/heads/master by this push: new 0370832 Add triple doc (#766) 0370832 is described below commit 037083240435fc11e99a4b7199a682ad6c42645e Author: GuoHao <guohao...@gmail.com> AuthorDate: Wed Apr 7 10:08:11 2021 +0800 Add triple doc (#766) Co-authored-by: 项升 <xiangsheng...@alibaba-inc.com> --- content/zh/docs/v3.0/references/tri.md | 145 +++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/content/zh/docs/v3.0/references/tri.md b/content/zh/docs/v3.0/references/tri.md new file mode 100644 index 0000000..f7a4093 --- /dev/null +++ b/content/zh/docs/v3.0/references/tri.md @@ -0,0 +1,145 @@ +--- +type: docs +title: "Triple 协议" +linkTitle: "Triple 协议" +weight: 12 +description: "Triple 协议使用" +--- + +Triple 协议是 Dubbo3 的主力协议,完整兼容 gRPC over HTTP/2,并在协议层面扩展了负载均衡和流量控制相关机制。本文档旨在指导用户正确的使用 Triple 协议。 + +在开始前,需要决定服务使用的序列化方式,如果为新服务,推荐使用 protobuf 作为默认序列化,在性能和跨语言上的效果都会更好。如果是原有服务想进行协议升级,Triple 协议也已经支持其他序列化方式,如 Hessian / JSON 等 + +### Protobuf + +1. 编写 IDL 文件 + ```protobuf + syntax = "proto3"; + + option java_multiple_files = true; + option java_package = "org.apache.dubbo.hello"; + option java_outer_classname = "HelloWorldProto"; + option objc_class_prefix = "HLW"; + + package helloworld; + + // The request message containing the user's name. + message HelloRequest { + string name = 1; + } + + // The response message containing the greetings + message HelloReply { + string message = 1; + } + ``` + +2. 添加编译 protobuf 的 extension 和 plugin (以 maven 为例) + ```xml + <extensions> + <extension> + <groupId>kr.motd.maven</groupId> + <artifactId>os-maven-plugin</artifactId> + <version>1.6.1</version> + </extension> + </extensions> + <plugins> + <plugin> + <groupId>org.xolstice.maven.plugins</groupId> + <artifactId>protobuf-maven-plugin</artifactId> + <version>0.6.1</version> + <configuration> + <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact> + <pluginId>triple-java</pluginId> + <outputDirectory>build/generated/source/proto/main/java</outputDirectory> + </configuration> + <executions> + <execution> + <goals> + <goal>compile</goal> + <goal>test-compile</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + ``` + +3. 构建/ 编译生成 protobuf Message 类 + ```shell + $ mvn clean install + ``` + +4. 编写 Java 接口 + ```java + import org.apache.dubbo.hello.HelloReply; + import org.apache.dubbo.hello.HelloRequest; + + public interface IGreeter { + /** + * <pre> + * Sends a greeting + * </pre> + */ + HelloReply sayHello(HelloRequest request); + + } + ``` + +5. 创建 Provider + ```java + public static void main(String[] args) throws InterruptedException { + ServiceConfig<IGreeter> service = new ServiceConfig<>(); + service.setInterface(IGreeter.class); + service.setRef(new IGreeter1Impl()); + // 这里需要显示声明使用的协议为triple + service.setProtocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051)); + service.setApplication(new ApplicationConfig("demo-provider")); + service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); + service.export(); + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + + ``` + + +6. 创建 Consumer + + ```java + public static void main(String[] args) throws IOException { + ReferenceConfig<IGreeter> ref = new ReferenceConfig<>(); + ref.setInterface(IGreeter.class); + ref.setCheck(false); + ref.setInterface(IGreeter.class); + ref.setCheck(false); + ref.setProtocol(CommonConstants.TRIPLE); + ref.setLazy(true); + ref.setTimeout(100000); + ref.setApplication(new ApplicationConfig("demo-consumer")); + ref.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); + final IGreeter iGreeter = ref.get(); + + System.out.println("dubbo ref started"); + try { + final HelloReply reply = iGreeter.sayHello(HelloRequest.newBuilder() + .setName("name") + .build()); + TimeUnit.SECONDS.sleep(1); + System.out.println("Reply:" + reply); + } catch (Throwable t) { + t.printStackTrace(); + } + System.in.read(); + } + ``` + +7. 运行 Provider 和 Consumer ,可以看到请求正常返回了 + > Reply:message: "name" + + +### 其他序列化方式 +省略上文中的 1-3 步,指定 Provider 和 Consumer 使用的协议即可完成协议升级。 + +### 示例程序 +本文的示例程序可以在 [triple-samples](https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-triple) 找到