【发布时间】:2020-04-02 15:25:34
【问题描述】:
我们在代理后面使用一些旧版本的谷歌云扳手 Java 客户端。我们使用 env GRPC_PROXY_EXP 来设置代理主机和端口。现在我们要迁移到最新版本,并且客户端库不再支持此变量。没有关于什么是新变量的明确文档。请协助 Google Cloud Spanner Java 客户端的 GRPC 代理的新 env 变量是什么
【问题讨论】:
标签: grpc-java google-cloud-spanner
我们在代理后面使用一些旧版本的谷歌云扳手 Java 客户端。我们使用 env GRPC_PROXY_EXP 来设置代理主机和端口。现在我们要迁移到最新版本,并且客户端库不再支持此变量。没有关于什么是新变量的明确文档。请协助 Google Cloud Spanner Java 客户端的 GRPC 代理的新 env 变量是什么
【问题讨论】:
标签: grpc-java google-cloud-spanner
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();
}
【讨论】: