|
| 1 | +# gRPC-GCP for Java |
| 2 | + |
| 3 | +grpc-gcp provides gRPC support for Google Cloud Clients. |
| 4 | + |
| 5 | +This module is currently used by the Google Cloud Spanner client library to provide |
| 6 | +gRPC-GCP channel pooling and related transport behavior. It is published separately |
| 7 | +as `com.google.cloud:grpc-gcp` because Spanner exposes grpc-gcp types in its public |
| 8 | +API. |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +Use Spanner API as an example. |
| 13 | + |
| 14 | +First, Create a json file defining API configuration, with ChannelPoolConfig and MethodConfig. |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "channelPool": { |
| 19 | + "maxSize": 3, |
| 20 | + "maxConcurrentStreamsLowWatermark": 0 |
| 21 | + }, |
| 22 | + "method": [ |
| 23 | + { |
| 24 | + "name": [ "google.spanner.v1.Spanner/CreateSession" ], |
| 25 | + "affinity": { |
| 26 | + "command": "BIND", |
| 27 | + "affinityKey": "name" |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": [ "google.spanner.v1.Spanner/GetSession" ], |
| 32 | + "affinity": { |
| 33 | + "command": "BOUND", |
| 34 | + "affinityKey": "name" |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + "name": [ "google.spanner.v1.Spanner/DeleteSession" ], |
| 39 | + "affinity": { |
| 40 | + "command": "UNBIND", |
| 41 | + "affinityKey": "name" |
| 42 | + } |
| 43 | + } |
| 44 | + ] |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Initialize `GcpManagedChannel` based on this API config file. |
| 49 | + |
| 50 | +```java |
| 51 | +String API_CONFIG_FILE = "api_config_file.json" |
| 52 | +String SPANNER_TARGET = "spanner.googleapis.com"; |
| 53 | +... |
| 54 | + |
| 55 | +ManagedChannelBuilder delegateChannelBuilder = ManagedChannelBuilder.forAddress(SPANNER_TARGET, 443); |
| 56 | +GcpManagedChannelBuilder gcpBuilder = |
| 57 | + GcpManagedChannelBuilder.forDelegateBuilder(delegateChannelBuilder) |
| 58 | + .withApiConfigJsonFile(jsonApiConfig); |
| 59 | +ManagedChannel gcpChannel = gcpBuilder.build(); |
| 60 | +``` |
| 61 | + |
| 62 | +Create Cloud API stub using `GcpManagedChannel`. |
| 63 | + |
| 64 | +```java |
| 65 | +GoogleCredentials creds = getCreds(); |
| 66 | +SpannerBlockingStub stub = |
| 67 | + SpannerGrpc.newBlockingStub(gcpChannel) |
| 68 | + .withCallCredentials(MoreCallCredentials.from(creds)); |
| 69 | +``` |
| 70 | + |
| 71 | +## Build from source |
| 72 | + |
| 73 | +Download source. |
| 74 | + |
| 75 | +```sh |
| 76 | +git clone https://github.com/googleapis/google-cloud-java.git && cd google-cloud-java/grpc-gcp-java |
| 77 | +``` |
| 78 | + |
| 79 | +Build project with unit tests. |
| 80 | + |
| 81 | +```sh |
| 82 | +mvn verify |
| 83 | +``` |
| 84 | + |
| 85 | +Install the jar to your local Maven repository. |
| 86 | + |
| 87 | +```sh |
| 88 | +mvn install |
| 89 | +``` |
| 90 | + |
| 91 | +## Test |
| 92 | + |
| 93 | +Setup credentials. See [Getting Started With Authentication](https://cloud.google.com/docs/authentication/getting-started) for more details. |
| 94 | + |
| 95 | +```sh |
| 96 | +export GOOGLE_APPLICATION_CREDENTIALS=path/to/key.json |
| 97 | +``` |
| 98 | + |
| 99 | +```sh |
| 100 | +export GCP_PROJECT_ID=project_id |
| 101 | +``` |
| 102 | + |
| 103 | +Run unit tests. |
| 104 | + |
| 105 | +```sh |
| 106 | +mvn test |
| 107 | +``` |
| 108 | + |
| 109 | +Integration tests are kept as source references but are not wired into the default monorepo Maven lifecycle. |
| 110 | + |
| 111 | +## Publish |
| 112 | + |
| 113 | +This artifact is released by the `google-cloud-java` monorepo release pipeline. |
| 114 | + |
| 115 | +## Code Format |
| 116 | + |
| 117 | +Run google-java-format |
| 118 | + |
| 119 | +```sh |
| 120 | +mvn fmt:format |
| 121 | +``` |
0 commit comments