文档

Endpoint配置

更新时间:
一键部署
重要

本文中含有需要您注意的重要提示信息,忽略该信息可能对您的业务造成影响,请务必仔细阅读。

本文为您介绍V1.0 SDK如何设置Endpoint。

Endpoint是请求接口服务的网络域名,如ecs.cn-hangzhou.aliyuncs.com,V1.0 SDK提供了四种Endpoint的寻址方式。下面按优先级排列的方式,为您详细介绍寻址流程。

Endpoint寻址流程

  1. 用户自定义Endpoint

    它是优先级最高的寻址逻辑,用户可以直接指定endpoint的具体内容。

    1. 只对当前Request请求生效,优先级最高。

      import com.aliyuncs.DefaultAcsClient;
      import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
      import com.aliyuncs.ecs.model.v20140526.DescribeRegionsRequest;
      import com.aliyuncs.exceptions.ClientException;
      import com.aliyuncs.http.HttpResponse;
      import com.aliyuncs.profile.DefaultProfile;
      
      public class Sample {
          public static void main(String[] args) throws ClientException {
              // 从环境变量中获取阿里云访问凭证,确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
              EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
              DefaultProfile profile = DefaultProfile.getProfile();
              // 通过 SDK Core 初始化 client
              DefaultAcsClient client = new DefaultAcsClient(profile, credentialsProvider);
      
              DescribeRegionsRequest request = new DescribeRegionsRequest();
              // 只对当前 Request 生效
              request.setSysEndpoint("ecs.cn-shanghai.aliyuncs.com");
      
              HttpResponse httpResponse = client.doAction(request);
              System.out.println(httpResponse.getHttpContentString());
          }
      }
    2. 全局生效,支持添加多个endpoint。

      import com.aliyuncs.DefaultAcsClient;
      import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
      import com.aliyuncs.ecs.model.v20140526.DescribeRegionsRequest;
      import com.aliyuncs.exceptions.ClientException;
      import com.aliyuncs.http.HttpResponse;
      import com.aliyuncs.profile.DefaultProfile;
      
      public class Sample {
          public static void main(String[] args) throws ClientException {
              // 从环境变量中获取阿里云访问凭证,确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
              EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
              String regionId = "cn-hangzhou";
              DefaultProfile profile = DefaultProfile.getProfile(regionId);
              // 全局生效
              DefaultProfile.addEndpoint("cn-hangzhou", "ecs", "ecs.cn-hangzhou.aliyuncs.com");
              DefaultProfile.addEndpoint("cn-shanghai", "ecs", "ecs.cn-shanghai.aliyuncs.com");
              // 通过 SDK Core 初始化 client
              DefaultAcsClient client = new DefaultAcsClient(profile, credentialsProvider);
      
              DescribeRegionsRequest request = new DescribeRegionsRequest();
              HttpResponse httpResponse = client.doAction(request);
              System.out.println(httpResponse.getHttpContentString());
          }
      }
  2. 云产品SDK寻址

    若用户未设置自定义Endpoint,会校验云产品SDK中是否存在Endpoint文件,Endpoint文件示例Ecs Endpoint Data File。如果存在,寻址逻辑如下:

    1. 若productNetwork(云产品网络)的值是public,且Endpoint文件中包含传入的regionId时,则会根据regionId在云产品SDK的Endpoint中自动寻址。

    2. 若productNetwork的值非public,或者Endpoint文件不存在传入的regionId时,则会根据规则自动拼接endpoint。

    如果不存在,则进行Core SDK寻址

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
    import com.aliyuncs.ecs.model.v20140526.DescribeRegionsRequest;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    
    public class Sample {
    
        public static void main(String[] args) throws ClientException {
            // 从环境变量中获取阿里云访问凭证,确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
            // 设置阿里云区域ID,此处以“cn-shanghai”为例。
            String regionId = "cn-shanghai";
            // 初始化阿里云配置profile,用于创建客户端。
            DefaultProfile profile = DefaultProfile.getProfile(regionId);
            // 使用profile和访问凭证创建DefaultAcsClient实例,用于发送请求。
            DefaultAcsClient client = new DefaultAcsClient(profile, credentialsProvider);
            // 请求参数
            DescribeRegionsRequest request = new DescribeRegionsRequest();
    
            // 产品网络访问方式,productNetwork的默认值为 public(公网请求)。
            // 还可设置的值share(跨域请求)、ipv6(Ipv6 请求)、proxy(代理请求)、inner(内部请求)、dualstack(Ipv4/Ipv6 双协议栈)、vpc(vpc网络)
            request.productNetwork = "public";
    //        // 针对于vpc网络,还可以通过启用enableUsingVpcEndpoint配置
    //        profile.enableUsingVpcEndpoint();
    
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getHttpContentString());
        }
    }
    
  3. Core SDK寻址

    若云产品SDK中不存在Endpoint文件时,根据regionId在SDK Core中的endpoints.json数据文件进行寻址。Core SDK寻址逻辑:优先从数据文件中云产品的Endpoint中寻址,如果没有匹配到符合的endpoint,则会继续根据云产品的productCode在global endpoints中寻址。

  4. 调用Location服务寻址

    若通过Core SDK还未寻址成功,则会通过云产品的serviceCode和regionId调用Location服务接口获取endpoint。

    serviceCode:已在您要调用的接口的Request对象中默认设置,不需要手动设置。

    regionId:初始化客户端时传入的regionId。

警告

不是所有的云产品都支持该寻址模式,建议使用自定义的方式。