This example creates a gRCP server that connect to spanner to find the name of user for a given user id.
.
└── 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
- gRPC
- Spanner
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.
- Open the Cloud Platform Console.
- In the drop-down menu at the top, select Create a project.
- Give your project a name = my-dfdl-project
- 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.
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.
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.
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
- Create an instance called grpc-example
- Create a database called grpc_example_db
- 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);
- Insert the following record into the table Users
INSERT INTO
Users (user_id,
name)
VALUES
(1234, -- type: INT64
"MyName" -- type: STRING(MAX)
);
$ mvn -DskipTests package exec:java
-Dexec.mainClass=com.example.grpc.server.ConnectServer
$ mvn -DskipTests package exec:java
-Dexec.mainClass=com.example.grpc.client.ConnectClient