Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions 1-basic/dubbo-samples-spring-boot-idl-diff-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Dubbo Spring Boot Example using Triple and Protobuf with different package and java_package

This example uses triple as the underlying RPC protocol and IDL to define services. It's highly recommend to use triple and IDL for cross-language communication scenarios. Please refer to [dubbo-samples-spring-boot](../dubbo-samples-spring-boot) for how to use pure Java interface to define services for triple protocol.

This example define the Java package name separately from the service prefix, which is useful in some cross-language calling scenarios.And For instance, in the following IDL definition:

1. The complete service name is `idl.Greeter`, which will be used during rpc calls and service discovery.
2. The Java package name is defined as `org.apache.dubbo.springboot.demo.idl.Greeter`, where the generated Java code will be placed.

# How to run

## Start Nacos
This example replies on Nacos as service discovery registry center, so you need to run the Nacos server first, there are two ways to do so:
1. [Download Nacos binary and start it directly](https://dubbo-next.staged.apache.org/zh-cn/overview/reference/integrations/nacos/#本地下载)
2. [Start Nacos using docker](https://dubbo-next.staged.apache.org/zh-cn/overview/reference/integrations/nacos/#docker)

## Compile

Step into 'dubbo-samples-spring-boot-idl-diff-package' directory, run the following command:

```shell
$ mvn clean compile
```

## Start provider

Enter provider directory:
```shell
$ cd dubbo-samples-spring-boot-idl-diff-package-provider
```

then, run the following command to start provider:
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.springboot.demo.provider.ProviderApplication"
```

Run the following command to see server works as expected:
```shell
curl \
--header "Content-Type: application/json" \
--data '{"name":"Dubbo"}' \
http://localhost:50052/idl.Greeter/greet/
```

## Start consumer

Enter provider directory:
```shell
$ cd dubbo-samples-spring-boot-idl-diff-package-consumer
```

then, run the following command to start consumer:
```shell
$ mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.springboot.demo.consumer.ConsumerApplication"
```



Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* 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.springboot.demo.idl;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.PathResolver;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.ServerService;
import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.model.StubMethodDescriptor;
import org.apache.dubbo.rpc.model.StubServiceDescriptor;
import org.apache.dubbo.rpc.service.Destroyable;
import org.apache.dubbo.rpc.stub.BiStreamMethodHandler;
import org.apache.dubbo.rpc.stub.ServerStreamMethodHandler;
import org.apache.dubbo.rpc.stub.StubInvocationUtil;
import org.apache.dubbo.rpc.stub.StubInvoker;
import org.apache.dubbo.rpc.stub.StubMethodHandler;
import org.apache.dubbo.rpc.stub.StubSuppliers;
import org.apache.dubbo.rpc.stub.UnaryStubMethodHandler;

import com.google.protobuf.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.concurrent.CompletableFuture;

public final class DubboGreeterTriple {

public static final String SERVICE_NAME = Greeter.SERVICE_NAME;

private static final StubServiceDescriptor serviceDescriptor = new StubServiceDescriptor(SERVICE_NAME,Greeter.class);

static {
org.apache.dubbo.rpc.protocol.tri.service.SchemaDescriptorRegistry.addSchemaDescriptor(SERVICE_NAME,GreeterOuterClass.getDescriptor());
StubSuppliers.addSupplier(SERVICE_NAME, DubboGreeterTriple::newStub);
StubSuppliers.addSupplier(Greeter.JAVA_SERVICE_NAME, DubboGreeterTriple::newStub);
StubSuppliers.addDescriptor(SERVICE_NAME, serviceDescriptor);
StubSuppliers.addDescriptor(Greeter.JAVA_SERVICE_NAME, serviceDescriptor);
}

@SuppressWarnings("all")
public static Greeter newStub(Invoker<?> invoker) {
return new GreeterStub((Invoker<Greeter>)invoker);
}

private static final StubMethodDescriptor greetMethod = new StubMethodDescriptor("greet",
org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, org.apache.dubbo.springboot.demo.idl.GreeterReply.class, MethodDescriptor.RpcType.UNARY,
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom,
org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom);

private static final StubMethodDescriptor greetAsyncMethod = new StubMethodDescriptor("greet",
org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, java.util.concurrent.CompletableFuture.class, MethodDescriptor.RpcType.UNARY,
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom,
org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom);

private static final StubMethodDescriptor greetProxyAsyncMethod = new StubMethodDescriptor("greetAsync",
org.apache.dubbo.springboot.demo.idl.GreeterRequest.class, org.apache.dubbo.springboot.demo.idl.GreeterReply.class, MethodDescriptor.RpcType.UNARY,
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), org.apache.dubbo.springboot.demo.idl.GreeterRequest::parseFrom,
org.apache.dubbo.springboot.demo.idl.GreeterReply::parseFrom);




static{
serviceDescriptor.addMethod(greetMethod);
serviceDescriptor.addMethod(greetProxyAsyncMethod);
}

public static class GreeterStub implements Greeter, Destroyable {
private final Invoker<Greeter> invoker;

public GreeterStub(Invoker<Greeter> invoker) {
this.invoker = invoker;
}

@Override
public void $destroy() {
invoker.destroy();
}

@Override
public org.apache.dubbo.springboot.demo.idl.GreeterReply greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){
return StubInvocationUtil.unaryCall(invoker, greetMethod, request);
}

public CompletableFuture<org.apache.dubbo.springboot.demo.idl.GreeterReply> greetAsync(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){
return StubInvocationUtil.unaryCall(invoker, greetAsyncMethod, request);
}

public void greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply> responseObserver){
StubInvocationUtil.unaryCall(invoker, greetMethod , request, responseObserver);
}



}

public static abstract class GreeterImplBase implements Greeter, ServerService<Greeter> {

private <T, R> BiConsumer<T, StreamObserver<R>> syncToAsync(java.util.function.Function<T, R> syncFun) {
return new BiConsumer<T, StreamObserver<R>>() {
@Override
public void accept(T t, StreamObserver<R> observer) {
try {
R ret = syncFun.apply(t);
observer.onNext(ret);
observer.onCompleted();
} catch (Throwable e) {
observer.onError(e);
}
}
};
}

@Override
public CompletableFuture<org.apache.dubbo.springboot.demo.idl.GreeterReply> greetAsync(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){
return CompletableFuture.completedFuture(greet(request));
}

/**
* This server stream type unary method is <b>only</b> used for generated stub to support async unary method.
* It will not be called if you are NOT using Dubbo3 generated triple stub and <b>DO NOT</b> implement this method.
*/
public void greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply> responseObserver){
greetAsync(request).whenComplete((r, t) -> {
if (t != null) {
responseObserver.onError(t);
} else {
responseObserver.onNext(r);
responseObserver.onCompleted();
}
});
}

@Override
public final Invoker<Greeter> getInvoker(URL url) {
PathResolver pathResolver = url.getOrDefaultFrameworkModel()
.getExtensionLoader(PathResolver.class)
.getDefaultExtension();
Map<String,StubMethodHandler<?, ?>> handlers = new HashMap<>();

pathResolver.addNativeStub( "/" + SERVICE_NAME + "/greet");
pathResolver.addNativeStub( "/" + SERVICE_NAME + "/greetAsync");
// for compatibility
pathResolver.addNativeStub( "/" + JAVA_SERVICE_NAME + "/greet");
pathResolver.addNativeStub( "/" + JAVA_SERVICE_NAME + "/greetAsync");


BiConsumer<org.apache.dubbo.springboot.demo.idl.GreeterRequest, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply>> greetFunc = this::greet;
handlers.put(greetMethod.getMethodName(), new UnaryStubMethodHandler<>(greetFunc));
BiConsumer<org.apache.dubbo.springboot.demo.idl.GreeterRequest, StreamObserver<org.apache.dubbo.springboot.demo.idl.GreeterReply>> greetAsyncFunc = syncToAsync(this::greet);
handlers.put(greetProxyAsyncMethod.getMethodName(), new UnaryStubMethodHandler<>(greetAsyncFunc));




return new StubInvoker<>(this, url, Greeter.class, handlers);
}


@Override
public org.apache.dubbo.springboot.demo.idl.GreeterReply greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request){
throw unimplementedMethodException(greetMethod);
}





@Override
public final ServiceDescriptor getServiceDescriptor() {
return serviceDescriptor;
}
private RpcException unimplementedMethodException(StubMethodDescriptor methodDescriptor) {
return TriRpcStatus.UNIMPLEMENTED.withDescription(String.format("Method %s is unimplemented",
"/" + serviceDescriptor.getInterfaceName() + "/" + methodDescriptor.getMethodName())).asException();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.springboot.demo.idl;

import org.apache.dubbo.common.stream.StreamObserver;
import com.google.protobuf.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.concurrent.CompletableFuture;

public interface Greeter extends org.apache.dubbo.rpc.model.DubboStub {

String JAVA_SERVICE_NAME = "org.apache.dubbo.springboot.demo.idl.Greeter";
String SERVICE_NAME = "idl.Greeter";
org.apache.dubbo.springboot.demo.idl.GreeterReply greet(org.apache.dubbo.springboot.demo.idl.GreeterRequest request);

CompletableFuture<org.apache.dubbo.springboot.demo.idl.GreeterReply> greetAsync(org.apache.dubbo.springboot.demo.idl.GreeterRequest request);








}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading