Skip to content

Commit 15cd740

Browse files
wlu2016lesv
authored andcommitted
add gRPC java samples (GoogleCloudPlatform#419)
* add gRPC java samples * remove build directories * update code to address review comments
1 parent 520f5a2 commit 15cd740

File tree

16 files changed

+1275
-0
lines changed

16 files changed

+1275
-0
lines changed

endpoints/bookstore-grpc/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://github.com/GoogleCloudPlatform/openjdk-runtime
2+
FROM gcr.io/google_appengine/openjdk8
3+
4+
RUN echo 'deb http://httpredir.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list \
5+
&& apt-get update \
6+
&& apt-get install --no-install-recommends -y -q ca-certificates \
7+
&& apt-get -y -q upgrade \
8+
&& apt-get install --no-install-recommends -y openjdk-8-jre-headless \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
ADD ./server/build/libs/server.jar /bookstore/server.jar
12+
13+
EXPOSE 8000
14+
15+
ENTRYPOINT ["java", "-jar", "/bookstore/server.jar"]

endpoints/bookstore-grpc/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Cloud Endpoints Bookstore App in Java
2+
3+
## Prerequisites
4+
5+
* [Java 8](http://openjdk.java.net/install/)
6+
* [Docker](https://www.docker.com/products/docker)
7+
8+
## Building and Running the Server
9+
10+
The Java Bookstore gRPC example is built using Gradle:
11+
12+
./gradlew build
13+
14+
To run the Java server and client locally:
15+
16+
# Start the server (listens on port 8000 by default)
17+
java -jar ./server/build/libs/server.jar
18+
19+
# Run the client (connects to localhost:8000 by default)
20+
java -jar ./client/build/libs/client.jar
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
apply plugin: 'java'
18+
apply plugin: 'com.google.protobuf'
19+
20+
buildscript {
21+
repositories {
22+
mavenCentral()
23+
}
24+
dependencies {
25+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
26+
27+
}
28+
}
29+
30+
dependencies {
31+
repositories {
32+
mavenCentral()
33+
}
34+
compile 'io.grpc:grpc-netty:1.0.1'
35+
compile 'io.grpc:grpc-protobuf:1.0.1'
36+
compile 'io.grpc:grpc-stub:1.0.1'
37+
}
38+
39+
protobuf {
40+
protoc {
41+
artifact = 'com.google.protobuf:protoc:3.0.2'
42+
}
43+
44+
plugins {
45+
grpc {
46+
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.1'
47+
}
48+
}
49+
generateProtoTasks {
50+
all()*.plugins {
51+
grpc {}
52+
}
53+
}
54+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
syntax = "proto3";
18+
19+
package endpoints.examples.bookstore;
20+
21+
option java_multiple_files = true;
22+
option java_outer_classname = "BookstoreProto";
23+
option java_package = "com.google.endpoints.examples.bookstore";
24+
25+
26+
import "google/protobuf/empty.proto";
27+
28+
// A simple Bookstore API.
29+
//
30+
// The API manages shelves and books resources. Shelves contain books.
31+
service Bookstore {
32+
// Returns a list of all shelves in the bookstore.
33+
rpc ListShelves(google.protobuf.Empty) returns (ListShelvesResponse) {}
34+
// Creates a new shelf in the bookstore.
35+
rpc CreateShelf(CreateShelfRequest) returns (Shelf) {}
36+
// Returns a specific bookstore shelf.
37+
rpc GetShelf(GetShelfRequest) returns (Shelf) {}
38+
// Deletes a shelf, including all books that are stored on the shelf.
39+
rpc DeleteShelf(DeleteShelfRequest) returns (google.protobuf.Empty) {}
40+
// Returns a list of books on a shelf.
41+
rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {}
42+
// Creates a new book.
43+
rpc CreateBook(CreateBookRequest) returns (Book) {}
44+
// Returns a specific book.
45+
rpc GetBook(GetBookRequest) returns (Book) {}
46+
// Deletes a book from a shelf.
47+
rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {}
48+
}
49+
50+
// A shelf resource.
51+
message Shelf {
52+
// A unique shelf id.
53+
int64 id = 1;
54+
// A theme of the shelf (fiction, poetry, etc).
55+
string theme = 2;
56+
}
57+
58+
// A book resource.
59+
message Book {
60+
// A unique book id.
61+
int64 id = 1;
62+
// An author of the book.
63+
string author = 2;
64+
// A book title.
65+
string title = 3;
66+
}
67+
68+
// Response to ListShelves call.
69+
message ListShelvesResponse {
70+
// Shelves in the bookstore.
71+
repeated Shelf shelves = 1;
72+
}
73+
74+
// Request message for CreateShelf method.
75+
message CreateShelfRequest {
76+
// The shelf resource to create.
77+
Shelf shelf = 1;
78+
}
79+
80+
// Request message for GetShelf method.
81+
message GetShelfRequest {
82+
// The ID of the shelf resource to retrieve.
83+
int64 shelf = 1;
84+
}
85+
86+
// Request message for DeleteShelf method.
87+
message DeleteShelfRequest {
88+
// The ID of the shelf to delete.
89+
int64 shelf = 1;
90+
}
91+
92+
// Request message for ListBooks method.
93+
message ListBooksRequest {
94+
// ID of the shelf which books to list.
95+
int64 shelf = 1;
96+
}
97+
98+
// Response message to ListBooks method.
99+
message ListBooksResponse {
100+
// The books on the shelf.
101+
repeated Book books = 1;
102+
}
103+
104+
// Request message for CreateBook method.
105+
message CreateBookRequest {
106+
// The ID of the shelf on which to create a book.
107+
int64 shelf = 1;
108+
// A book resource to create on the shelf.
109+
Book book = 2;
110+
}
111+
112+
// Request message for GetBook method.
113+
message GetBookRequest {
114+
// The ID of the shelf from which to retrieve a book.
115+
int64 shelf = 1;
116+
// The ID of the book to retrieve.
117+
int64 book = 2;
118+
}
119+
120+
// Request message for DeleteBook method.
121+
message DeleteBookRequest {
122+
// The ID of the shelf from which to delete a book.
123+
int64 shelf = 1;
124+
// The ID of the book to delete.
125+
int64 book = 2;
126+
}

endpoints/bookstore-grpc/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
subprojects {
2+
apply plugin: 'java'
3+
4+
repositories {
5+
mavenCentral()
6+
}
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
apply plugin: 'application'
18+
19+
mainClassName = "com.google.endpoints.examples.bookstore.BookstoreClient"
20+
21+
jar {
22+
manifest {
23+
attributes "Main-Class": "$mainClassName"
24+
}
25+
from {
26+
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
27+
}
28+
}
29+
30+
dependencies {
31+
compile project(':api')
32+
compile 'commons-cli:commons-cli:1.3'
33+
}

0 commit comments

Comments
 (0)