프록시 구성

Google Cloud 클라이언트 라이브러리는 서비스와의 기본 통신에서 HTTPS와 gRPC를 사용합니다. 두 프로토콜 모두에서 https.proxyHost 및 선택적으로 https.proxyPort 속성을 사용하여 프록시를 구성할 수 있습니다.

HTTP로 프록시 구성

HTTP 클라이언트의 경우 http.proxyHost 및 관련 시스템 속성을 사용하여 기본 프록시를 구성할 수 있습니다. 자세한 내용은 Java 네트워킹 및 프록시를 참고하세요.

인증된 프록시와 같은 맞춤 프록시의 경우 HttpTransportFactory에 맞춤 GoogleCredentials를 제공합니다.

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 맞춤 프록시 구성으로 프록시 구성

gRPC를 사용하는 맞춤 프록시의 경우 ProxyDetectorManagedChannelBuilder에 제공합니다.

PROXY_USERNAME, PROXY_PASSWORD, PROXY_HOST, PROXY_PORT를 프록시의 사용자 인증 정보와 주소로 바꿉니다.

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);
}