grpc-gcp provides gRPC support for Google Cloud Clients.
Use Spanner API as an example.
First, Create a json file defining API configuration, with ChannelPoolConfig and MethodConfig.
{
"channelPool": {
"maxSize": 3,
"maxConcurrentStreamsLowWatermark": 2
},
"method": [
{
"name": [ "google.spanner.v1.Spanner/CreateSession" ],
"affinity": {
"command": "BIND",
"affinityKey": "name"
}
},
{
"name": [ "google.spanner.v1.Spanner/GetSession" ],
"affinity": {
"command": "BOUND",
"affinityKey": "name"
}
},
{
"name": [ "google.spanner.v1.Spanner/DeleteSession" ],
"affinity": {
"command": "UNBIND",
"affinityKey": "name"
}
}
]
}Initialize GcpManagedChannel based on this API config file.
String API_CONFIG_FILE = "api_config_file.json"
String SPANNER_TARGET = "spanner.googleapis.com";
ManagedChannelBuilder delegateChannelBuilder = ManagedChannelBuilder.forAddress(SPANNER_TARGET, 443);
GcpManagedChannel gcpChannel = new GcpManagedChannel(delegateChannelBuilder, API_CONFIG_FILE);Create Cloud API stub using GcpManagedChannel.
GoogleCredentials creds = getCreds();
SpannerBlockingStub stub =
SpannerGrpc.newBlockingStub(gcpChannel)
.withCallCredentials(MoreCallCredentials.from(creds));Download source.
git clone https://github.com/GoogleCloudPlatform/grpc-gcp-java.git && cd grpc-gcp-java/grpc-gcpBuild project without tests.
./gradlew build -x testSetup credentials. See Getting Started With Authentication for more details.
export GOOGLE_APPLICATION_CREDENTIALS=path/to/key.jsonexport GCP_PROJECT_ID=project_idRun unit tests and integration tests:
./gradlew testRun Bigtable load test:
./gradlew bigtableLoadTestRun Spanner load test:
./gradlew spannerLoadTestRun Spanner benchmark tests:
./gradlew spannerBenchmark --args="--gcp=true --thread=3 --rpc=100"Run google-java-format
./gradlew goJF