-
Notifications
You must be signed in to change notification settings - Fork 3.9k
servlet: Implement gRPC server as a Servlet #4738
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
a0c92df
POC: Implement gRPC server as a Servlet
dapengzhang0 8719377
better-logging
dapengzhang0 46d00ec
get rid of public ServerImpl
dapengzhang0 4fa6271
rename files
dapengzhang0 e793c7c
fix serverListener NPE
dapengzhang0 393fcf6
fix onSendingBytes()
dapengzhang0 706a3c9
favor ConcurrentLinkedQueue for library
dapengzhang0 5e11674
refactor ServletAdapterImpl to ServletAdapter
dapengzhang0 1776917
not to use CDI for simplicity
dapengzhang0 bb3a720
add GrpcServlet(ServletServerBuilder serverBuilder)
dapengzhang0 4c7cd4f
add UndertowInteropTest and UndertowAbstractTest
dapengzhang0 af7f6a3
fix codecov
dapengzhang0 2fc04fc
minor enhancements
dapengzhang0 6fb4fc2
Merge branch 'master' of https://github.com/grpc/grpc-java into servl…
dapengzhang0 8b78b6d
update example gradle files
dapengzhang0 680872b
Merge branch 'master' of https://github.com/grpc/grpc-java into servl…
dapengzhang0 b3473df
Merge branch 'master' of https://github.com/grpc/grpc-java into servl…
dapengzhang0 3728866
revert core/src/test/java/io/grpc/internal/AbstractTransportTest.java
dapengzhang0 58fce63
ignore some transport tests
dapengzhang0 5b02064
cleanup GrpcServlet constructor
dapengzhang0 d59f81e
better way to get authority
dapengzhang0 5b7496e
comment SPSC
dapengzhang0 e98e4f8
attributes.toBuilder()
dapengzhang0 6aa26e4
move writeState and WriteListener
dapengzhang0 c3dfaba
Merge branch 'master' of https://github.com/grpc/grpc-java into servl…
dapengzhang0 44ea5f3
revert AbstractTransportTest reformat
dapengzhang0 ef5f879
temp
dapengzhang0 47e3e95
fix some of review comments
dapengzhang0 c5fc2bd
create util methods
dapengzhang0 63e8cb2
minor cleanup
dapengzhang0 986a885
Merge branch 'master' of https://github.com/grpc/grpc-java into servl…
dapengzhang0 3fd4ca8
factor out write path so that easy to replace with other implementation
dapengzhang0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
plugins { | ||
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier gradle versions | ||
id 'com.google.protobuf' version '0.8.8' | ||
// Generate IntelliJ IDEA's .idea & .iml project files | ||
id 'idea' | ||
id 'war' | ||
} | ||
|
||
repositories { | ||
maven { // The google mirror is less flaky than mavenCentral() | ||
url "https://maven-central.storage-download.googleapis.com/repos/central/data/" } | ||
mavenLocal() | ||
} | ||
|
||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
def grpcVersion = '1.24.0-SNAPSHOT' // CURRENT_GRPC_VERSION | ||
def protocVersion = '3.9.0' | ||
|
||
dependencies { | ||
implementation "io.grpc:grpc-protobuf:${grpcVersion}", | ||
"io.grpc:grpc-servlet:${grpcVersion}", | ||
"io.grpc:grpc-stub:${grpcVersion}" | ||
|
||
providedCompile "javax.annotation:javax.annotation-api:1.2", | ||
"javax.servlet:javax.servlet-api:4.0.1" | ||
} | ||
|
||
protobuf { | ||
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" } | ||
plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } } | ||
generateProtoTasks { | ||
all()*.plugins { grpc {} } | ||
} | ||
} | ||
|
||
// 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' | ||
} | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
pluginManagement { | ||
repositories { | ||
maven { // The google mirror is less flaky than mavenCentral() | ||
url "https://maven-central.storage-download.googleapis.com/repos/central/data/" | ||
} | ||
gradlePluginPortal() | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
.../example-servlet/src/main/java/io/grpc/servlet/examples/helloworld/HelloWorldServlet.java
This file contains hidden or 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,79 @@ | ||
/* | ||
* 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.ServletAdapter; | ||
import io.grpc.servlet.ServletServerBuilder; | ||
import java.io.IOException; | ||
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 over HTTP/2 and shares the resource URI for the normal servlet | ||
* clients over HTTP/1.0+. | ||
* | ||
* <p>For creating a servlet that solely serves gRPC services, do not follow this example, simply | ||
* extend or register a {@link io.grpc.servlet.GrpcServlet} instead. | ||
*/ | ||
@WebServlet(urlPatterns = {"/helloworld.Greeter/SayHello"}, asyncSupported = true) | ||
public class HelloWorldServlet extends HttpServlet { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final ServletAdapter servletAdapter = | ||
new ServletServerBuilder().addService(new GreeterImpl()).buildServletAdapter(); | ||
|
||
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(); | ||
} | ||
} | ||
|
||
@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>"); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
servletAdapter.destroy(); | ||
super.destroy(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/example-servlet/src/main/proto/helloworld/helloworld.proto
This file contains hidden or 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; | ||
} |
6 changes: 6 additions & 0 deletions
6
examples/example-servlet/src/main/webapp/WEB-INF/glassfish-web.xml
This file contains hidden or 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,6 @@ | ||
<?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=""> | ||
<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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
description = "gRPC: Servlet" | ||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
dependencies { | ||
compile project(':grpc-core') | ||
compileOnly 'javax.servlet:javax.servlet-api:4.0.1', | ||
libraries.javax_annotation // java 9, 10 needs it | ||
|
||
testCompile project(':grpc-stub'), | ||
project(':grpc-protobuf'), | ||
project(':grpc-servlet'), | ||
project(':grpc-netty'), | ||
project(':grpc-testing'), | ||
project(':grpc-auth'), | ||
project(':grpc-interop-testing'), | ||
project(':grpc-core').sourceSets.test.output, | ||
project(':grpc-netty').sourceSets.test.output, | ||
libraries.junit, | ||
'io.undertow:undertow-servlet:2.0.22.Final', | ||
'org.apache.tomcat.embed:tomcat-embed-core:9.0.20' | ||
} | ||
|
||
// Jetty only works with Java 11 | ||
if (JavaVersion.current().isJava11Compatible()) { | ||
compileTestJava { | ||
sourceCompatibility = "11" | ||
targetCompatibility = "11" | ||
} | ||
|
||
def jettyVersion = '10.0.0-alpha0' | ||
dependencies { | ||
testCompile "org.eclipse.jetty:jetty-servlet:${jettyVersion}", | ||
"org.eclipse.jetty.http2:http2-server:${jettyVersion}" | ||
} | ||
} else { | ||
sourceSets { | ||
test { | ||
java { | ||
exclude '**/Jetty*Test.java' | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.