Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GRPC #716

Merged
merged 26 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
27a02f4
GRPC extension done
neokidd Dec 10, 2018
04ce468
Merge branch 'master' of github.com:alipay/sofa-rpc into dib_grpc
neokidd Dec 11, 2018
3bdc7b5
GPRC integration done
neokidd Dec 21, 2018
b570336
support cluster, router, connection holder, method filter
neokidd Jun 19, 2019
5d1d940
Merge branch 'master' into neokidd_dib_grpc
neokidd Jun 20, 2019
eb2650b
Fix build issues, delete unused components
neokidd Jun 20, 2019
103dc39
Simplify GRPC interfaceID configurations, so that both server and cli…
neokidd Jul 10, 2019
74043f1
Merge branch 'master' into neokidd_dib_grpc
neokidd Jul 11, 2019
835ca98
prepare for 5.6.0 release
neokidd Jul 11, 2019
9f70e8a
discard changes only for dev
neokidd Jul 11, 2019
a2fabf1
prepare for PR
neokidd Jul 11, 2019
ae13f95
update maven plugin version for release
neokidd Jul 16, 2019
89b3bbc
fix build error
neokidd Jul 16, 2019
c4cfa2a
fix test issue
neokidd Jul 16, 2019
1c4382d
test issue fake fix
neokidd Jul 16, 2019
2dacbc4
test issue fake fix 2
neokidd Jul 16, 2019
58d283f
fake fix build issue
neokidd Jul 16, 2019
68e0c2e
all tests pass, try again on travis build env
neokidd Jul 24, 2019
02fcad1
try again
neokidd Jul 24, 2019
e4f3c49
Merge branch 'master' into neokidd_dib_grpc
ujjboy Aug 1, 2019
959bf02
update per review
neokidd Aug 26, 2019
7da4ffe
Merge branch 'neokidd_dib_grpc' of git@github.com:neokidd/sofa-rpc.gi…
neokidd Aug 26, 2019
2ad6ad3
fix bom.xml
neokidd Aug 26, 2019
3583393
Merge branch 'master' into neokidd_dib_grpc
leizhiyuan Nov 6, 2019
be6e483
Update pom.xml
leizhiyuan Nov 6, 2019
347cdc9
update pom.xml
neokidd Nov 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
<artifactId>sofa-rpc-bootstrap-dubbo</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-bootstrap-grpc</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-bootstrap-http</artifactId>
Expand Down Expand Up @@ -226,6 +231,11 @@
<artifactId>sofa-rpc-remoting-resteasy</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-remoting-grpc</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-tracer-opentracing</artifactId>
Expand Down Expand Up @@ -393,6 +403,7 @@
<include>com.alipay.sofa:sofa-rpc-api-compatible</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-bolt</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-dubbo</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-grpc</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-http</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-rest</include>
<include>com.alipay.sofa:sofa-rpc-codec-protobuf</include>
Expand All @@ -412,6 +423,7 @@
<include>com.alipay.sofa:sofa-rpc-remoting-bolt</include>
<include>com.alipay.sofa:sofa-rpc-remoting-http</include>
<include>com.alipay.sofa:sofa-rpc-remoting-resteasy</include>
<include>com.alipay.sofa:sofa-rpc-remoting-grpc</include>
<include>com.alipay.sofa:sofa-rpc-tracer-opentracing</include>
<include>com.alipay.sofa:sofa-rpc-tracer-opentracing-resteasy</include>
<!-- TODO -->
Expand Down
6 changes: 3 additions & 3 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<!-- serialization -->
<hessian.version>3.3.6</hessian.version>
<thrift.version>0.9.2</thrift.version>
<protobuf.version>3.1.0</protobuf.version>
<protobuf.version>3.5.1</protobuf.version>
<jackson.version>2.9.10</jackson.version>
<jackson.databind.version>2.9.10.1</jackson.databind.version>
<msgpack.version>0.6.11</msgpack.version>
Expand Down Expand Up @@ -128,7 +128,7 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -343,7 +343,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.ecwid.consul</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public Class<?> getProxyClass() {
try {
if (StringUtils.isNotBlank(interfaceId)) {
this.proxyClass = ClassUtils.forName(interfaceId);
if (!proxyClass.isInterface()) {
if (!RpcConstants.PROTOCOL_TYPE_GRPC.equals(protocol) && !proxyClass.isInterface()) {
throw ExceptionUtils.buildRuntime("consumer.interface",
interfaceId, "interfaceId must set interface class, not implement class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ public Class<?> getProxyClass() {
if (StringUtils.isNotBlank(interfaceId)) {
this.proxyClass = ClassUtils.forName(interfaceId);
if (!proxyClass.isInterface()) {
throw ExceptionUtils.buildRuntime("service.interfaceId",
interfaceId, "interfaceId must set interface class, not implement class");
if ((getServer() != null) && getServer().size() != 0) {
for (int i = 0; i < getServer().size(); i++) {
if (!"grpc".equals(getServer().get(i).getProtocol())) {
throw ExceptionUtils.buildRuntime("service.interfaceId",
interfaceId, "interfaceId must set interface class, not implement class");
}
}
}
}
} else {
throw ExceptionUtils.buildRuntime("service.interfaceId",
Expand Down
69 changes: 63 additions & 6 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<version>5.6.3-SNAPSHOT</version>
</parent>

<properties>
<protoc.version>3.5.1-1</protoc.version>
<protoc-gen-grpc-java.version>1.17.0</protoc-gen-grpc-java.version>
</properties>

<artifactId>sofa-rpc-example</artifactId>

<dependencies>
Expand All @@ -19,6 +24,23 @@
<version>${project.parent.version}</version>
</dependency>

<!-- grpc -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.17.0</version>
</dependency>

<!-- dependencies for rest begin -->
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
Expand Down Expand Up @@ -129,12 +151,6 @@
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
</extension>
</extensions>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
Expand All @@ -156,7 +172,35 @@
</testResource>
</testResources>

<!-- grpc -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</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:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${protoc-gen-grpc-java.version}: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 All @@ -166,6 +210,19 @@
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.github.neokidd.maven.plugins</groupId>
<artifactId>hack-modifier-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>hack</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 com.alipay.sofa.rpc.grpc.registry;

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

import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.stub.StreamObserver;

class GreeterImpl extends GreeterGrpc.GreeterImplBase {

//Intentionally using unsupported format
static final DateTimeFormatter[] datetimeFormatter = new DateTimeFormatter[] { DateTimeFormatter.ISO_DATE_TIME,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
DateTimeFormatter.BASIC_ISO_DATE };

@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloRequest.DateTime reqDateTime = req.getDateTime();
int i = 0;
try {
i = Integer.parseInt(reqDateTime.getTime());
} catch (Exception e) {
//TODO: handle exception
}
LocalDateTime dt = LocalDateTime.now();
String dtStr = dt.format(datetimeFormatter[i % datetimeFormatter.length]);
HelloRequest.DateTime rplyDateTime = HelloRequest.DateTime.newBuilder(reqDateTime)
.setDate(dtStr).build();
HelloReply reply = HelloReply.newBuilder()
.setMessage("Hello " + req.getName())
.setDateTime(rplyDateTime)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 com.alipay.sofa.rpc.grpc.registry;

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:luanyanqiang@dibgroup.cn">Luan Yanqiang</a>
*/
public class GrpcClientRegistryApplication {

static final DateTimeFormatter[] datetimeFormatter = new DateTimeFormatter[] { DateTimeFormatter.ISO_DATE_TIME,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
DateTimeFormatter.BASIC_ISO_DATE };

public static void main(String[] args) {
final Logger LOGGER = LoggerFactory.getLogger(GrpcClientRegistryApplication.class);

RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper").setAddress("127.0.0.1:2181");

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

// GreeterGrpc.GreeterBlockingStub s = new
GreeterGrpc.GreeterBlockingStub greeterBlockingStub = (GreeterGrpc.GreeterBlockingStub) consumerConfig.refer();

LOGGER.info("Grpc stub bean successful: {}", greeterBlockingStub.getClass().getName());

LOGGER.info("Will try to greet " + "world" + " ...");
HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00")
.build();
HelloRequest request = HelloRequest.newBuilder().setName("world").build();
HelloReply reply = null;
try {
for (int i = 0; i < 10000; i++) {
try {
HelloRequest.DateTime reqDateTime = HelloRequest.DateTime.newBuilder(dateTime).setTime("" + i)
.build();
request = HelloRequest.newBuilder(request).setName("world_" + i).setDateTime(reqDateTime).build();
reply = greeterBlockingStub.sayHello(request);
LOGGER.info("Greeting: {}, {}", reply.getMessage(), reply.getDateTime().getDate());
// Object r = greeterBlockingStub.sayHello(request);
// LOGGER.info("Greeting: {}, {}", r.toString(), r.toString());
} catch (StatusRuntimeException e) {
LOGGER.error("RPC failed: {}", e.getStatus());
} catch (Throwable e) {
LOGGER.error("Unexpected RPC call breaks", e);
}

try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
} catch (Exception e) {
LOGGER.error("Unexpected RPC call breaks", e);
}

synchronized (GrpcClientRegistryApplication.class) {
try {
while (true) {
GrpcClientRegistryApplication.class.wait();
}
} catch (InterruptedException e) {
LOGGER.error("Exit by Interrupted");
}
}

consumerConfig.unRefer();

}
}