forked from apache/dubbo-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
113 changed files
with
931 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,5 @@ | |
package org.apache.dubbo.samples.api; | ||
|
||
public interface GreetingsService { | ||
|
||
String sayHi(String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...riple-grpc/src/main/java/org/apache/dubbo/samples/tri/grpc/interop/client/GrpcServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.