Skip to content

Commit

Permalink
Support GRPC. (sofastack#716)
Browse files Browse the repository at this point in the history
Support GRPC
  • Loading branch information
neokidd authored and leizhiyuan committed Nov 18, 2019
1 parent ce48d97 commit a87b868
Show file tree
Hide file tree
Showing 25 changed files with 1,860 additions and 12 deletions.
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:[email protected]">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();

}
}
Loading

0 comments on commit a87b868

Please sign in to comment.