This is an automated email from the ASF dual-hosted git repository.
xuetaoli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go-samples.git
The following commit(s) were added to refs/heads/main by this push:
new 0da494c0 Update helloworld example (#989)
0da494c0 is described below
commit 0da494c0c7dba77ed2138f6a61c706358a4ecf71
Author: 陈乐樂 <[email protected]>
AuthorDate: Mon Dec 8 09:54:54 2025 +0800
Update helloworld example (#989)
---
helloworld/README.md | 2 +-
helloworld/README_CN.md | 2 +-
helloworld/java-client/pom.xml | 35 ++++++++------
.../java/org/example/client/JavaClientApp.java | 26 ++++++-----
helloworld/java-client/src/main/proto/greet.proto | 36 ---------------
helloworld/java-server/pom.xml | 35 ++++++++------
.../java/org/example/server/JavaServerApp.java | 54 ++++++++++++----------
helloworld/java-server/src/main/proto/greet.proto | 37 ---------------
helloworld/pom.xml | 31 ++++++++-----
helloworld/proto/greet.proto | 6 ++-
10 files changed, 111 insertions(+), 153 deletions(-)
diff --git a/helloworld/README.md b/helloworld/README.md
index d96f08e0..0cb67700 100644
--- a/helloworld/README.md
+++ b/helloworld/README.md
@@ -10,7 +10,7 @@ This example demonstrates the basic usage of dubbo-go as an
RPC framework, and s
- go-client/cmd/main.go - is the rpc client
- java-server/src/main/java/org/example/server/JavaServerApp.java - is the
Java server
- java-client/src/main/java/org/example/client/JavaClientApp.java - is the
Java client
-- proto - contains the protobuf definition of the API
+- proto - shared protobuf definition used by both Go and Java modules
## How to run
diff --git a/helloworld/README_CN.md b/helloworld/README_CN.md
index a234f918..829a9e4b 100644
--- a/helloworld/README_CN.md
+++ b/helloworld/README_CN.md
@@ -10,7 +10,7 @@
- go-client/cmd/main.go - RPC 客户端
- java-server/src/main/java/org/example/server/JavaServerApp.java - Java服务端
- java-client/src/main/java/org/example/client/JavaClientApp.java - Java客户端
-- proto - API 的 protobuf 定义
+- proto - Go 与 Java 模块共用的 API protobuf 定义
## 运行方法
diff --git a/helloworld/java-client/pom.xml b/helloworld/java-client/pom.xml
index 9e778431..6a3f8c3b 100644
--- a/helloworld/java-client/pom.xml
+++ b/helloworld/java-client/pom.xml
@@ -37,19 +37,9 @@
<version>${protobuf.version}</version>
</dependency>
<dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-stub</artifactId>
- <version>${grpc.version}</version>
- </dependency>
- <dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-protobuf</artifactId>
- <version>${grpc.version}</version>
- </dependency>
- <dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-netty-shaded</artifactId>
- <version>${grpc.version}</version>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo</artifactId>
+ <version>${dubbo.version}</version>
</dependency>
</dependencies>
@@ -59,6 +49,23 @@
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+
<source>${project.build.directory}/generated-sources/protobuf/java</source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@@ -69,4 +76,4 @@
</plugin>
</plugins>
</build>
-</project>
\ No newline at end of file
+</project>
diff --git
a/helloworld/java-client/src/main/java/org/example/client/JavaClientApp.java
b/helloworld/java-client/src/main/java/org/example/client/JavaClientApp.java
index 7eb158b3..bfede874 100644
--- a/helloworld/java-client/src/main/java/org/example/client/JavaClientApp.java
+++ b/helloworld/java-client/src/main/java/org/example/client/JavaClientApp.java
@@ -17,24 +17,28 @@
package org.example.client;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+
import greet.GreetRequest;
import greet.GreetResponse;
-import greet.GreetServiceGrpc;
-
-import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
+import greet.GreetService;
public class JavaClientApp {
public static void main(String[] args) {
- ManagedChannel channel = ManagedChannelBuilder
- .forAddress("127.0.0.1", 20000)
- .usePlaintext()
- .build();
+ ReferenceConfig<GreetService> reference = new ReferenceConfig<>();
+ reference.setInterface(GreetService.class);
+ reference.setUrl("tri://127.0.0.1:20000");
- GreetServiceGrpc.GreetServiceBlockingStub client =
- GreetServiceGrpc.newBlockingStub(channel);
+ DubboBootstrap bootstrap = DubboBootstrap.getInstance();
+ bootstrap.application(new ApplicationConfig("java-greet-client"))
+ .reference(reference)
+ .start();
+
+ GreetService client = reference.get();
GreetRequest req = GreetRequest.newBuilder()
.setName("Java Client")
@@ -43,7 +47,5 @@ public class JavaClientApp {
GreetResponse resp = client.greet(req);
System.out.println("Response: " + resp.getGreeting());
-
- channel.shutdown();
}
}
diff --git a/helloworld/java-client/src/main/proto/greet.proto
b/helloworld/java-client/src/main/proto/greet.proto
deleted file mode 100644
index 28982b54..00000000
--- a/helloworld/java-client/src/main/proto/greet.proto
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-syntax = "proto3";
-
-package greet;
-
-option go_package = "./greet";
-option java_package = "greet";
-option java_multiple_files = true;
-
-message GreetRequest {
- string name = 1;
-}
-
-message GreetResponse {
- string greeting = 1;
-}
-
-service GreetService {
- rpc Greet(GreetRequest) returns (GreetResponse);
-}
diff --git a/helloworld/java-server/pom.xml b/helloworld/java-server/pom.xml
index 2d8853ce..13aa0ff3 100644
--- a/helloworld/java-server/pom.xml
+++ b/helloworld/java-server/pom.xml
@@ -37,19 +37,9 @@
<version>${protobuf.version}</version>
</dependency>
<dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-stub</artifactId>
- <version>${grpc.version}</version>
- </dependency>
- <dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-protobuf</artifactId>
- <version>${grpc.version}</version>
- </dependency>
- <dependency>
- <groupId>io.grpc</groupId>
- <artifactId>grpc-netty-shaded</artifactId>
- <version>${grpc.version}</version>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo</artifactId>
+ <version>${dubbo.version}</version>
</dependency>
</dependencies>
@@ -59,6 +49,23 @@
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+
<source>${project.build.directory}/generated-sources/protobuf/java</source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@@ -69,4 +76,4 @@
</plugin>
</plugins>
</build>
-</project>
\ No newline at end of file
+</project>
diff --git
a/helloworld/java-server/src/main/java/org/example/server/JavaServerApp.java
b/helloworld/java-server/src/main/java/org/example/server/JavaServerApp.java
index b47e2cc4..5cdc3fdc 100644
--- a/helloworld/java-server/src/main/java/org/example/server/JavaServerApp.java
+++ b/helloworld/java-server/src/main/java/org/example/server/JavaServerApp.java
@@ -17,39 +17,45 @@
package org.example.server;
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+
+import greet.DubboGreetServiceTriple;
import greet.GreetRequest;
import greet.GreetResponse;
-import greet.GreetServiceGrpc;
-
-import io.grpc.Server;
-import io.grpc.ServerBuilder;
-import io.grpc.stub.StreamObserver;
+import greet.GreetService;
public class JavaServerApp {
public static void main(String[] args) throws Exception {
- Server server = ServerBuilder.forPort(20000)
- .addService(new GreetServiceGrpc.GreetServiceImplBase() {
-
- @Override
- public void greet(GreetRequest request,
- StreamObserver<GreetResponse>
respObserver) {
+ ServiceConfig<GreetService> serviceConfig = new ServiceConfig<>();
+ serviceConfig.setInterface(GreetService.class);
+ serviceConfig.setRef(new GreetServiceImpl());
- String name = request.getName();
+ DubboBootstrap bootstrap = DubboBootstrap.getInstance();
+ bootstrap.application(new ApplicationConfig("java-greet-server"))
+ .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 20000))
+ .service(serviceConfig)
+ .start();
- GreetResponse resp = GreetResponse.newBuilder()
- .setGreeting("Hello from Java Server, " + name)
- .build();
-
- respObserver.onNext(resp);
- respObserver.onCompleted();
- }
- })
- .build();
+ System.out.println("Dubbo Triple Java server started on port 20000");
+ System.in.read();
+ }
- server.start();
- System.out.println("Java Triple Server started on port 20000");
- server.awaitTermination();
+ /**
+ * Dubbo triple service implementation generated from proto.
+ */
+ static class GreetServiceImpl extends
DubboGreetServiceTriple.GreetServiceImplBase {
+
+ @Override
+ public GreetResponse greet(GreetRequest request) {
+ return GreetResponse.newBuilder()
+ .setGreeting("Hello from Dubbo Triple Java Server, " +
request.getName())
+ .build();
+ }
}
}
diff --git a/helloworld/java-server/src/main/proto/greet.proto
b/helloworld/java-server/src/main/proto/greet.proto
deleted file mode 100644
index 28b87265..00000000
--- a/helloworld/java-server/src/main/proto/greet.proto
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-syntax = "proto3";
-
-package greet;
-
-option go_package =
"github.com/apache/dubbo-go-samples/helloworld/proto;greet";
-option java_package = "greet";
-option java_multiple_files = true;
-
-message GreetRequest {
- string name = 1;
-}
-
-message GreetResponse {
- string greeting = 1;
-}
-
-// gRPC/Dubbo-Triple
-service GreetService {
- rpc Greet(GreetRequest) returns (GreetResponse);
-}
diff --git a/helloworld/pom.xml b/helloworld/pom.xml
index c93e3c5b..28801627 100644
--- a/helloworld/pom.xml
+++ b/helloworld/pom.xml
@@ -35,6 +35,7 @@
<properties>
<protobuf.version>3.21.7</protobuf.version>
<grpc.version>1.51.0</grpc.version>
+ <dubbo.version>3.3.0-beta.2</dubbo.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -48,7 +49,6 @@
</dependency>
</dependencies>
-
<build>
<extensions>
<extension>
@@ -65,26 +65,33 @@
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
- <!-- Windows平台protoc配置(可根据实际环境调整) -->
- <protocArtifact>
-
com.google.protobuf:protoc:${protobuf.version}:exe:windows-x86_64
- </protocArtifact>
- <pluginId>grpc-java</pluginId>
- <pluginArtifact>
-
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:windows-x86_64
- </pluginArtifact>
-
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
+
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
+
<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
+ <protocPlugins>
+ <protocPlugin>
+ <id>dubbo</id>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-compiler</artifactId>
+ <version>${dubbo.version}</version>
+
<mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
+ </protocPlugin>
+ </protocPlugins>
+
<protoSourceRoot>${project.parent.basedir}/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
- <goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>3.3.0</version>
+ </plugin>
</plugins>
</pluginManagement>
</build>
-</project>
\ No newline at end of file
+</project>
diff --git a/helloworld/proto/greet.proto b/helloworld/proto/greet.proto
index 109ddad3..dbf263b9 100644
--- a/helloworld/proto/greet.proto
+++ b/helloworld/proto/greet.proto
@@ -20,6 +20,8 @@ syntax = "proto3";
package greet;
option go_package =
"github.com/apache/dubbo-go-samples/helloworld/proto;greet";
+option java_package = "greet";
+option java_multiple_files = true;
message GreetRequest {
string name = 1;
@@ -30,5 +32,5 @@ message GreetResponse {
}
service GreetService {
- rpc Greet(GreetRequest) returns (GreetResponse) {}
-}
\ No newline at end of file
+ rpc Greet(GreetRequest) returns (GreetResponse);
+}