文档简介:
AsGroupClient是Auto Scaling Group服务的客户端,为开发者与AsGroup服务进行交互提供了一系列的方法。
新建AsGroupClient
通过AK/SK方式访问AsGroup
用户可以参考如下代码新建一个AsGroupClient:
public class Sample { public static void main(String[] args) { String ACCESS_KEY_ID = <your-access-key-id>; //
用户的Access Key ID String SECRET_ACCESS_KEY = <your-secret-access-key>; // 用户的Secret Access Key //
初始化一个AsGroupClient AsGroupClientConfiguration config = new AsGroupClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
AsGroupClient client = new AsGroupClient(config); } }
在上面代码中,变量ACCESS_KEY_ID与SECRET_ACCESS_KEY是由系统分配给用户的,均为字符串,用于标识用户,为访问BCC做签名验证。 其中ACCESS_KEY_ID对应控制台中的“Access Key ID”,SECRET_ACCESS_KEY对应控制台中的“Access Key Secret”,获取方式请参考《操作指南 获取ACCESSKEY》。
上面的方式使用默认域名作为AsGroup的服务地址,如果用户需要自己指定域名,可以通过传入ENDPOINT参数来指定。
String ACCESS_KEY_ID = <your-access-key-id>; // 用户的Access Key ID String SECRET_ACCESS_KEY = <your-secret-access-key>;
// 用户的Secret Access Key String ENDPOINT = <domain-name>; // 用户自己指定的域名
AsGroupClientConfiguration config = new AsGroupClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT); AsGroupClient client = new AsGroupClient(config);
注意: ENDPOINT参数只能用指定的包含区域的域名来进行定义,不指定时默认为北京区域http://as.bj.baidubce.com。百度智能云目前开放了多区域支持,请参考区域选择说明。
目前支持“华北-北京”、“华南-广州”、“华东-苏州”和“华中-武汉”四个区域。北京区域:http://as.bj.baidubce.com,广州区域:http://as.gz.baidubce.com,苏州区域:http://as.su.baidubce.com,武汉区域:http://as.fwh.baidubce.com。
配置HTTPS协议访问AsGroup
AsGroup支持HTTPS传输协议,您可以通过如下两种方式在AsGroup Java SDK中使用HTTPS访问AsGroup服务:
- 在endpoint中指明https:
String endpoint = "http://as.bj.baidubce.com"; String ak = "ak"; String sk
= "sk"; AsGroupClientConfiguration config = new AsGroupClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ak, sk)); AsGroupClient client = new AsGroupClient(config);
- 通过调用setProtocol方法设置https协议:
String endpoint = "as.bj.baidubce.com"; // endpoint中不包含protocol String ak = "ak"; String
sk = "sk"; AsGroupClientConfiguration config = new AsGroupClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ak, sk)); config.setEndpoint(ENDPOINT);
config.setProtocol(Protocol.HTTPS); // 如果不指明, 则使用http AsGroupClient client = new AsGroupClient(config);
注意:如果在endpoint中指明了protocol, 则endpoint中的生效, 另外单独再调用setProtocol()不起作用。
String endpoint = "http://as.bj.baidubce.com"; String ak = "ak"; String sk = "sk";
AsGroupClientConfiguration config = new AsGroupClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ak, sk)); config.setEndpoint(ENDPOINT);
config.setProtocol(Protocol.HTTPS); // endpoint中已经指明, 此为无效操作, 对http也是如此
AsGroupClient client = new AsGroupClient(config);
配置AsGroupClient
如果用户需要配置AsGroupClient的一些细节的参数,可以在构造AsGroupClient的时候传入AsGroupClientConfiguration对象。 AsGroupClientConfiguration是AsGroup服务的配置类,可以为客户端配置代理,最大连接数等参数。
使用代理
下面一段代码可以让客户端使用代理访问AsGroup服务:
String ACCESS_KEY_ID = <your-access-key-id>; // 用户的Access Key ID String SECRET_ACCESS_KEY =
<your-secret-access-key>; // 用户的Secret Access Key String ENDPOINT = <domain-name>;
// 用户自己指定的域名 // 创建AsGroupClientConfiguration实例 AsGroupClientConfiguration config =
new AsGroupClientConfiguration(); // 配置代理为本地8080端口 config.setProxyHost("127.0.0.1");
config.setProxyPort(8080); // 创建AsGroup客户端 config.setCredentials
(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY)); config.setEndpoint(ENDPOINT);
AsGroupClient client = new AsGroupClient(config);
使用上面的代码段,客户端的所有操作都会通过127.0.0.1地址的8080端口做代理执行。 对于有用户验证的代理,可以通过下面的代码段配置用户名和密码:
// 创建AsGroupClientConfiguration实例 AsGroupClientConfiguration config = new AsGroupClientConfiguration();
// 配置代理为本地8080端口 config.setProxyHost("127.0.0.1"); config.setProxyPort(8080);
//设置用户名和密码 config.setProxyUsername(<username>); //用户名 config.setProxyPassword(<password>); //密码
设置网络参数
用户可以用AsGroupClientConfiguration对基本网络参数进行设置:
AsGroupClientConfiguration config = new AsGroupClientConfiguration(); //
设置HTTP最大连接数为10 config.setMaxConnections(10); // 设置TCP连接超时为5000毫秒
config.setConnectionTimeout(5000); // 设置Socket传输数据超时的时间为2000毫秒 config.setSocketTimeout(2000);
参数说明
通过AsGroupClientConfiguration能指定的所有参数如下表所示:
参数 | 说明 |
---|---|
UserAgent用户代理,指HTTP的User-Agent头 | |
Protocol | 连接协议类型 |
ProxyDomain | 访问NTLM验证的代理服务器的Windows域名 |
ProxyHost | 代理服务器主机地址 |
ProxyPort | 代理服务器端口 |
ProxyUsername | 代理服务器验证的用户名 |
ProxyPassword | 代理服务器验证的密码 |
ProxyPreemptiveAuthenticationEnabled | 是否设置用户代理认证 |
ProxyWorkstation | NTLM代理服务器的Windows工作站名称 |
LocalAddress | 本地地址 |
ConnectionTimeoutInMillis | 建立连接的超时时间(单位:毫秒) |
SocketTimeoutInMillis | 通过打开的连接传输数据的超时时间(单位:毫秒) |
MaxConnections | 允许打开的最大HTTP连接数 |
RetryPolicy | 连接重试策略 |
SocketBufferSizeInBytes | Socket缓冲区大小 |
StreamBufferSize | 流文件缓冲区大小 |