代理配置

更新时间:

本文为您介绍V2.0 Java SDK对于代理的配置方式。

代理配置方式

说明

代理配置优先级:RuntimeOption配置>Client配置,优先级依次降低。

  • 支持通过运行时参数(RuntimeOptions)配置当前请求的代理。

    import com.aliyun.ecs20140526.Client;
    import com.aliyun.ecs20140526.models.DescribeRegionsRequest;
    import com.aliyun.teaopenapi.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class Sample {
        public static void main(String[] args) throws Exception {
            Config config = new Config();
            // 从环境变量获取RAM用户的AccessKey ID
            config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
            // 从环境变量获取RAM用户的AccessKey Secret
            config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            // 地域ID
            config.setRegionId("<regionId>");
            Client client = new Client(config);
    
            // 运行时参数设置,仅对使用了该运行时参数实例的请求有效
            RuntimeOptions runtimeOptions = new RuntimeOptions();
            runtimeOptions.httpProxy = "http://127.0.0.1:9898";
            runtimeOptions.httpsProxy = "http://user:password@127.0.0.1:8989";
            runtimeOptions.noProxy = "127.0.0.1,localhost";
    
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
    
            client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions);
        }
    }
  • 支持通过Config在初始化Client阶段设置请求的代理。

    import com.aliyun.ecs20140526.Client;
    import com.aliyun.ecs20140526.models.DescribeRegionsRequest;
    import com.aliyun.teaopenapi.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class Sample {
        public static void main(String[] args) throws Exception {
            Config config = new Config();
            // 从环境变量获取RAM用户的AccessKey ID
            config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
            // 从环境变量获取RAM用户的AccessKey Secret
            config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            // 地域ID
            config.setRegionId("<regionId>");
            // Client代理参数设置
            config.setHttpProxy("http://127.0.0.1:9898");
            config.setHttpsProxy("http://user:password@127.0.0.1:8989");
            config.setNoProxy("127.0.0.1,localhost");
            Client client = new Client(config);
    
            RuntimeOptions runtimeOptions = new RuntimeOptions();
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
    
            client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions);
        }
    }