Skip to content

Commit

Permalink
Feat/optimize grpc (sofastack#816)
Browse files Browse the repository at this point in the history
* chore:move package

* chore:rename package

* test:add test case for grpc

* chore:organize import

* chore:fix format

* test:fix test case

* chore:fix cr
  • Loading branch information
leizhiyuan authored Dec 23, 2019
1 parent 4166ae2 commit db18a70
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.sofa.rpc.bootstrap.Bootstraps;
import com.alipay.sofa.rpc.bootstrap.ProviderBootstrap;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.utils.ClassUtils;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.common.utils.ExceptionUtils;
Expand Down Expand Up @@ -160,7 +161,7 @@ public Class<?> getProxyClass() {
if (!proxyClass.isInterface()) {
if ((getServer() != null) && getServer().size() != 0) {
for (int i = 0; i < getServer().size(); i++) {
if (!"grpc".equals(getServer().get(i).getProtocol())) {
if (!RpcConstants.PROTOCOL_TYPE_GRPC.equals(getServer().get(i).getProtocol())) {
throw ExceptionUtils.buildRuntime("service.interfaceId",
interfaceId, "interfaceId must set interface class, not implement class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.grpc.registry;
package com.alipay.sofa.rpc.grpc;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.grpc.registry;
package com.alipay.sofa.rpc.grpc;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.grpc.registry.GreeterImpl;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

import io.grpc.StatusRuntimeException;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;

/**
* @author <a href="mailto:[email protected]">Luan Yanqiang</a>
Expand All @@ -48,7 +46,7 @@ public static void main(String[] args) {

ConsumerConfig<GreeterGrpc.GreeterBlockingStub> consumerConfig = new ConsumerConfig<GreeterGrpc.GreeterBlockingStub>();
consumerConfig.setInterfaceId(GreeterGrpc.class.getName())
.setProtocol("grpc")
.setProtocol(RpcConstants.PROTOCOL_TYPE_GRPC)
.setRegistry(registryConfig);

// GreeterGrpc.GreeterBlockingStub s = new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.grpc.registry;
package com.alipay.sofa.rpc.grpc;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.config.ApplicationConfig;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.dib.sofa.rpc.bootstrap.grpc.GrpcProviderBootstrap;
import com.alipay.sofa.rpc.grpc.*;
import io.grpc.examples.helloworld.*;

/**
Expand All @@ -48,12 +47,12 @@ public static void main(String[] args) {
.setAddress("127.0.0.1:2181");

ServerConfig serverConfig = new ServerConfig()
.setProtocol("grpc")
.setProtocol(RpcConstants.PROTOCOL_TYPE_GRPC)
.setPort(port);

ProviderConfig<GreeterImpl> providerConfig = new ProviderConfig<GreeterImpl>()
.setApplication(applicationConfig)
.setBootstrap("grpc")
.setBootstrap(RpcConstants.PROTOCOL_TYPE_GRPC)
.setInterfaceId(GreeterGrpc.class.getName())
.setRef(new GreeterImpl())
.setServer(serverConfig)
Expand Down
20 changes: 0 additions & 20 deletions extension-impl/bootstrap-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,6 @@
</extensions>

<plugins>
<!-- grpc -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.17.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dib.sofa.rpc.bootstrap.grpc;
package com.alipay.sofa.rpc.bootstrap.grpc;

import com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap;
import com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap;
import com.alipay.sofa.rpc.client.Cluster;
import com.alipay.sofa.rpc.client.ClusterFactory;
import com.alipay.sofa.rpc.client.ProviderGroup;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.utils.ClassUtils;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.config.ApplicationConfig;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.MethodConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.listener.ConfigListener;
import com.alipay.sofa.rpc.listener.ProviderInfoListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import io.grpc.BindableService;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.AbstractStub;

import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.proxy.Proxy;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.LoaderClassPath;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.message.MessageBuilder;

import com.alipay.sofa.rpc.core.exception.RpcErrorType;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;

import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dib.sofa.rpc.bootstrap.grpc;
package com.alipay.sofa.rpc.bootstrap.grpc;

import com.alipay.sofa.rpc.bootstrap.ProviderBootstrap;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.Version;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.*;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.common.struct.NamedThreadFactory;

import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.Extension;
Expand All @@ -37,23 +36,18 @@
import com.alipay.sofa.rpc.server.ProviderProxyInvoker;
import com.alipay.sofa.rpc.server.Server;

// import io.grpc.Server;
// import io.grpc.ServerBuilder;
// import io.grpc.BindableService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import java.io.IOException;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

// import io.grpc.Server;
// import io.grpc.ServerBuilder;
// import io.grpc.BindableService;

/**
* Provider bootstrap for grpc
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
grpc=com.dib.sofa.rpc.bootstrap.grpc.GrpcConsumerBootstrap
grpc=com.alipay.sofa.rpc.bootstrap.grpc.GrpcConsumerBootstrap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
grpc=com.dib.sofa.rpc.bootstrap.grpc.GrpcProviderBootstrap
grpc=com.alipay.sofa.rpc.bootstrap.grpc.GrpcProviderBootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,20 @@
*/
package com.alipay.sofa.rpc.transport.grpc;

import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.rpc.common.utils.ClassUtils;
import com.alipay.sofa.rpc.core.exception.RpcErrorType;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;

import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.MethodDescriptor;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.stub.ClientCalls;
import io.grpc.stub.StreamObserver;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

/**
* Invoker for Grpc
*
Expand Down Expand Up @@ -68,7 +58,6 @@ public class GrpcClientInvoker {
/**
* The constructor
* @param sofaRequest The SofaRequest
* @param methodDescriptor The MethodDescriptor
* @param channel The Channel
*/
public GrpcClientInvoker(SofaRequest sofaRequest, Channel channel) {
Expand All @@ -85,25 +74,12 @@ public GrpcClientInvoker(SofaRequest sofaRequest, Channel channel) {
try {
requestClass.cast(request);
} catch (ClassCastException e) {
//TODO: handle exception
LOGGER.error("Request type error!");
throw e;
}

// if (request instanceof String ) {
// Object realRequest = makeRequestFromString((String)request);
// }
this.timeout = sofaRequest.getTimeout();
}

public Object makeRequestFromString(String in) {
int end = in.lastIndexOf('}');
int begine = in.indexOf('{');
String trimmed = in.substring(begine, end + 1);
JSONObject jObject = JSONObject.parseObject(trimmed);
return new Object();
}

public SofaResponse invoke() {
Object response = invokeRequestMethod();
SofaResponse r = new SofaResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
import io.grpc.ConnectivityState;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -86,7 +84,6 @@ public void disconnect() {
try {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
LOGGER.warn("GRPC channel shut down interrupted.");
e.printStackTrace();
}
Expand Down Expand Up @@ -132,11 +129,9 @@ public ResponseFuture asyncSend(SofaRequest message, int timeout) throws SofaRpc

@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
String methodName = request.getMethodName();
try {
SofaResponse r = new GrpcClientInvoker(request, channel).invoke();
return r;
// return new SofaResponse();
} catch (Exception e) {
throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Grpc invoke error", e);
}
Expand Down
Loading

0 comments on commit db18a70

Please sign in to comment.