|
| 1 | +# Endpoints Getting Started with gRPC & Java Quickstart |
| 2 | + |
| 3 | +It is assumed that you have a |
| 4 | +working |
| 5 | +[gRPC](http://www.grpc.io/docs/), |
| 6 | +[ProtoBuf](https://github.com/google/protobuf#protocol-compiler-installation) and |
| 7 | +Java environment, a Google Cloud account |
| 8 | +and [SDK](https://cloud.google.com/sdk/) configured. |
| 9 | + |
| 10 | +1. Build the code: |
| 11 | + |
| 12 | + ```bash |
| 13 | + ./gradlew build |
| 14 | + ``` |
| 15 | + |
| 16 | +1. Test running the code, optional: |
| 17 | + |
| 18 | + ```bash |
| 19 | + # In the background or another terminal run the server: |
| 20 | + java -jar server/build/libs/server.jar |
| 21 | +
|
| 22 | + # Check the client parameters: |
| 23 | + java -jar client/build/libs/client.jar --help |
| 24 | +
|
| 25 | + # Run the client |
| 26 | + java -jar client/build/libs/client.jar --greetee 'Endpoints!' |
| 27 | + ``` |
| 28 | + |
| 29 | +1. Generate the `out.pb` from the proto file. |
| 30 | + |
| 31 | + ```bash |
| 32 | + protoc --include_imports --include_source_info api/src/main/proto/helloworld.proto --descriptor_set_out out.pb |
| 33 | + ``` |
| 34 | + |
| 35 | +1. Edit, `api_config.yaml`. Replace `MY_PROJECT_ID` with your project id. |
| 36 | + |
| 37 | +1. Deploy your service config to Service Management: |
| 38 | + |
| 39 | + ```bash |
| 40 | + gcloud service-management deploy out.pb api_config.yaml |
| 41 | + # The Config ID should be printed out, looks like: 2017-02-01r0, remember this |
| 42 | +
|
| 43 | + # set your project to make commands easier |
| 44 | + GCLOUD_PROJECT=<Your Project ID> |
| 45 | +
|
| 46 | + # Print out your Config ID again, in case you missed it |
| 47 | + gcloud service-management configs list --service hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog |
| 48 | + ``` |
| 49 | + |
| 50 | +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) |
| 51 | +
|
| 52 | +1. Build a docker image for your gRPC server, store in your Registry |
| 53 | +
|
| 54 | + ```bash |
| 55 | + gcloud container builds submit --tag gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0 . |
| 56 | + ``` |
| 57 | +
|
| 58 | +1. Either deploy to GCE (below) or GKE (further down) |
| 59 | +
|
| 60 | +### GCE |
| 61 | +
|
| 62 | +1. Create your instance and ssh in. |
| 63 | +
|
| 64 | + ```bash |
| 65 | + gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server |
| 66 | + gcloud compute ssh grpc-host |
| 67 | + ``` |
| 68 | +
|
| 69 | +1. Set some variables to make commands easier |
| 70 | +
|
| 71 | + ```bash |
| 72 | + GCLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") |
| 73 | + SERVICE_NAME=hellogrpc.endpoints.${GCLOUD_PROJECT}.cloud.goog |
| 74 | + SERVICE_CONFIG_ID=<Your Config ID> |
| 75 | + ``` |
| 76 | +
|
| 77 | +1. Pull your credentials to access Container Registry, and run your gRPC server container |
| 78 | +
|
| 79 | + ```bash |
| 80 | + /usr/share/google/dockercfg_update.sh |
| 81 | + docker run -d --name=grpc-hello gcr.io/${GCLOUD_PROJECT}/java-grpc-hello:1.0 |
| 82 | + ``` |
| 83 | +
|
| 84 | +1. Run the Endpoints proxy |
| 85 | +
|
| 86 | + ```bash |
| 87 | + docker run --detach --name=esp \ |
| 88 | + -p 80:9000 \ |
| 89 | + --link=grpc-hello:grpc-hello \ |
| 90 | + gcr.io/endpoints-release/endpoints-runtime:1 \ |
| 91 | + -s ${SERVICE_NAME} \ |
| 92 | + -v ${SERVICE_CONFIG_ID} \ |
| 93 | + -P 9000 \ |
| 94 | + -a grpc://grpc-hello:50051 |
| 95 | + ``` |
| 96 | +
|
| 97 | +1. Back on your local machine, get the external IP of your GCE instance. |
| 98 | +
|
| 99 | + ```bash |
| 100 | + gcloud compute instances list |
| 101 | + ``` |
| 102 | +
|
| 103 | +1. Run the client |
| 104 | +
|
| 105 | + ```bash |
| 106 | + java -jar client/build/libs/client.jar --host <IP of GCE Instance>:80 --api_key <API Key from Console> |
| 107 | + ``` |
| 108 | +
|
| 109 | +### GKE |
| 110 | +
|
| 111 | +1. Create a cluster |
| 112 | +
|
| 113 | + ```bash |
| 114 | + gcloud container clusters create my-cluster |
| 115 | + ``` |
| 116 | +
|
| 117 | +1. Edit `container-engine.yaml`. Replace `SERVICE_NAME`, `SERVICE_CONFIG_ID`, and `GCLOUD_PROJECT` with your values. |
| 118 | +
|
| 119 | +1. Deploy to GKE |
| 120 | +
|
| 121 | + ```bash |
| 122 | + kubectl create -f ./container-engine.yaml |
| 123 | + ``` |
| 124 | +
|
| 125 | +1. Get IP of load balancer, run until you see an External IP. |
| 126 | +
|
| 127 | + ```bash |
| 128 | + kubectl get svc grpc-hello |
| 129 | + ``` |
| 130 | +
|
| 131 | +1. Run the client |
| 132 | +
|
| 133 | + ```bash |
| 134 | + java -jar client/build/libs/client.jar --host <IP of GKE LoadBalancer>:80 --api_key <API Key from Console> |
| 135 | + ``` |
0 commit comments