|
| 1 | +# Client Library Endpoint |
| 2 | +Google Client libraries will automatically resolve an endpoint to connect to Google Cloud services. |
| 3 | +The default resolved endpoint will be to Google servers (i.e. `https://speech.googleapis.com:443`). |
| 4 | + |
| 5 | +## Anatomy of an Endpoint |
| 6 | +Using the default java-speech endpoint as an example: `https://speech.googleapis.com:443`: |
| 7 | + |
| 8 | +| Scheme | Service Name | Universe Domain | Port | |
| 9 | +|---------- |-------------- |----------------- |------ | |
| 10 | +| https:// | speech | googleapis.com | 443 | |
| 11 | + |
| 12 | +Default values for Java SDK Client libraries: |
| 13 | +- Scheme: https:// |
| 14 | +- [Universe Domain](universe_domain.md): googleapis.com |
| 15 | +- Port: 443 |
| 16 | + |
| 17 | +Note: Service Name does not have a default value and is different for each cloud service. |
| 18 | + |
| 19 | +## Configuring a Specific Endpoint |
| 20 | +There are two ways to configure the endpoint in Java Cloud Client Libraries: ServiceSettings and |
| 21 | +TransportChannelProvider. |
| 22 | + |
| 23 | +### (Recommended) Set through the generated {Service}Settings |
| 24 | +The following example is using Java-KMS v2.42.0 as an example: |
| 25 | + |
| 26 | +1. Set the endpoint in {Service}Settings.Builder and create the Settings object |
| 27 | +```java |
| 28 | +String endpoint = "settingsEndpoint.com"; |
| 29 | +KeyManagementServiceSettings keyManagementServiceSettings = |
| 30 | + KeyManagementServiceSettings.newBuilder() |
| 31 | + .setEndpoint(endpoint) |
| 32 | + .build(); |
| 33 | +``` |
| 34 | +2. Create the client with the Settings |
| 35 | +```java |
| 36 | +try (KeyManagementServiceClient keyManagementServiceClient = |
| 37 | + KeyManagementServiceClient.create(keyManagementServiceSettings)) { |
| 38 | + ... |
| 39 | +} |
| 40 | +``` |
| 41 | +The endpoint will be resolved to `settingsEndpoint.com`. |
| 42 | + |
| 43 | +### Set through the Instantiating{Transport}ChannelProvider |
| 44 | +The following example is using Java-KMS v2.42.0 as an example: |
| 45 | + |
| 46 | +1. Create the transport specific TransportChannelProvider |
| 47 | +```java |
| 48 | +String endpoint = "transportEndpoint.com"; |
| 49 | +InstantiatingGrpcChannelProvider instantiatingGrpcChannelProvider = |
| 50 | + InstantiatingGrpcChannelProvider.newBuilder() |
| 51 | + .setEndpoint(endpoint) |
| 52 | + // ... Other required configurations |
| 53 | + .build(); |
| 54 | +``` |
| 55 | +2. Pass the TransportChannelProvider to the Settings |
| 56 | +```java |
| 57 | +KeyManagementServiceSettings keyManagementServiceSettings = |
| 58 | + KeyManagementServiceSettings.newBuilder() |
| 59 | + .setTransportChannelProvider(instantiatingGrpcChannelProvider) |
| 60 | + .build(); |
| 61 | +``` |
| 62 | +3. Create the client with the Settings |
| 63 | +```java |
| 64 | +try (KeyManagementServiceClient keyManagementServiceClient = |
| 65 | + KeyManagementServiceClient.create(keyManagementServiceSettings)) { |
| 66 | + ... |
| 67 | +} |
| 68 | +``` |
| 69 | +The endpoint will be resolved to `transportEndpoint.com`. |
| 70 | + |
| 71 | +### Set through ServiceSettings and TransportChannelProvider |
| 72 | +If you are setting an endpoint via both methods above, like: |
| 73 | +```java |
| 74 | +String endpoint1 = "transportEndpoint.com"; |
| 75 | + InstantiatingGrpcChannelProvider instantiatingGrpcChannelProvider = |
| 76 | + InstantiatingGrpcChannelProvider.newBuilder() |
| 77 | + .setEndpoint(endpoint1) |
| 78 | + // ... Other required configurations |
| 79 | + .build(); |
| 80 | + |
| 81 | +String endpoint2 = "settingsEndpoint.com"; |
| 82 | +KeyManagementServiceSettings keyManagementServiceSettings = |
| 83 | + KeyManagementServiceSettings.newBuilder() |
| 84 | + .setEndpoint(endpoint2) |
| 85 | + .setTransportChannelProvider(instantiatingGrpcChannelProvider) |
| 86 | + .build(); |
| 87 | +``` |
| 88 | +The endpoint will be resolved to `transportEndpoint.com`. |
| 89 | + |
| 90 | +### Endpoint Hierarchy |
| 91 | +1. If set in the TransportChannelProvider, use this value. Otherwise, go to the next step. |
| 92 | +2. If set via the ClientSettings, use this value. Otherwise, go to the next step. |
| 93 | +3. Use the default endpoint (i.e. `https://{serviceName}.googleapis.com:443`) |
| 94 | + |
| 95 | +#### How can I check which endpoint the client library is using |
| 96 | +Assuming you have configured a custom endpoint, like: |
| 97 | +```java |
| 98 | +String endpoint = "..."; |
| 99 | +KeyManagementServiceSettings keyManagementServiceSettings = |
| 100 | + KeyManagementServiceSettings.newBuilder() |
| 101 | + .setEndpoint(endpoint) |
| 102 | + .build(); |
| 103 | +try (KeyManagementServiceClient keyManagementServiceClient = |
| 104 | + KeyManagementServiceClient.create(keyManagementServiceSettings)) { |
| 105 | + ... |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +Retrieve the resolved endpoint from the Setting's `getEndpoint()` method |
| 110 | +```java |
| 111 | +keyManagementServiceClient.getSettings().getEndpoint(); |
| 112 | +``` |
| 113 | + |
| 114 | +## When should I specify my custom endpoint |
| 115 | +There are a few use cases: |
| 116 | +1. Service offers [regional endpoints](https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library) and you do not want the default endpoint. |
| 117 | +2. Service can't use https (i.e. local testing to hit localhost) |
0 commit comments