java EC2MetadataUtils的替代

r1wp621o  于 2023-06-04  发布在  Java
关注(0)|答案(2)|浏览(465)

在我们的代码中,为了获取ec2示例的区域,我们使用了EC2MetadataUtils.getEC2InstanceRegion(),我们刚刚意识到我们不能使用EC2MetadataUtils,因为它是一个内部API,可能会发生变化。
注意:这是一个内部API,可能会发生变化。SDK的用户不应该依赖于此。
做了一些谷歌搜索,但无法找到一个替代的解决方案,是否有任何替代的解决方案,以获得ec2示例的区域?
感谢您的任何帮助!

mctunoxg

mctunoxg1#

下面是class的实现:https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/util/EC2MetadataUtils.java
我没有找到Java的替代方案,尽管在谷歌上搜索,所以,我意识到需要更深入的研究。我描述了您拥有的可能性如下:

1.您可以保持原样

这个警告清楚地表明使用替代方案是个好主意,但是不存在现成的替代方案以及class未来版本的可能好处是很好的反驳,所以现在可以忽略这个说明。

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;
    }
}

因此,getCurrentRegionRegions看起来像是另一种选择。如果你成功地使用了其中一个,那么重构就会很容易,而且相应地重构也是有意义的。

3.复制并重命名类

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

4.最后,自己动手的方法

这是一篇关于检索示例元数据的文章:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
我们可以看到,在示例的元数据信息中,可以找到region:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html

使用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

fnvucqvd

fnvucqvd2#

EC2示例运行的区域信息作为示例标识文档的一部分提供。
正如in the AWS documentation所解释的,以及@LajosArpad在他非常好的回答中所指出的,可以从EC2示例内部的以下URL获得此信息,例如运行curl

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字段。
例如,使用JSON处理的标准Java API,并使用documentation中提供的示例,您可以尝试以下操作来获取一行中的区域:

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读和写)。
该代码使用IDMSv 1,并且可以通过许多不同的方式进行改进,例如验证身份文档签名等。

相关问题