設定 Proxy

Google Cloud 用戶端程式庫會在與服務的底層通訊中使用 HTTPS 和 gRPC。在這兩種通訊協定中,您可以使用 https.proxyHost 和 (選用) https.proxyPort 屬性設定 Proxy。

設定 HTTP Proxy

如果是 HTTP 用戶端,您可以透過 http.proxyHost 和相關系統屬性設定基本 Proxy,如「Java Networking and Proxies」一文所述。

如為自訂 Proxy (例如已驗證的 Proxy),請提供自訂 HttpTransportFactoryGoogleCredentials

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;


import java.io.IOException;


public class ProxyExample {
 public GoogleCredentials getCredentials() throws IOException {
   HttpTransportFactory httpTransportFactory = getHttpTransportFactory(
       "some-host", 8080, "some-username", "some-password"
   );


   return GoogleCredentials.getApplicationDefault(httpTransportFactory);
 }


 public HttpTransportFactory getHttpTransportFactory(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) {
   HttpHost proxyHostDetails = new HttpHost(proxyHost, proxyPort);
   HttpRoutePlanner httpRoutePlanner = new DefaultProxyRoutePlanner(proxyHostDetails);


   CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
   credentialsProvider.setCredentials(
       new AuthScope(proxyHostDetails.getHostName(), proxyHostDetails.getPort()),
       new UsernamePasswordCredentials(proxyUsername, proxyPassword)
   );


   HttpClient httpClient = ApacheHttpTransport.newDefaultHttpClientBuilder()
       .setRoutePlanner(httpRoutePlanner)
       .setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
       .setDefaultCredentialsProvider(credentialsProvider)
       .build();


   final HttpTransport httpTransport = new ApacheHttpTransport(httpClient);
   return new HttpTransportFactory() {
     @Override
     public HttpTransport create() {
       return httpTransport;
     }
   };
 }
}

上述範例需要執行 com.google.http-client:google-http-client-apache-v2

使用 gRPC 自訂 Proxy 設定設定 Proxy

如要使用 gRPC 搭配自訂 Proxy,請將 ProxyDetector 提供給 ManagedChannelBuilder

PROXY_USERNAMEPROXY_PASSWORDPROXY_HOSTPROXY_PORT 替換為您的 Proxy 憑證和地址。

import com.google.api.core.ApiFunction;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.CloudTasksSettings;
import com.google.cloud.tasks.v2.stub.CloudTasksStubSettings;
import io.grpc.HttpConnectProxiedSocketAddress;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ProxiedSocketAddress;
import io.grpc.ProxyDetector;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

public CloudTasksClient getService() throws IOException {
  TransportChannelProvider transportChannelProvider =
      CloudTasksStubSettings.defaultGrpcTransportProviderBuilder()
          .setChannelConfigurator(
              new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
                @Override
                public ManagedChannelBuilder apply(ManagedChannelBuilder managedChannelBuilder) {
                  return managedChannelBuilder.proxyDetector(
                      new ProxyDetector() {
                        @Nullable
                        @Override
                        public ProxiedSocketAddress proxyFor(SocketAddress socketAddress)
                            throws IOException {
                          return HttpConnectProxiedSocketAddress.newBuilder()
                              .setUsername(PROXY_USERNAME)
                              .setPassword(PROXY_PASSWORD)
                              .setProxyAddress(new InetSocketAddress(PROXY_HOST, PROXY_PORT))
                              .setTargetAddress((InetSocketAddress) socketAddress)
                              .build();
                        }
                      });
                }
              })
          .build();
  CloudTasksSettings cloudTasksSettings =
      CloudTasksSettings.newBuilder()
          .setTransportChannelProvider(transportChannelProvider)
          .build();
  return CloudTasksClient.create(cloudTasksSettings);
}