-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: Implement gRPC server as a Servlet
- Loading branch information
1 parent
989bc87
commit 09c60d8
Showing
21 changed files
with
1,392 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
description = "gRPC: Servlet example" | ||
buildscript { | ||
repositories { | ||
maven { // The google mirror is less flaky than mavenCentral() | ||
url "https://maven-central.storage-download.googleapis.com/repos/central/data/" } | ||
|
||
} | ||
dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3' } | ||
} | ||
|
||
repositories { | ||
maven { // The google mirror is less flaky than mavenCentral() | ||
url "https://maven-central.storage-download.googleapis.com/repos/central/data/" } | ||
mavenLocal() | ||
} | ||
|
||
apply plugin: 'war' | ||
|
||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
def grpcVersion = '1.15.0-SNAPSHOT' // CURRENT_GRPC_VERSION | ||
def protobufVersion = '3.5.1' | ||
def protocVersion = '3.5.1-1' | ||
|
||
dependencies { | ||
compile "io.grpc:grpc-stub:${grpcVersion}" | ||
compile "io.grpc:grpc-protobuf:${grpcVersion}" | ||
compile "io.grpc:grpc-servlet:${grpcVersion}" | ||
|
||
// for container that already supports CDI 2 | ||
// providedCompile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1' | ||
|
||
// for container that needs CDI 2 | ||
compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1' | ||
compile "org.jboss.weld.servlet:weld-servlet-core:3.0.5.Final" | ||
|
||
compile ("com.google.protobuf:protobuf-java-util:${protobufVersion}") { | ||
exclude group: 'com.google.guava', module: 'guava' | ||
} | ||
|
||
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' | ||
} | ||
|
||
apply plugin: 'com.google.protobuf' | ||
protobuf { | ||
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" } | ||
plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.13.1" } } | ||
generateProtoTasks { | ||
all()*.plugins { grpc {} } | ||
} | ||
} | ||
|
||
apply plugin: 'idea' | ||
|
||
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code. | ||
sourceSets { | ||
main { | ||
java { | ||
srcDirs 'build/generated/source/proto/main/grpc' | ||
srcDirs 'build/generated/source/proto/main/java' | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
.../example-servlet/src/main/java/io/grpc/servlet/examples/helloworld/HelloWorldServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2018 The gRPC Authors | ||
* | ||
* Licensed 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 io.grpc.servlet.examples.helloworld; | ||
|
||
import io.grpc.servlet.ServletAdapter; | ||
import java.io.IOException; | ||
import javax.inject.Inject; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* A servlet that hosts a gRPC server. | ||
*/ | ||
@WebServlet(urlPatterns = {"/helloworld.Greeter/SayHello"}, asyncSupported = true) | ||
public class HelloWorldServlet extends HttpServlet { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Inject | ||
private ServletAdapter servletAdapter; | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
response.setContentType("text/html"); | ||
response.getWriter().println("<p>Hello World!</p>"); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
if (ServletAdapter.isGrpc(request)) { | ||
servletAdapter.doPost(request, response); | ||
} else { | ||
response.setContentType("text/html"); | ||
response.getWriter().println("<p>Hello non-gRPC client!</p>"); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ple-servlet/src/main/java/io/grpc/servlet/examples/helloworld/ServletAdapterProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2018 The gRPC Authors | ||
* | ||
* Licensed 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 io.grpc.servlet.examples.helloworld; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.grpc.servlet.ServerBuilder; | ||
import io.grpc.servlet.ServletAdapter; | ||
import java.util.concurrent.Executors; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.Produces; | ||
|
||
/** | ||
* A ManagedBean that produces an instance of ServletAdapter. | ||
*/ | ||
@ApplicationScoped | ||
final class ServletAdapterProvider { | ||
@Produces | ||
private ServletAdapter getServletAdapter() { | ||
return Provider.servletAdapter; | ||
} | ||
|
||
private static final class Provider { | ||
static final ServletAdapter servletAdapter = ServletAdapter.Factory.create( | ||
new ServerBuilder().addService(new GreeterImpl())); | ||
} | ||
|
||
private static final class GreeterImpl extends GreeterGrpc.GreeterImplBase { | ||
GreeterImpl() {} | ||
|
||
@Override | ||
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { | ||
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); | ||
responseObserver.onNext(reply); | ||
responseObserver.onCompleted(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/example-servlet/src/main/proto/helloworld/helloworld.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2015 The gRPC Authors | ||
// | ||
// Licensed 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. | ||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "io.grpc.examples.helloworld"; | ||
option java_outer_classname = "HelloWorldProto"; | ||
option objc_class_prefix = "HLW"; | ||
|
||
package helloworld; | ||
|
||
// The greeting service definition. | ||
service Greeter { | ||
// Sends a greeting | ||
rpc SayHello (HelloRequest) returns (HelloReply) {} | ||
} | ||
|
||
// The request message containing the user's name. | ||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
// The response message containing the greetings | ||
message HelloReply { | ||
string message = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Need this file for deployment to Jetty --> | ||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" bean-discovery-mode="all" version="2.0"> | ||
<scan></scan> | ||
</beans> |
7 changes: 7 additions & 0 deletions
7
examples/example-servlet/src/main/webapp/WEB-INF/glassfish-web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Need this file for deployment to GlassFish --> | ||
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"> | ||
<glassfish-web-app error-url=""> | ||
<context-root></context-root> | ||
<class-loader delegate="false"/> | ||
</glassfish-web-app> |
9 changes: 9 additions & 0 deletions
9
examples/example-servlet/src/main/webapp/WEB-INF/jboss-web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Need this file for deployment to WildFly --> | ||
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
http://www.jboss.com/xml/ns/javaee | ||
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"> | ||
<context-root>/</context-root> | ||
</jboss-web> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
description = "gRPC: Servlet" | ||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
dependencies { | ||
compile project(':grpc-core') | ||
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' | ||
compileOnly libraries.javax_annotation // java 9, 10 needs it | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
description = "gRPC: Servlet testing" | ||
buildscript { | ||
repositories { | ||
maven { // The google mirror is less flaky than mavenCentral() | ||
url "https://maven-central.storage-download.googleapis.com/repos/central/data/" } | ||
|
||
} | ||
dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3' } | ||
} | ||
|
||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
apply plugin: 'war' | ||
|
||
apply plugin: 'com.google.protobuf' | ||
protobuf { | ||
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" } | ||
plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.13.1" } } | ||
generateProtoTasks { | ||
all()*.plugins { grpc {} } | ||
} | ||
} | ||
|
||
sourceSets { | ||
main { | ||
java { | ||
srcDirs "${rootDir}/interop-testing/src/main/java" | ||
// include only the necessary source files in interop-testing | ||
fileTree("${rootDir}/interop-testing/src/main/java") | ||
.exclude("io/grpc/testing/integration/Util.java") | ||
.exclude("io/grpc/testing/integration/TestServiceImpl.java") | ||
.each { | ||
exclude new File("${rootDir}/interop-testing/src/main/java").toPath() | ||
.relativize(it.toPath()).toString() } | ||
} | ||
|
||
proto { srcDirs "${rootDir}/interop-testing/src/main/proto" } | ||
|
||
resources { srcDirs "${rootDir}/interop-testing/src/main/resources" } | ||
} | ||
} | ||
|
||
dependencies { | ||
compile project(':grpc-core') | ||
compile project(':grpc-stub') | ||
compile project(':grpc-protobuf') | ||
compile project(':grpc-servlet') | ||
|
||
// for container that already supports CDI 2 | ||
// providedCompile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1' | ||
|
||
// for container that needs CDI 2 | ||
compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1' | ||
compile "org.jboss.weld.servlet:weld-servlet-core:3.0.5.Final" | ||
|
||
compile ("com.google.protobuf:protobuf-java-util:${protobufVersion}") { | ||
exclude group: 'com.google.guava', module: 'guava' | ||
} | ||
|
||
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' | ||
|
||
providedCompile "junit:junit:4.12" | ||
} | ||
|
||
compileJava { | ||
options.compilerArgs += [ | ||
// Protobuf-generated code produces some warnings. | ||
// https://github.com/google/protobuf/issues/2718 | ||
"-Xlint:-cast", | ||
"-XepExcludedPaths:.*/generated/.*/java/.*", | ||
] | ||
} |
Oops, something went wrong.