Google Cloud Endpoints Sample for Go using gRPC
This sample demonstrates how to use Google Cloud Endpoints using Go and gRPC.
For a complete walkthrough, see the following guides:
Test the code locally (optional)
Run the backend using go run
:
$ go run server/main.go
Send a request from another terminal:
$ go run client/main.go
2017/03/30 17:08:32 Greeting: Hello world
Deploying service config
-
First, generate out.pb
from the proto file:
protoc --include_imports --include_source_info helloworld/helloworld.proto --descriptor_set_out out.pb
-
Edit api_config.yaml
. Replace YOUR_PROJECT_ID
:
name: hellogrpc.endpoints.YOUR_PROJECT_ID.cloud.goog
-
Deploy your service:
gcloud service-management deploy out.pb api_config.yaml
Your config ID should be printed out, it looks like 2017-03-30r0
.
Take a note of it, you'll need it later.
You can list the config IDs using this command:
gcloud service-management configs list --service hellogrpc.endpoints.YOUR_PROJECT_ID.cloud.goog
Building the server's Docker container
Build and tag your gRPC server, storing it in your private container registry:
gcloud container builds submit --tag gcr.io/YOUR_PROJECT_ID/go-grpc-hello:1.0 .
Deploy to GCE or GKE
Deploy to GCE
-
Create an instance and SSH into it:
gcloud compute instances create grpc-host --image-family gci-stable --image-project google-containers --tags=http-server
gcloud compute ssh grpc-host
-
Set some environment variables (you'll need to manually set the service config ID):
GOOGLE_CLOUD_PROJECT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
SERVICE_NAME=hellogrpc.endpoints.${GOOGLE_CLOUD_PROJECT}.cloud.goog
SERVICE_CONFIG_ID=<Your Config ID>
-
Pull your credentials to access your private container registry:
/usr/share/google/dockercfg_update.sh
-
Run your gRPC server's container:
docker run -d --name=grpc-hello gcr.io/${GOOGLE_CLOUD_PROJECT}/go-grpc-hello:1.0
-
Run 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
-
Get the IP address of your secured gRPC server:
gcloud compute instances list --filter=grpc-host
-
Send a request to the API server (see "Running the client" below)
Deploy to GKE
If you haven't got a cluster, first create one.
-
Edit container-engine.yaml
. Replace <YOUR_PROJECT_ID>
and <SERVICE_CONFIG_ID>
.
-
Create the deployment and service:
kubectl apply -f container-engine.yaml
-
Wait until the load balancer is active:
kubectl get svc grpc-hello --watch
-
Send a request to the API server (see "Running the client" below)
Running the client
-
First, create a project API key.
-
Then, after you have your server's IP address (via GKE's kubectl get svc
or your GCE instance's IP), run:
go run client/main.go -api-key=AIza.... -addr=YOUR_SERVER_IP_ADDRESS:80 [message]