EC2MetadataUtils的替代方案

7
在我们的代码中,为了获取EC2实例的区域,我们使用了EC2MetadataUtils.getEC2InstanceRegion(),并且我们刚刚意识到我们不应该使用EC2MetadataUtils,因为它是一个内部API,可能会发生变化。

注意:这是一个内部API,可能会发生变化。SDK用户不应该依赖它。

我做了一些谷歌搜索,但没有找到替代方案。有没有其他可用的解决方案可以获取EC2实例的区域?
感谢任何帮助!
2个回答

5
这是 class 的实现:https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/util/EC2MetadataUtils.java。在 Google 上搜寻后,我发现没有 Java 的替代方案,因此需要进行深入研究。以下是您可以采取的措施:

1. 你可以保持不变

警告明确建议使用备用方案,但是缺乏现成的替代方案以及未来版本可能带来的好处是反对意见,因此您现在可以忽略此提示。

2. 您可以下载开源库并查搜索此方法的调用

如果您在库中找到了此方法的调用,并能够以某种方式使用它,则可能是一种替代方案。例如,在克隆后:

git clone git@github.com:aws/aws-sdk-java.git

并使用以下方法搜索此方法的出现:

grep -rn 'yourpath' -e "getEC2InstanceRegion"

我得到了这些结果:
<path>/aws-sdk-java/aws-java-sdk-core/src/main/java/com/amazonaws/util/EC2MetadataUtils.java:286:    public static String getEC2InstanceRegion() {
<path>/aws-sdk-java/aws-java-sdk-core/src/main/java/com/amazonaws/regions/InstanceMetadataRegionProvider.java:59:            return EC2MetadataUtils.getEC2InstanceRegion();
<path>/aws-sdk-java/aws-java-sdk-core/src/main/java/com/amazonaws/regions/Regions.java:110:            final String region = EC2MetadataUtils.getEC2InstanceRegion();

第一项匹配是该方法的定义。

第二项匹配如下:

/*
 * Copyright 2011-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazonaws.regions;

import com.amazonaws.AmazonClientException;
import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.util.EC2MetadataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Attempts to load region information from the EC2 Metadata service. If the application is not
 * running on EC2 or {@link SDKGlobalConfiguration#isEc2MetadataDisabled()} returns true,
 * this provider will return null.
 */
public class InstanceMetadataRegionProvider extends AwsRegionProvider {

    private static final Log LOG = LogFactory.getLog(InstanceMetadataRegionProvider.class);

    /**
     * Cache region as it will not change during the lifetime of the JVM.
     */
    private volatile String region;

    /**
     * @throws AmazonClientException if {@link SDKGlobalConfiguration#isEc2MetadataDisabled()} is true
     */
    @Override
    public String getRegion() {
        if (SDKGlobalConfiguration.isEc2MetadataDisabled()) {
            throw new AmazonClientException("AWS_EC2_METADATA_DISABLED is set to true, not loading region from EC2 Instance "
                                         + "Metadata service");
        }

        if (region == null) {
            synchronized (this) {
                if (region == null) {
                    this.region = tryDetectRegion();
                }
            }
        }
        return region;
    }

    private String tryDetectRegion() {
        try {
            return EC2MetadataUtils.getEC2InstanceRegion();
        } catch (AmazonClientException sce) {
            LOG.debug("Ignoring failure to retrieve the region: " + sce.getMessage());
            return null;
        }
    }
}

看起来 InstanceMetadataRegionProvidergetRegion 方法是你正在寻找的替代方案。

第三个匹配项如下:

/*
 * Copyright 2013-2022 Amazon Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *    http://aws.amazon.com/apache2.0
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and
 * limitations under the License.
 */
package com.amazonaws.regions;

import com.amazonaws.AmazonClientException;
import org.apache.commons.logging.LogFactory;

import com.amazonaws.util.EC2MetadataUtils;

/**
 * Enumeration of region names
 */
public enum Regions {

    GovCloud("us-gov-west-1", "AWS GovCloud (US)"),
    US_GOV_EAST_1("us-gov-east-1", "AWS GovCloud (US-East)"),
    US_EAST_1("us-east-1", "US East (N. Virginia)"),
    US_EAST_2("us-east-2", "US East (Ohio)"),
    US_WEST_1("us-west-1", "US West (N. California)"),
    US_WEST_2("us-west-2", "US West (Oregon)"),
    EU_WEST_1("eu-west-1", "EU (Ireland)"),
    EU_WEST_2("eu-west-2", "EU (London)"),
    EU_WEST_3("eu-west-3", "EU (Paris)"),
    EU_CENTRAL_1("eu-central-1", "EU (Frankfurt)"),
    EU_NORTH_1("eu-north-1", "EU (Stockholm)"),
    EU_SOUTH_1("eu-south-1", "EU (Milan)"),
    AP_EAST_1("ap-east-1", "Asia Pacific (Hong Kong)"),
    AP_SOUTH_1("ap-south-1", "Asia Pacific (Mumbai)"),
    AP_SOUTHEAST_1("ap-southeast-1", "Asia Pacific (Singapore)"),
    AP_SOUTHEAST_2("ap-southeast-2", "Asia Pacific (Sydney)"),
    AP_SOUTHEAST_3("ap-southeast-3", "Asia Pacific (Jakarta)"),
    AP_NORTHEAST_1("ap-northeast-1", "Asia Pacific (Tokyo)"),
    AP_NORTHEAST_2("ap-northeast-2", "Asia Pacific (Seoul)"),
    AP_NORTHEAST_3("ap-northeast-3", "Asia Pacific (Osaka)"),

    SA_EAST_1("sa-east-1", "South America (Sao Paulo)"),
    CN_NORTH_1("cn-north-1", "China (Beijing)"),
    CN_NORTHWEST_1("cn-northwest-1", "China (Ningxia)"),
    CA_CENTRAL_1("ca-central-1", "Canada (Central)"),
    ME_SOUTH_1("me-south-1", "Middle East (Bahrain)"),
    AF_SOUTH_1("af-south-1", "Africa (Cape Town)"),
    US_ISO_EAST_1("us-iso-east-1", "US ISO East"),
    US_ISOB_EAST_1("us-isob-east-1", "US ISOB East (Ohio)"),
    US_ISO_WEST_1("us-iso-west-1", "US ISO West")
    ;

    /**
     * The default region that new customers in the US are encouraged to use
     * when using AWS services for the first time.
     */
    public static final Regions DEFAULT_REGION = US_WEST_2;

    private final String name;
    private final String description;

    private Regions(String name, String description) {
        this.name = name;
        this.description = description;
    }

    /**
     * The name of this region, used in the regions.xml file to identify it.
     */
    public String getName() {
        return name;
    }

    /**
     * Descriptive readable name for this region.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns a region enum corresponding to the given region name.
     *
     * @param regionName
     *            The name of the region. Ex.: eu-west-1
     * @return Region enum representing the given region name.
     */
    public static Regions fromName(String regionName) {
        for (Regions region : Regions.values()) {
            if (region.getName().equals(regionName)) {
                return region;
            }
        }
        throw new IllegalArgumentException("Cannot create enum from " + regionName + " value!");
    }

    /**
     * Returns a Region object representing the region the application is
     * running in, when running in EC2. If this method is called from a non-EC2
     * environment, it will return null.
     */
    public static Region getCurrentRegion() {
        try {
            final String region = EC2MetadataUtils.getEC2InstanceRegion();
            if (region != null)
                return RegionUtils.getRegion(region);
        } catch (AmazonClientException e) {
            LogFactory.getLog(Regions.class).debug(
                "Ignoring failure to retrieve the region: " + e.getMessage());
        }
        return null;
    }
}

因此,在Regions中,getCurrentRegion看起来是另一个选择。如果您成功地使用其中之一达到了您的目的,那么重构将变得容易,并且相应地进行重构也是有意义的。

3. 复制并重命名类

如果前两个选项对您不可行,则可以复制并重命名class,这样您就可以确保即使内部API更改,此方法也将保持不变。这不是一个非常优雅的方法,也不容易实现,因为class具有依赖项,因此您将遇到一些解决困难,但我们预先知道这是一种可能的解决方案。

4. 最后的自己动手方法

这是一篇关于检索实例元数据的文章:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

正如我们所看到的,除了实例的元数据信息之外,还可以找到区域:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html

enter image description here

一个使用curljq的命令示例如下:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region

可以在这里找到:https://www.howtouselinux.com/post/find-ec2-instance-region-info-in-aws


感谢您的回答,我在问题中必须提到了aws-sdk版本,它是2.17.127。在这个版本中,https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/util/EC2MetadataUtils.java不可用。 - Suganthan Madhavan Pillai
@SuganthanMadhavanPillai 这个类肯定是可用的,因为你甚至在你使用的版本中都使用了它。我建议你在GitHub或其他地方搜索它的源代码并将其克隆到本地。然后应用我所描述的相同方法。如果你有困难,请把2.17.127版本的链接发给我,我会帮你查看。 - Lajos Arpad

1

EC2实例运行的区域信息是作为实例身份文档的一部分提供的。

正如AWS文档中所解释的并且也被@LajosArpad在他非常好的答案中指出,可以通过从EC2实例内部运行curl等方式获取此信息,具体URL如下:

curl http://169.254.169.254/latest/dynamic/instance-identity/document

返回的响应将类似于以下内容:

{
    "devpayProductCodes" : null,
    "marketplaceProductCodes" : [ "1abc2defghijklm3nopqrs4tu" ], 
    "availabilityZone" : "us-west-2b",
    "privateIp" : "10.158.112.84",
    "version" : "2017-09-30",
    "instanceId" : "i-1234567890abcdef0",
    "billingProducts" : null,
    "instanceType" : "t2.micro",
    "accountId" : "123456789012",
    "imageId" : "ami-5fb8c835",
    "pendingTime" : "2016-11-19T16:32:11Z",
    "architecture" : "x86_64",
    "kernelId" : null,
    "ramdiskId" : null,
    "region" : "us-west-2"
}

请注意region字段。 EC2MetadataUtils基本上,当然,更详细地、以更结构化的方式,也可以阅读这些信息,以便为您提供实例正在运行的EC2区域。
您可以尝试在自己的代码中复制EC2MetadataUtils公开的功能的最小版本。
确切的方法将取决于您想要使用哪些库来执行任务,但是无论如何,您都需要读取返回的文档,将其解析为JSON,并获取region字段。
例如,使用标准的Java API进行JSON处理,并使用文档中提供的示例,您可以尝试类似以下方式以获取一行中的区域:
URL url = new URL("http://169.254.169.254/latest/dynamic/instance-identity/document");
try (InputStream is = url.openStream();
     JsonReader reader = Json.createReader(is)) {

     JsonObject instanceDocument = reader.readObject();
     String region = instanceDocument.getString("region");
     // return it or use it as you need to
}

正如我所说,你可以使用其他库(例如Jackson、org.json、Gson等)进行JSON处理,或者像commons-io一样的库来读写URL/InputStream。
这段代码使用IDMSv1协议,可以通过许多不同的方式进行改进,例如验证身份文件签名等。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接