Skip to content

Commit

Permalink
Add getting started hello world sample for Endpoints gRPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmendoza committed Feb 3, 2017
1 parent f8a5663 commit c8b9224
Show file tree
Hide file tree
Showing 17 changed files with 988 additions and 0 deletions.
5 changes: 5 additions & 0 deletions endpoints/getting-started-grpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gradle/
api/build/
client/build/
out.pb
server/build/
10 changes: 10 additions & 0 deletions endpoints/getting-started-grpc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://github.com/GoogleCloudPlatform/openjdk-runtime
FROM gcr.io/google_appengine/openjdk8

RUN apt-get update \
&& apt-get -y -q upgrade \
&& rm -rf /var/lib/apt/lists/*

ADD ./server/build/libs/server.jar /hello/server.jar

ENTRYPOINT ["java", "-jar", "/hello/server.jar"]
130 changes: 130 additions & 0 deletions endpoints/getting-started-grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Endpoints Getting Started with gRPC & Java Quickstart

It is assumed that you have a working gRPC and Java environment.

1. Build the code:

```
./gradlew build
```
1. Test running the code, optional:
```
# In the background or another terminal run the server:
java -jar server/build/libs/server.jar
# Check the client parameters:
java -jar client/build/libs/client.jar --help
# Run the client
java -jar client/build/libs/client.jar --greetee 'Endpoints!'
```
1. Generate the `out.pb` from the proto file.
```
protoc --include_imports --include_source_info api/src/main/proto/helloworld.proto --descriptor_set_out out.pb
```
1. Edit, `api_config.yaml`. Replace `MY_PROJECT_ID` with your project id.
1. Deploy your service config to Service Management:
```
gcloud service-management deploy out.pb api_config.yaml
# The Config ID should be printed out, looks like: 2017-02-01r0, remember this
# set your project to make commands easier
GCLOUD_PROJECT=<Your Project ID>
# Print out your Config ID again, in case you missed it
gcloud service-management configs list --service hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog
```
1. Also get an API key from the Console's API Manager for use in the client later. (https://console.cloud.google.com/apis/credentials)
1. Build a docker image for your gRPC server, store in your Registry
```
gcloud container builds submit --tag gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0 .
```
1. Either deploy to GCE (below) or GKE (further down)
### GCE
1. Create your instance and ssh in.
```
gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server
gcloud compute ssh grpc-host
```
1. Set some variables to make commands easier
```
GCLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
SERVICE_NAME=hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog
SERVICE_CONFIG_ID=<Your Config ID>
```
1. Pull your credentials to access Container Registry, and run your gRPC server container
```
/usr/share/google/dockercfg_update.sh
docker run -d --name=grpc-hello gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0
```
1. Run the Endpoints proxy
```
docker run --detach --name=esp \
-p 80:9000 \
--link=grpc-hello:grpc-hello \
gcr.io/endpoints-release/endpoints-runtime:1 \
-s ${SERVICE_NAME} \
-v ${SERVICE_CONFIG_ID} \
-P 9000 \
-a grpc://grpc-hello:50051
```
1. Back on your local machine, get the external IP of your GCE instance.
```
gcloud compute instances list
```
1. Run the client
```
java -jar client/build/libs/client.jar --host <IP of GCE Instance>:80 --api_key <API Key from Console>
```
### GKE
1. Create a cluster
```
gcloud container clusters create my-cluster
```
1. Edit `container-engine.yaml`. Replace `SERVICE_NAME`, `SERVICE_CONFIG_ID`, and `GCLOUD_PROJECT` with your values.
1. Deploy to GKE
```
kubectl create -f ./container-engine.yaml
```
1. Get IP of load balancer, run until you see an External IP.
```
kubectl get svc grpc-hello
```
1. Run the client
```
java -jar client/build/libs/client.jar --host <IP of GKE LoadBalancer>:80 --api_key <API Key from Console>
```
56 changes: 56 additions & 0 deletions endpoints/getting-started-grpc/api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'

}
}

def grpcVersion = '1.0.3'

dependencies {
repositories {
mavenCentral()
}
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.2'
}

plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
52 changes: 52 additions & 0 deletions endpoints/getting-started-grpc/api/src/main/proto/helloworld.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.google.endpoints.examples.hello";
option java_outer_classname = "HelloWorldProto";

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;
}
37 changes: 37 additions & 0 deletions endpoints/getting-started-grpc/api_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.


#
# An example API configuration.
#
# Below, replace MY_PROJECT_ID with your Google Cloud Project ID.
#

# The configuration schema is defined by service.proto file
# https://github.com/googleapis/googleapis/blob/master/google/api/service.proto
type: google.api.Service
config_version: 3

#
# Name of the service configuration.
#
name: hellogrpc.endpoints.MY_PROJECT_ID.cloud.goog

#
# API title to appear in the user interface (Google Cloud Console).
#
title: Hello gRPC API
apis:
- name: helloworld.Greeter
23 changes: 23 additions & 0 deletions endpoints/getting-started-grpc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////

subprojects {
apply plugin: 'java'

repositories {
mavenCentral()
}
}
33 changes: 33 additions & 0 deletions endpoints/getting-started-grpc/client/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////

apply plugin: 'application'

mainClassName = "com.google.endpoints.examples.hello.HelloWorldClient"

jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}

dependencies {
compile project(':api')
compile 'commons-cli:commons-cli:1.3'
}
Loading

0 comments on commit c8b9224

Please sign in to comment.