GeezerVison is showing github.com — Add getting started hello world sample for Endpoints gRPC. · foreverimps/java-docs-samples@8f57b0e
File treeExpand file treeCollapse file tree Open diff view settings endpoints/getting-started-grpc src/main/java/com/google/endpoints/examples/hello
src/main/java/com/google/endpoints/examples/hello
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +.gradle/ | | 2 | +api/build/ | | 3 | +client/build/ | | 4 | +out.pb | | 5 | +server/build/ |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# https://github.com/GoogleCloudPlatform/openjdk-runtime | | 2 | +FROM gcr.io/google_appengine/openjdk8 | | 3 | + | | 4 | +RUN apt-get update \ | | 5 | + && apt-get -y -q upgrade \ | | 6 | + && rm -rf /var/lib/apt/lists/* | | 7 | + | | 8 | +ADD ./server/build/libs/server.jar /hello/server.jar | | 9 | + | | 10 | +ENTRYPOINT ["java", "-jar", "/hello/server.jar"] |
Collapse file Copy file name to clipboard +130Lines changed: 130 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Endpoints Getting Started with gRPC & Java Quickstart | | 2 | + | | 3 | +It is assumed that you have a working gRPC and Java environment. | | 4 | + | | 5 | +1. Build the code: | | 6 | + | | 7 | + ``` | | 8 | + ./gradlew build | | 9 | + ``` | | 10 | +
| | 11 | +1. Test running the code, optional: | | 12 | +
| | 13 | + ``` | | 14 | + # In the background or another terminal run the server: | | 15 | + java -jar server/build/libs/server.jar | | 16 | +
| | 17 | + # Check the client parameters: | | 18 | + java -jar client/build/libs/client.jar --help | | 19 | +
| | 20 | + # Run the client | | 21 | + java -jar client/build/libs/client.jar --greetee 'Endpoints!' | | 22 | + ``` | | 23 | +
| | 24 | +1. Generate the `out.pb` from the proto file. | | 25 | +
| | 26 | + ``` | | 27 | + protoc --include_imports --include_source_info api/src/main/proto/helloworld.proto --descriptor_set_out out.pb | | 28 | + ``` | | 29 | +
| | 30 | +1. Edit, `api_config.yaml`. Replace `MY_PROJECT_ID` with your project id. | | 31 | +
| | 32 | +1. Deploy your service config to Service Management: | | 33 | +
| | 34 | + ``` | | 35 | + gcloud service-management deploy out.pb api_config.yaml | | 36 | + # The Config ID should be printed out, looks like: 2017-02-01r0, remember this | | 37 | +
| | 38 | + # set your project to make commands easier | | 39 | + GCLOUD_PROJECT=<Your Project ID> | | 40 | +
| | 41 | + # Print out your Config ID again, in case you missed it | | 42 | + gcloud service-management configs list --service hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog | | 43 | + ``` | | 44 | +
| | 45 | +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) | | 46 | +
| | 47 | +1. Build a docker image for your gRPC server, store in your Registry | | 48 | +
| | 49 | + ``` | | 50 | + gcloud container builds submit --tag gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0 . | | 51 | + ``` | | 52 | +
| | 53 | +1. Either deploy to GCE (below) or GKE (further down) | | 54 | +
| | 55 | +### GCE | | 56 | +
| | 57 | +1. Create your instance and ssh in. | | 58 | +
| | 59 | + ``` | | 60 | + gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server | | 61 | + gcloud compute ssh grpc-host | | 62 | + ``` | | 63 | +
| | 64 | +1. Set some variables to make commands easier | | 65 | +
| | 66 | + ``` | | 67 | + GCLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") | | 68 | + SERVICE_NAME=hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog | | 69 | + SERVICE_CONFIG_ID=<Your Config ID> | | 70 | + ``` | | 71 | +
| | 72 | +1. Pull your credentials to access Container Registry, and run your gRPC server container | | 73 | +
| | 74 | + ``` | | 75 | + /usr/share/google/dockercfg_update.sh | | 76 | + docker run -d --name=grpc-hello gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0 | | 77 | + ``` | | 78 | +
| | 79 | +1. Run the Endpoints proxy | | 80 | +
| | 81 | + ``` | | 82 | + docker run --detach --name=esp \ | | 83 | + -p 80:9000 \ | | 84 | + --link=grpc-hello:grpc-hello \ | | 85 | + gcr.io/endpoints-release/endpoints-runtime:1 \ | | 86 | + -s ${SERVICE_NAME} \ | | 87 | + -v ${SERVICE_CONFIG_ID} \ | | 88 | + -P 9000 \ | | 89 | + -a grpc://grpc-hello:50051 | | 90 | + ``` | | 91 | +
| | 92 | +1. Back on your local machine, get the external IP of your GCE instance. | | 93 | +
| | 94 | + ``` | | 95 | + gcloud compute instances list | | 96 | + ``` | | 97 | +
| | 98 | +1. Run the client | | 99 | +
| | 100 | + ``` | | 101 | + java -jar client/build/libs/client.jar --host <IP of GCE Instance>:80 --api_key <API Key from Console> | | 102 | + ``` | | 103 | +
| | 104 | +### GKE | | 105 | +
| | 106 | +1. Create a cluster | | 107 | +
| | 108 | + ``` | | 109 | + gcloud container clusters create my-cluster | | 110 | + ``` | | 111 | +
| | 112 | +1. Edit `container-engine.yaml`. Replace `SERVICE_NAME`, `SERVICE_CONFIG_ID`, and `GCLOUD_PROJECT` with your values. | | 113 | +
| | 114 | +1. Deploy to GKE | | 115 | +
| | 116 | + ``` | | 117 | + kubectl create -f ./container-engine.yaml | | 118 | + ``` | | 119 | +
| | 120 | +1. Get IP of load balancer, run until you see an External IP. | | 121 | +
| | 122 | + ``` | | 123 | + kubectl get svc grpc-hello | | 124 | + ``` | | 125 | +
| | 126 | +1. Run the client | | 127 | +
| | 128 | + ``` | | 129 | + java -jar client/build/libs/client.jar --host <IP of GKE LoadBalancer>:80 --api_key <API Key from Console> | | 130 | + ``` |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright 2017 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 | +def grpcVersion = '1.0.3' | | 31 | + | | 32 | +dependencies { | | 33 | + repositories { | | 34 | + mavenCentral() | | 35 | + } | | 36 | + compile "io.grpc:grpc-netty:${grpcVersion}" | | 37 | + compile "io.grpc:grpc-protobuf:${grpcVersion}" | | 38 | + compile "io.grpc:grpc-stub:${grpcVersion}" | | 39 | +} | | 40 | + | | 41 | +protobuf { | | 42 | + protoc { | | 43 | + artifact = 'com.google.protobuf:protoc:3.0.2' | | 44 | + } | | 45 | + | | 46 | + plugins { | | 47 | + grpc { | | 48 | + artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" | | 49 | + } | | 50 | + } | | 51 | + generateProtoTasks { | | 52 | + all()*.plugins { | | 53 | + grpc {} | | 54 | + } | | 55 | + } | | 56 | +} |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright 2015, Google Inc. | | 2 | +// All rights reserved. | | 3 | +// | | 4 | +// Redistribution and use in source and binary forms, with or without | | 5 | +// modification, are permitted provided that the following conditions are | | 6 | +// met: | | 7 | +// | | 8 | +// * Redistributions of source code must retain the above copyright | | 9 | +// notice, this list of conditions and the following disclaimer. | | 10 | +// * Redistributions in binary form must reproduce the above | | 11 | +// copyright notice, this list of conditions and the following disclaimer | | 12 | +// in the documentation and/or other materials provided with the | | 13 | +// distribution. | | 14 | +// * Neither the name of Google Inc. nor the names of its | | 15 | +// contributors may be used to endorse or promote products derived from | | 16 | +// this software without specific prior written permission. | | 17 | +// | | 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | | 19 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | | 20 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | | 21 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | | 22 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | | 23 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | | 24 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 25 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 26 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 27 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | | 28 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 29 | + | | 30 | +syntax = "proto3"; | | 31 | + | | 32 | +option java_multiple_files = true; | | 33 | +option java_package = "com.google.endpoints.examples.hello"; | | 34 | +option java_outer_classname = "HelloWorldProto"; | | 35 | + | | 36 | +package helloworld; | | 37 | + | | 38 | +// The greeting service definition. | | 39 | +service Greeter { | | 40 | + // Sends a greeting | | 41 | + rpc SayHello (HelloRequest) returns (HelloReply) {} | | 42 | +} | | 43 | + | | 44 | +// The request message containing the user's name. | | 45 | +message HelloRequest { | | 46 | + string name = 1; | | 47 | +} | | 48 | + | | 49 | +// The response message containing the greetings | | 50 | +message HelloReply { | | 51 | + string message = 1; | | 52 | +} |
Collapse file Copy file name to clipboard +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2017 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 | +# An example API configuration. | | 18 | +# | | 19 | +# Below, replace MY_PROJECT_ID with your Google Cloud Project ID. | | 20 | +# | | 21 | + | | 22 | +# The configuration schema is defined by service.proto file | | 23 | +# https://github.com/googleapis/googleapis/blob/master/google/api/service.proto | | 24 | +type: google.api.Service | | 25 | +config_version: 3 | | 26 | + | | 27 | +# | | 28 | +# Name of the service configuration. | | 29 | +# | | 30 | +name: hellogrpc.endpoints.MY_PROJECT_ID.cloud.goog | | 31 | + | | 32 | +# | | 33 | +# API title to appear in the user interface (Google Cloud Console). | | 34 | +# | | 35 | +title: Hello gRPC API | | 36 | +apis: | | 37 | +- name: helloworld.Greeter |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright 2017 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 | +subprojects { | | 18 | + apply plugin: 'java' | | 19 | + | | 20 | + repositories { | | 21 | + mavenCentral() | | 22 | + } | | 23 | +} |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Copyright 2017 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.hello.HelloWorldClient" | | 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 | +} |
|