You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by gu...@apache.org on 2022/05/25 06:32:38 UTC
[dubbo-website] branch master updated: Add Tri pojo tutorial (#1093)
This is an automated email from the ASF dual-hosted git repository.
guohao 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 2580081a27 Add Tri pojo tutorial (#1093)
2580081a27 is described below
commit 2580081a270f71201bc0b64f90c9e15296097695
Author: GuoHao <gu...@gmail.com>
AuthorDate: Wed May 25 14:32:33 2022 +0800
Add Tri pojo tutorial (#1093)
* Fix style
* Add pojo tutorial
---
.../reference-manual/protocol/triple/idl.md | 7 +-
.../protocol/triple/{idl.md => pojo.md} | 128 +++++----------------
2 files changed, 30 insertions(+), 105 deletions(-)
diff --git a/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md b/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md
index 1126d884cd..c838976b86 100644
--- a/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md
+++ b/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md
@@ -152,7 +152,6 @@ weight: 2
package org.apache.dubbo;
import org.apache.dubbo.common.constants.CommonConstants;
- import org.apache.dubbo.common.context.Lifecycle;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
@@ -219,11 +218,11 @@ weight: 2
$ mvn clean install
```
10. 启动服务端
- ```
+ ```
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboServer"
Dubbo triple stub server started
- ```
-11. 启动一个新的终端,启动客户端
+ ```
+11. 打开新的终端,启动客户端
```
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboClient"
Received reply:message: "Hello,Demo!"
diff --git a/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md b/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/pojo.md
similarity index 51%
copy from content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md
copy to content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/pojo.md
index 1126d884cd..34b125bb9b 100644
--- a/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/idl.md
+++ b/content/zh/docs3-building/java-sdk/reference-manual/protocol/triple/pojo.md
@@ -1,11 +1,12 @@
---
type: docs
-title: "IDL 方式使用 Triple"
-linkTitle: "IDL 方式使用 Triple"
+title: "POJO 方式使用 Triple"
+linkTitle: "POJO 方式使用 Triple"
weight: 2
---
-这篇教程会通过从零构建一个简单的工程来演示如何基于 IDL 方式使用 Dubbo Triple
+这篇教程会通过从零构建一个简单的工程来演示如何基于 POJO 方式使用 Dubbo Triple, 在应用不改变已有接口定义的同时升级到 Triple 协议。
+POJO on Triple 原理请参考[POJO on Triple](TBD)
### 前置条件
- [JDK](https://jdk.java.net/) 版本 >= 8
@@ -17,7 +18,7 @@ weight: 2
```
$ mvn archetype:generate \
-DgroupId=org.apache.dubbo \
- -DartifactId=tri-stub-demo \
+ -DartifactId=tri-pojo-demo \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DarchetypeGroupId=org.apache.maven.archetypes \
@@ -25,7 +26,7 @@ weight: 2
```
2. 切换到工程目录
```
- $ cd tri-stub-demo
+ $ cd tri-pojo-demo
```
3. 在 `pom.xml` 中设置 JDK 版本,添加 Dubbo 依赖和插件
```xml
@@ -59,91 +60,23 @@ weight: 2
<version>3.19.4</version>
</dependency>
</dependencies>
-
- <build>
- <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.19.4:exe:${os.detected.classifier}</protocArtifact>
- <protocPlugins>
- <protocPlugin>
- <id>dubbo</id>
- <groupId>org.apache.dubbo</groupId>
- <artifactId>dubbo-compiler</artifactId>
- <version>0.0.4.1-SNAPSHOT</version>
- <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
- </protocPlugin>
- </protocPlugins>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>compile</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
```
-4. 添加接口定义文件`src/main/proto/hello.proto`,Dubbo 使用 [Protobuf](https://developers.google.com/protocol-buffers) 作为 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;
-
- message HelloRequest {
- string name = 1;
- }
-
- message HelloReply {
- string message = 1;
- }
- service Greeter{
- rpc greet(HelloRequest) returns (HelloReply);
- }
+4. 添加接口定义`src/main/java/org/apache/dubbo/Greeter.java`
+ ```java
+ package org.apache.dubbo;
- ```
-5. 编译 IDL
- ```
- $ mvn clean install
- ```
- 编译成功后,可以看到`target/generated-sources/protobuf/java` 目录下生成了代码文件
- ```
- $ ls org/apache/dubbo/hello/
- DubboGreeterTriple.java HelloReply.java HelloRequest.java HelloWorldProto.java
- Greeter.java HelloReplyOrBuilder.java HelloRequestOrBuilder.java
+ public interface Greeter {
+ String sayHello(String name);
+ }
```
-
-6. 添加服务端接口实现`src/main/java/org/apache/dubbo/GreeterImpl.java`
+5. 添加服务端接口实现`src/main/java/org/apache/dubbo/GreeterImpl.java`
```java
package org.apache.dubbo;
- import org.apache.dubbo.hello.DubboGreeterTriple;
- import org.apache.dubbo.hello.HelloReply;
- import org.apache.dubbo.hello.HelloRequest;
-
- public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
+ public class GreeterImpl implements Greeter {
@Override
- public HelloReply greet(HelloRequest request) {
- return HelloReply.newBuilder()
- .setMessage("Hello," + request.getName() + "!")
- .build();
+ public String sayHello(String name) {
+ return "Hello," + name + "!";
}
}
```
@@ -152,13 +85,11 @@ weight: 2
package org.apache.dubbo;
import org.apache.dubbo.common.constants.CommonConstants;
- import org.apache.dubbo.common.context.Lifecycle;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
- import org.apache.dubbo.hello.Greeter;
import java.io.IOException;
@@ -170,12 +101,12 @@ weight: 2
service.setRef(new GreeterImpl());
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
- bootstrap.application(new ApplicationConfig("tri-stub-server"))
+ bootstrap.application(new ApplicationConfig("tri-pojo-server"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
.service(service)
.start();
- System.out.println("Dubbo triple stub server started");
+ System.out.println("Dubbo triple pojo server started");
System.in.read();
}
}
@@ -190,9 +121,6 @@ weight: 2
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
- import org.apache.dubbo.hello.Greeter;
- import org.apache.dubbo.hello.HelloReply;
- import org.apache.dubbo.hello.HelloRequest;
public class MyDubboClient {
public static void main(String[] args) {
@@ -200,16 +128,14 @@ weight: 2
ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
ref.setInterface(Greeter.class);
ref.setProtocol(CommonConstants.TRIPLE);
- ref.setProxy(CommonConstants.NATIVE_STUB);
ref.setTimeout(3000);
- bootstrap.application(new ApplicationConfig("tri-stub-client"))
- .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
- .reference(ref)
- .start();
+ bootstrap.application(new ApplicationConfig("tri-pojo-client"))
+ .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
+ .reference(ref)
+ .start();
Greeter greeter = ref.get();
- HelloRequest request = HelloRequest.newBuilder().setName("Demo").build();
- HelloReply reply = greeter.greet(request);
+ String reply = greeter.sayHello("pojo");
System.out.println("Received reply:" + reply);
}
}
@@ -219,11 +145,11 @@ weight: 2
$ mvn clean install
```
10. 启动服务端
- ```
+ ```
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboServer"
- Dubbo triple stub server started
- ```
-11. 启动一个新的终端,启动客户端
+ Dubbo triple pojo server started
+ ```
+11. 打开新的终端,启动客户端
```
$ mvn org.codehaus.mojo:exec-maven-plugin:3.0.0:java -Dexec.mainClass="org.apache.dubbo.MyDubboClient"
Received reply:message: "Hello,Demo!"