Google cloud spanner java 客户端使用代理
Google cloud spanner java client using proxy
我们在代理后面使用一些旧版本的 google cloud spanner Java 客户端。我们使用 env GRPC_PROXY_EXP
来设置代理主机和端口。现在我们要迁移到最新版本,客户端库不再支持此变量。没有关于什么是新变量的明确文档。请协助 Google cloud spanner Java 客户端
的 GRPC 代理的新环境变量是什么
Java 系统属性 https.proxyHost
和 https.proxyPort
应该适用于 Spanner 客户端。如果代理也需要身份验证,那么你可以添加一个默认的身份验证方法来使用。
您能否尝试使用以下代码示例(如果您的代理不需要验证部分,可以选择删除它)?
// 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();
}
我们在代理后面使用一些旧版本的 google cloud spanner Java 客户端。我们使用 env GRPC_PROXY_EXP
来设置代理主机和端口。现在我们要迁移到最新版本,客户端库不再支持此变量。没有关于什么是新变量的明确文档。请协助 Google cloud spanner Java 客户端
Java 系统属性 https.proxyHost
和 https.proxyPort
应该适用于 Spanner 客户端。如果代理也需要身份验证,那么你可以添加一个默认的身份验证方法来使用。
您能否尝试使用以下代码示例(如果您的代理不需要验证部分,可以选择删除它)?
// 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();
}