Naresh Nagarajan
Naresh Nagarajan

Reputation: 95

Google cloud spanner java client using proxy

we are using some older version of google cloud spanner Java client behind proxy. We used env GRPC_PROXY_EXP to set proxy host and port. Now we want to migrate to latest version and this variable no more honored by the client lib. There is no clear documentation on what is new variable. Please assist what is the new env variable for GRPC proxy for Google cloud spanner Java client

Upvotes: 0

Views: 668

Answers (1)

Knut Olav Løite
Knut Olav Løite

Reputation: 3512

The Java system properties https.proxyHost and https.proxyPort should work with the Spanner client. If the proxy also requires authentication, then you can add a default authentication method to use.

Could you try with the following code sample (optionally removing the authentication part if you don't need that for your proxy)?

    // Set proxy host and port. This will instruct both the HTTP and gRPC clients to go through the proxy.
    System.setProperty("https.proxyHost", "127.0.0.1");
    System.setProperty("https.proxyPort", "3128");

    // The following is OPTIONAL, depending on whether your proxy requires authentication.
    // Allow all AUTH schemes. Needed if you are using basic AUTH.
    System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
    // Set the default authentication to use for the proxy.
    java.net.Authenticator.setDefault(
        new Authenticator() {
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("myuser1", "test".toCharArray());
          }
        });
    // Setup Spanner in the normal way.
    GoogleCredentials credentials =
        GoogleCredentials.fromStream(
            new FileInputStream("/path/to/key.json"));
    Spanner spanner =
        SpannerOptions.newBuilder()
            .setProjectId("project-id")
            .setCredentials(credentials)
            .build()
            .getService();
    DatabaseClient client =
        spanner.getDatabaseClient(
            DatabaseId.of("project-id", "test-instance", "testdb"));
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
      assertThat(rs.next()).isTrue();
      assertThat(rs.getLong(0)).isEqualTo(1L);
      assertThat(rs.next()).isFalse();
    }

Upvotes: 2

Related Questions