You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/11/15 05:51:41 UTC

[dubbo] 03/03: fix grpc impl proxy problem

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit 610b3e6e13140fb044f3875459f0b115e39c8d61
Author: ken.lj <ke...@gmail.com>
AuthorDate: Thu Nov 14 20:56:36 2019 +0800

    fix grpc impl proxy problem
---
 .../dubbo/rpc/protocol/grpc/GrpcProtocol.java      | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcProtocol.java b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcProtocol.java
index 5cdf150..aa862a5 100644
--- a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcProtocol.java
+++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcProtocol.java
@@ -25,6 +25,9 @@ import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.ProtocolServer;
 import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ProviderModel;
+import org.apache.dubbo.rpc.model.ServiceRepository;
 import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol;
 
 import io.grpc.BindableService;
@@ -55,7 +58,7 @@ public class GrpcProtocol extends AbstractProxyProtocol {
     private final ConcurrentMap<String, ManagedChannel> channelMap = new ConcurrentHashMap<>();
 
     @Override
-    protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
+    protected <T> Runnable doExport(T proxiedImpl, Class<T> type, URL url) throws RpcException {
         String key = url.getAddress();
         ProtocolServer protocolServer = serverMap.computeIfAbsent(key, k -> {
             DubboHandlerRegistry registry = new DubboHandlerRegistry();
@@ -71,7 +74,23 @@ public class GrpcProtocol extends AbstractProxyProtocol {
         });
 
         GrpcRemotingServer grpcServer = (GrpcRemotingServer) protocolServer.getRemotingServer();
-        grpcServer.getRegistry().addService((BindableService) impl, url.getServiceKey());
+
+        ServiceRepository serviceRepository = ApplicationModel.getServiceRepository();
+        ProviderModel providerModel = serviceRepository.lookupExportedService(url.getServiceKey());
+        if (providerModel == null) {
+            throw new IllegalStateException("Service " + url.getServiceKey() + "should have already been stored in service repository, " +
+                    "but failed to find it.");
+        }
+        Object originalImpl = providerModel.getServiceInstance();
+
+        Class<?> implClass = originalImpl.getClass();
+        try {
+            Method method = implClass.getDeclaredMethod("setProxiedImpl", type);
+            method.invoke(originalImpl, proxiedImpl);
+        } catch (Exception e) {
+            throw new IllegalStateException();
+        }
+        grpcServer.getRegistry().addService((BindableService) originalImpl, url.getServiceKey());
 
         grpcServer.start();