我第一次在 java 中使用 aws api 来获取我的 ec2 实例的云监视统计信息。我用谷歌搜索了这个,我发现了一些代码片段。这里是
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(
new BasicAWSCredentials(AccessKey, SecretKey));
cloudWatch.setEndpoint("ec2-<my-static-ip>.compute-1.amazonaws.com");
long offsetInMilliseconds = 1000 * 60 * 60 * 24;
Dimension instanceDimension = new Dimension();
instanceDimension.setName("Instanceid");
instanceDimension.setValue(InstanceId);
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(
new Date(new Date().getTime()
- offsetInMilliseconds))
.withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withDimensions(
new Dimension().withName("InstanceId").withValue(
InstanceId))
.withMetricName("CPUUtilization")
.withStatistics("Average", "Maximum")
.withEndTime(new Date());
GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch
.getMetricStatistics(request);
double avgCPUUtilization = 0;
List dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint) {
Datapoint dp = (Datapoint) aDataPoint;
avgCPUUtilization = dp.getAverage();
System.out.println(InstanceId
+ " instance's average CPU utilization : "
+ dp.getAverage());
}
} catch (AmazonServiceException ase) {
System.out
.println("Caught an AmazonServiceException, which means the request was made "
+ "to Amazon EC2, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
}
所以,使用这段代码我试图获取统计信息,但第一次抛出错误说
com.amazonaws.AmazonClientException: Unable to execute HTTP request:Connection to https://ec2-<my-static-ip>.compute-1.amazonaws.com refused
然后我认为它正在发送 https 请求。所以我在我的实例上启用了 ssl 并尝试了,然后我遇到了异常。
com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated
我在我的实例中使用 OpenJDK,所以我认为这可能会导致问题。然后我删除了 openjdk 并安装了 Oracle JDK 1.7。但仍然是同样的问题。
我的问题是,
1) 我怎样才能只发送 HTTP(而不是 HTTPS)请求来获取统计信息?
2)如何摆脱这个问题,这样我就能得到我的结果?
但请不要让我阅读任何文档,因为我在网络、博客、论坛、文档等中搜索时搞砸了。然后我就到了这里。所以,请为我提供解决方案或告诉我哪里出错了。
谁能帮我解决这个问题。
先感谢您。