Sug*_*lai 7 java spring amazon-ec2 amazon-web-services aws-sdk
在我们的代码中,为了获取我们正在使用的 ec2 实例的区域EC2MetadataUtils.getEC2InstanceRegion(),我们刚刚意识到我们不能使用它EC2MetadataUtils,因为它是一个内部 API,可能会发生变化。
注意:这是一个内部 API,可能会发生变化。SDK 用户不应依赖于此。
进行了一些谷歌搜索,但无法找到替代解决方案,是否有任何替代解决方案可用于获取 ec2 实例的区域?
谢谢你的帮助!
这是以下的实现class: https: //github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-core/src/main/java/com/amazonaws/util/EC2MetadataUtils.java
尽管在谷歌上进行了搜索,但我没有找到 Java 替代方案,因此,我意识到需要进行更深入的研究。我将您拥有的可能性描述如下:
该警告清楚地表明使用替代方案是一个好主意,但不存在现成的替代方案以及未来版本的可能优点是class很好的反驳,因此您现在可以忽略此注释。
如果您在库中的其他地方找到了对此方法的调用,并且您能够以某种方式使用它,那么这可能是一种替代方案。例如,克隆后
git clone git@github.com:aws/aws-sdk-java.git
Run Code Online (Sandbox Code Playgroud)
并使用以下命令搜索此方法的出现:
grep -rn 'yourpath' -e "getEC2InstanceRegion"
Run Code Online (Sandbox Code Playgroud)
我得到了这些结果:
<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();
Run Code Online (Sandbox Code Playgroud)
第一个匹配是方法的定义。
第二场比赛如下所示:
/*
* 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,看起来getRegion的方法InstanceMetadataRegionProvider看起来像是您正在寻找的替代方案。
第三场比赛如下所示:
/*
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,getCurrentRegion看起来Regions像是另一种选择。如果您成功地使用其中之一来达到您的目的,那么重构就会很容易,并且相应地重构也是有意义的。
如果前两个选项对您来说不可行,那么您可以复制并重命名该class,这样您就可以确保即使内部 API 发生更改,该方法也将保持不变。这不是一个非常优雅的方法,也不容易实现,因为它class具有依赖性,因此,您将遇到一些难以解决的困难,但我们事先知道这是一个可能的解决方案。
这是一篇有关检索实例元数据的文章:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
我们可以看到,在实例的元数据信息中,您可以找到区域: https: //docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-categories.html
使用curl和jq的命令示例如下所示
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region
Run Code Online (Sandbox Code Playgroud)
可以在这里找到: https: //www.howtouselinux.com/post/find-ec2-instance-region-info-in-aws
| 归档时间: |
|
| 查看次数: |
617 次 |
| 最近记录: |