Skip to content
Permalink
Browse files
Merge pull request #356 from guohao/add-tri-samples
Add tri samples
  • Loading branch information
guohao committed Sep 15, 2021
2 parents 353bb79 + d2c89f5 commit b4c7fd7ef813515f59181ef16584319efc6fb738
Show file tree
Hide file tree
Showing 58 changed files with 3,193 additions and 130 deletions.
@@ -0,0 +1,37 @@
## Triple-Samples

### For beginners

1. Start a zookeeper or config other registry in `org.apache.dubbo.sample.tri.ApiProvider`
and `org.apache.dubbo.sample.tri.ApiConsumer`
2. Build with `mvn clean install` to generate protobuf classes.
3. Run `org.apache.dubbo.sample.tri.ApiProvider`
4. Run `org.apache.dubbo.sample.tri.ApiConsumer`

### For dubbo2 or non-prototbuf users
Run `org.apache.dubbo.sample.tri.ApiWrapperProvider` and `org.apache.dubbo.sample.tri.ApiWrapperConsumer`.

Dubbo3 will wrap request and response with a protobuf wrapper automatically, so it is still compatible with GRPC.

### For Dubbo developers
Code under `test` folder is a general interoperation test suite for both Dubbo/Dubbo and Dubbo/GRPC.

#### Dubbo/Dubbo test
1. Run `org.apache.dubbo.sample.tri.TriGrpcProvider`
2. Run `org.apache.dubbo.sample.tri.TriPbConsumerTest` , `org.apache.dubbo.sample.tri.TriWrapConsumerTest` and `org.apache.dubbo.sample.tri.TriGenericTest`

#### DUbbo/GRPC test

##### GRPC --> Dubbo

1. Run `org.apache.dubbo.sample.tri.TriGrpcProvider`
2. Run `org.apache.dubbo.sample.tri.grpc.GrpcConsumerTest`

##### Dubbo --> GRPC

1. Run `org.apache.dubbo.sample.tri.grpc.GrpcProvider`
2. Run `org.apache.dubbo.sample.tri.TriPbConsumerTest`




@@ -0,0 +1,50 @@
# 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.

services:
dubbo-samples-triple:
type: app
basedir: .
mainClass: org.apache.dubbo.sample.tri.TriGrpcProvider

dubbo-samples-triple-test:
type: test
basedir: .
tests:
# tri-->tri
- "org.apache.dubbo.sample.tri/TriPbConsumerTest.class"
- "org.apache.dubbo.sample.tri/TriWrapConsumerTest.class"
- "org.apache.dubbo.sample.tri/TriGenericTest.class"
# grpc-->tri
- "org.apache.dubbo.sample.tri/grpc/Grpc*Test.class"
# tri-->grpc
# fixme Failure is always present (https://github.com/apache/dubbo/issues/8738)
#- "org.apache.dubbo.sample.tri.grpc/TriGrpcDirectPbConsumerTest.class"
systemProps:
- zookeeper.host=dubbo-samples-triple
- zookeeper.port=2181
- provider.host=dubbo-samples-triple
- provider.port=50052
- grpc.provider.port=50053
waitPortsBeforeRun:
- dubbo-samples-triple:2181
- dubbo-samples-triple:50052
depends_on:
- dubbo-samples-triple




@@ -0,0 +1,23 @@
#
#
# 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.
#

# Supported component versions of the test case

# Spring app
dubbo.version=3.*
spring.version=4.*, 5.*
@@ -32,13 +32,14 @@
<source.level>1.8</source.level>
<target.level>1.8</target.level>
<dubbo.version>3.0.2.1</dubbo.version>
<grpc.version>1.40.1</grpc.version>
<junit.version>4.12</junit.version>
<spring-test.version>4.3.16.RELEASE</spring-test.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
<spring-boot.version>1.5.13.RELEASE</spring-boot.version>
<grpc.version>1.19.0</grpc.version>
<protoc.version>3.7.1</protoc.version>
<dubbo.compiler.version>0.0.4-SNAPSHOT</dubbo.compiler.version>
</properties>

<dependencies>
@@ -58,6 +59,12 @@
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
<version>${grpc.version}</version>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -110,15 +117,26 @@
<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>
<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>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-compiler</artifactId>
<version>${dubbo.compiler.version}</version>
<mainClass>org.apache.dubbo.gen.dubbo.Dubbo3Generator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
<goal>compile-custom</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
@@ -132,23 +150,6 @@
<target>${target.level}</target>
</configuration>
</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>build/generated/source/proto/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

This file was deleted.

This file was deleted.

@@ -15,21 +15,25 @@
* limitations under the License.
*/

package com.apache.dubbo.sample.basic;
package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.hello.HelloReply;
import org.apache.dubbo.hello.HelloRequest;
import org.apache.dubbo.sample.tri.helper.StdoutStreamObserver;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class ApiConsumer {
public static void main(String[] args) throws IOException {
class ApiConsumer {
private final IGreeter delegate;

ApiConsumer() {
ReferenceConfig<IGreeter> ref = new ReferenceConfig<>();
ref.setInterface(IGreeter.class);
ref.setCheck(false);
@@ -41,22 +45,48 @@ public static void main(String[] args) throws IOException {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.reference(ref)
.start();

final IGreeter iGreeter = ref.get();
this.delegate = ref.get();
}

public static void main(String[] args) throws IOException {
final ApiConsumer consumer = new ApiConsumer();
System.out.println("dubbo triple consumer started");
consumer.unaryHello();
consumer.stream();
consumer.serverStream();
System.in.read();
}

System.out.println("dubbo ref started");
public void serverStream() {
delegate.sayHelloServerStream(HelloRequest.newBuilder()
.setName("request")
.build(), new StdoutStreamObserver<>("serverStream"));
}

public void stream() {
final StreamObserver<HelloRequest> request = delegate.sayHelloStream(new StdoutStreamObserver<>("stream"));
for (int i = 0; i < 10; i++) {
request.onNext(HelloRequest.newBuilder()
.setName("request")
.build());
}
request.onCompleted();
}

public void unaryHello() {
try {
final HelloReply reply = iGreeter.sayHello(HelloRequest.newBuilder()
final HelloReply reply = delegate.sayHello(HelloRequest.newBuilder()
.setName("name")
.build());
TimeUnit.SECONDS.sleep(1);
System.out.println("Reply:" + reply);
} catch (Throwable t) {
t.printStackTrace();
}
System.in.read();
}

}
@@ -15,7 +15,7 @@
* limitations under the License.
*/

package com.apache.dubbo.sample.basic;
package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
@@ -24,17 +24,19 @@
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;

public class ApiProvider {
class ApiProvider {
public static void main(String[] args) {
new EmbeddedZooKeeper(TriSampleConstants.ZK_PORT, false).start();

ServiceConfig<IGreeter> service = new ServiceConfig<>();
service.setInterface(IGreeter.class);
service.setRef(new IGreeter1Impl());
service.setToken(true);

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-provider"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, TriSampleConstants.SERVER_PORT))
.service(service)
.start()
.await();

0 comments on commit b4c7fd7

Please sign in to comment.