簡體   English   中英

AmazonCloudWatchClient不發送HTTP請求

[英]AmazonCloudWatchClient not sending HTTP requests

第一次使用Java中的AWS API獲取ec2-instance的雲監視統計信息。 我用谷歌搜索了一下,發現了一些代碼片段。 這里是

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)如何擺脫這個問題,以便我可以得到我的結果?

但是請不要要求我閱讀任何文檔,因為我在網絡,博客,論壇,文檔等中搜索時搞砸了,然后我就結束了。 因此,請提供解決方案或告訴我我要去哪里了。

誰能幫我解決這個問題。

先感謝您。

得到了解決方案。

1)刪除了AmazonCloudWatchClient的設置端點。

2)AWS憑證(訪問密鑰ID,秘密密鑰)出現問題。因此,我創建了另一組憑證並為用戶提供了CloudWatchFullAccess策略。

現在它就像Charm ... :-)

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM