Skip to content

Latest commit

 

History

History
 
 

grpc_spanner_example

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

gRPC Example

This example creates a gRCP server that connect to spanner to find the name of user for a given user id.

Application Project Structure

   .
   └── grpc_example
       └── src
           └── main
               ├── java
                       └── com.example.grpc
                           ├── client
                               └── ConnectClient # Example Client
                           ├── server
                               └── ConnectServer # Initializes gRPC Server
                           └── service
                               └── ConnectService # Implementation of rpc services in the proto
               └── proto
                   └── connect_service.proto # Proto definition of the server
      ├── pom.xml
      └── README.md

Technology Stack

  1. gRPC
  2. Spanner

Setup Instructions

Project Setup

Creating a Project in the Google Cloud Platform Console

If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.

  1. Open the Cloud Platform Console.
  2. In the drop-down menu at the top, select Create a project.
  3. Give your project a name = my-dfdl-project
  4. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.

Enabling billing for your project.

If you haven't already enabled billing for your project, enable billing now. Enabling billing allows is required to use Cloud Bigtable and to create VM instances.

Install the Google Cloud SDK.

If you haven't already installed the Google Cloud SDK, install the Google Cloud SDK now. The SDK contains tools and libraries that enable you to create and manage resources on Google Cloud Platform.

Setting Google Application Default Credentials

Set your Google Application Default Credentials by initializing the Google Cloud SDK with the command:

gcloud init

Generate a credentials file by running the application-default login command:

gcloud auth application-default login

Spanner Setup from the Console

  1. Create an instance called grpc-example
  2. Create a database called grpc_example_db
  3. Create a table named Users using the following Database Definition Language ( DDL) statement for the database
  CREATE TABLE Users (
  user_id INT64 NOT NULL,
  name STRING(MAX),
) PRIMARY KEY(user_id);
  1. Insert the following record into the table Users
INSERT INTO
  Users (user_id,
    name)
VALUES
  (1234, -- type: INT64
    "MyName" -- type: STRING(MAX)
    );

Usage

Initialize the server

$ mvn -DskipTests package exec:java 
    -Dexec.mainClass=com.example.grpc.server.ConnectServer

Run the Client

$ mvn -DskipTests package exec:java 
      -Dexec.mainClass=com.example.grpc.client.ConnectClient