Skip to content

Commit

Permalink
update triple samples
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Jul 10, 2023
1 parent 35a0613 commit 1c3eea3
Show file tree
Hide file tree
Showing 113 changed files with 931 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
package org.apache.dubbo.samples.api;

public interface GreetingsService {

String sayHi(String name);
}
38 changes: 34 additions & 4 deletions 1-basic/dubbo-samples-triple-unary/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
# Dubbo Spring Boot Example

This example shows the basic usage of Triple protocol, it is a typical request-response model demo and uses IDL to define the Dubbo service.
This example shows the basic usage of Triple protocol with a typical request-response model demo that uses IDL as the method of defining Dubbo service.

As described in Triple documentation, unary Triple services can be accessed by cURL and web browsers directly.
As described in [the Triple protocol documentation](https://dubbo.apache.org/zh-cn/overview/reference/protocols/triple/), Dubbo triple protocol is a better gRPC implementation which can be accessed by cURL and web browsers directly.

More usages of Triple protocol can be found here:
* [Streaming RPCs](../../2-advanced/dubbo-samples-triple-streaming/)
* [Interoperability with standard gRPC clients and servers](../../2-advanced/dubbo-samples-triple-grpc/)
* [Triple without using IDL](../../2-advanced/dubbo-samples-triple-no-idl/)
* [Visiting Triple service with standard HTTP tools]()
* [Triple without Protobuf (no IDL mode)](../../2-advanced/dubbo-samples-triple-no-idl/)
* [Using triple with other languages and browser](https://dubbo.apache.org/zh-cn/overview/quickstart/rpc/)

## Run The Demo
Detailed explanation of this demo can be found [here](https://dubbo.apache.org/zh-cn/overview/quickstart/rpc/java/).

> Make sure you are in `dubbo-samples-triple-unary` before running the following commands.
### Start server
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.unary.TriUnaryServer"
```

### Start Client

There are two ways to test the server works as expected:
* Standard HTTP tools like cURL.
* Dubbo sdk client.

#### cURL
```shell
curl \
--header "Content-Type: application/json" \
--data '[{"name": "Dubbo"}]' \
http://localhost:50052/org.apache.dubbo.samples.tri.unary.Greeter/greet/
```

#### Start client
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.unary.TriUnaryClient"
```

24 changes: 21 additions & 3 deletions 1-basic/dubbo-samples-triple-unary/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.19.6</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
Expand Down Expand Up @@ -98,6 +98,24 @@
<target>${target.level}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/protobuf/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
38 changes: 38 additions & 0 deletions 2-advanced/dubbo-samples-triple-grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Dubbo Spring Boot Example

This example shows how you can use triple protocol to do interoperate with standard gRPC.

![triple-grpc.png](./docs/triple-grpc.png)

As described in [the Triple protocol documentation](https://dubbo.apache.org/zh-cn/overview/reference/protocols/triple/), Dubbo triple protocol is a better gRPC implementation which can be accessed by cURL and web browsers directly.

## Serve As Standard gRPC Server
This part showcases how standard gRPC client written with gRPC-java consumes Triple protocol service written with Dubbo.

### Start a Dubbo server
Make sure you are in `dubbo-samples-triple-grpc` directory and then run the following command:

```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.grpc.interop.server.TriOpServer"
```

### Call Triple server with standard gRPC client
Open a new terminal, enter `dubbo-samples-triple-grpc` directory and then run the following command:

```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.grpc.interop.server.GrpcClient"
```

## Consume Standard gRPC Server
This part showcases how Triple client written with Dubbo consumes gRPC service written with standard gRPC-java.

### Start a standard gRPC server
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.grpc.interop.client.GrpcServer"
```

### Call gRPC server with Dubbo client
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.grpc.interop.client.TriOpClient"
```

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 54 additions & 2 deletions 2-advanced/dubbo-samples-triple-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-samples-triple-streaming</artifactId>
<artifactId>dubbo-samples-triple-grpc</artifactId>

<properties>
<dubbo.version>3.2.0</dubbo.version>
Expand All @@ -42,10 +42,21 @@
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
<exclusions>
<exclusion>
<artifactId>grpc-netty</artifactId>
<groupId>io.grpc</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
Expand All @@ -69,7 +80,11 @@
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
<protocPlugins>
<protocPlugin>
<id>dubbo</id>
Expand All @@ -84,6 +99,9 @@
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
<goal>compile-custom</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
Expand All @@ -97,6 +115,40 @@
<target>${target.level}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/protobuf/java</source>
<source>target/generated-sources/protobuf/grpc-java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,34 @@
* limitations under the License.
*/

package org.apache.dubbo.samples.tri.streaming;
package org.apache.dubbo.samples.tri.grpc.interop.client;

import org.apache.dubbo.samples.tri.grpc.GreeterReply;
import org.apache.dubbo.samples.tri.grpc.GreeterRequest;
import org.apache.dubbo.samples.tri.grpc.GreeterGrpc;

import org.apache.dubbo.common.stream.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
private static final Logger LOGGER = LoggerFactory.getLogger(GreeterImpl.class);
private final String serverName;
public class GrpcGreeterImpl extends GreeterGrpc.GreeterImplBase {
private static final Logger LOGGER = LoggerFactory.getLogger(GrpcGreeterImpl.class);

public GreeterImpl(String serverName) {
this.serverName = serverName;
@Override
public void greet(GreeterRequest request, io.grpc.stub.StreamObserver<GreeterReply> responseObserver) {
try {
final GreeterReply response = GreeterReply.newBuilder()
.setMessage("hello," + request.getName())
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
} catch (Throwable t) {
responseObserver.onError(t);
}
}

public StreamObserver<GreeterRequest> biStream(StreamObserver<GreeterReply> responseObserver) {
return new StreamObserver<GreeterRequest>() {
@Override
public io.grpc.stub.StreamObserver<GreeterRequest> biStream(io.grpc.stub.StreamObserver<GreeterReply> responseObserver) {
return new io.grpc.stub.StreamObserver<GreeterRequest>() {
@Override
public void onNext(GreeterRequest data) {
GreeterReply resp = GreeterReply.newBuilder().setMessage("reply from biStream " + data.getName()).build();
Expand All @@ -50,12 +62,12 @@ public void onCompleted() {
}

@Override
public void serverStream(GreeterRequest request, StreamObserver<GreeterReply> responseObserver) {
public void serverStream(GreeterRequest request, io.grpc.stub.StreamObserver<GreeterReply> responseObserver) {
LOGGER.info("receive request: {}", request.getName());
for (int i = 0; i < 10; i++) {
GreeterReply reply = GreeterReply.newBuilder().setMessage("reply from serverStream. " + i).build();
responseObserver.onNext(reply);
}
responseObserver.onCompleted();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.
*/

package org.apache.dubbo.samples.tri.grpc.interop.client;

import org.apache.dubbo.common.context.Lifecycle;

import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Objects;

public class GrpcServer implements Lifecycle {

private static final Logger LOGGER = LoggerFactory.getLogger(GrpcServer.class);

private final int port;
private Server server;

public GrpcServer(int port) {
this.port = port;
}

public static void main(String[] args) throws IOException, InterruptedException {
GrpcServer server = new GrpcServer(50051);
server.initialize();
server.start();
System.in.read();
}

@Override
public void initialize() throws IllegalStateException {
this.server = ServerBuilder.forPort(port)
.addService(new GrpcGreeterImpl())
.intercept(new EchoAttachmentInterceptor())
.build();
}

@Override
public void start() throws IllegalStateException {
try {
server.start();
LOGGER.info("Grpc server started at port {}", port);
} catch (IOException e) {
throw new IllegalStateException("Start grpc server failed ", e);
}
}

@Override
public void destroy() throws IllegalStateException {
server.shutdown();
}

private static class EchoAttachmentInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> serverCall,
Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> forwardingCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(serverCall) {
@Override
public void close(Status status, Metadata trailers) {
final String key = "user-attachment";
final Metadata.Key<String> metaKey = Metadata.Key.of(key,
Metadata.ASCII_STRING_MARSHALLER);
if (metadata.containsKey(metaKey)) {
trailers.put(metaKey, "hello," + Objects.requireNonNull(
metadata.get(metaKey)));
}
super.close(status, trailers);
}
};
return serverCallHandler.startCall(forwardingCall, metadata);
}
}
}
Loading

0 comments on commit 1c3eea3

Please sign in to comment.