Skip to content

Add getting started hello world sample for Endpoints gRPC. #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/
24 changes: 24 additions & 0 deletions endpoints/getting-started-grpc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2017 Google Inc.
#
# 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.

# https://github.com/GoogleCloudPlatform/openjdk-runtime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License?

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"]
135 changes: 135 additions & 0 deletions endpoints/getting-started-grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Endpoints Getting Started with gRPC & Java Quickstart

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should tell folks how to find gradle, protoc, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some links. I wanted to keep this terse, as the Quickstart on c.g.c will have all the details. I'll link to that when it is published.

It is assumed that you have a
working
[gRPC](http://www.grpc.io/docs/),
[ProtoBuf](https://github.com/google/protobuf#protocol-compiler-installation) and
Java environment, a Google Cloud account
and [SDK](https://cloud.google.com/sdk/) configured.

1. Build the code:

```bash
./gradlew build
```

1. Test running the code, optional:

```bash
# 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.

```bash
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:

```bash
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.cloud.goog ?? should that be something else?

Copy link
Contributor Author

@jeffmendoza jeffmendoza Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct. Endpoints needs a globally unique name for the service. We were previously using [project].appspot.com, but that was causing confusion, as it's not necessarily using AppEnine, and is not actually setting up DNS to wherever your API is running. [project].cloud.goog is a new namespace available, so all the endpoints docs are using [apiname].endpoints.[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

```bash
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.

```bash
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

```bash
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

```bash
/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

```bash
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.

```bash
gcloud compute instances list
```

1. Run the client

```bash
java -jar client/build/libs/client.jar --host <IP of GCE Instance>:80 --api_key <API Key from Console>
```

### GKE

1. Create a cluster

```bash
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

```bash
kubectl create -f ./container-engine.yaml
```

1. Get IP of load balancer, run until you see an External IP.

```bash
kubectl get svc grpc-hello
```

1. Run the client

```bash
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.
//
// 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 {}
}
}
}
51 changes: 51 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,51 @@
// Copyright 2015, Google Inc.
//
// 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;
}
36 changes: 36 additions & 0 deletions endpoints/getting-started-grpc/api_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2017 Google Inc.
#
# 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.jeffs-test-deploy-1.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.
//
// 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.
//
// 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