Skip to content

Commit

Permalink
Merge pull request #1361 from fiibbb/lingy/enable-custom-amazon-info-…
Browse files Browse the repository at this point in the history
…provider-factory

Make instance config factory support custom amazon info provider
  • Loading branch information
lingyuan2014 authored Nov 6, 2020
2 parents 360d536 + 5d2db6d commit c56bfb2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.netflix.appinfo;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import com.netflix.discovery.CommonConstants;
Expand Down Expand Up @@ -29,7 +30,7 @@ public class Ec2EurekaArchaius2InstanceConfig extends EurekaArchaius2InstanceCon
};

private final AmazonInfoConfig amazonInfoConfig;
private final RefreshableAmazonInfoProvider amazonInfoHolder;
private final Provider<AmazonInfo> amazonInfoHolder;

@Inject
public Ec2EurekaArchaius2InstanceConfig(Config configInstance, AmazonInfoConfig amazonInfoConfig) {
Expand All @@ -40,6 +41,12 @@ public Ec2EurekaArchaius2InstanceConfig(Config configInstance, AmazonInfoConfig
this(configInstance, new Archaius2AmazonInfoConfig(configInstance), CommonConstants.DEFAULT_CONFIG_NAMESPACE, info, false);
}

public Ec2EurekaArchaius2InstanceConfig(Config configInstance, Provider<AmazonInfo> amazonInfoProvider) {
super(configInstance, CommonConstants.DEFAULT_CONFIG_NAMESPACE);
this.amazonInfoConfig = null;
this.amazonInfoHolder = amazonInfoProvider;
}

public Ec2EurekaArchaius2InstanceConfig(Config configInstance, AmazonInfoConfig amazonInfoConfig, String namespace) {
this(configInstance, amazonInfoConfig, namespace, null, true);
}
Expand Down Expand Up @@ -73,8 +80,8 @@ public String getFallbackHostname() {

@Override
public String getHostName(boolean refresh) {
if (refresh) {
amazonInfoHolder.refresh();
if (refresh && this.amazonInfoHolder instanceof RefreshableAmazonInfoProvider) {
((RefreshableAmazonInfoProvider)amazonInfoHolder).refresh();
}
return amazonInfoHolder.get().get(MetaDataKey.publicHostname);
}
Expand All @@ -98,7 +105,9 @@ public String[] getDefaultAddressResolutionOrder() {
*/
@Deprecated
public synchronized void refreshAmazonInfo() {
amazonInfoHolder.refresh();
if (this.amazonInfoHolder instanceof RefreshableAmazonInfoProvider) {
((RefreshableAmazonInfoProvider)amazonInfoHolder).refresh();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.netflix.appinfo.providers;

import com.netflix.appinfo.AmazonInfo;

import javax.inject.Provider;

public interface AmazonInfoProviderFactory {
Provider<AmazonInfo> get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.netflix.appinfo.providers;

import com.netflix.appinfo.AmazonInfo;
import com.netflix.appinfo.Ec2EurekaArchaius2InstanceConfig;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.archaius.api.Config;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

@Singleton
public class CustomAmazonInfoProviderInstanceConfigFactory implements EurekaInstanceConfigFactory {

private final Config configInstance;
private final Provider<AmazonInfo> amazonInfoProvider;
private EurekaInstanceConfig eurekaInstanceConfig;

@Inject
public CustomAmazonInfoProviderInstanceConfigFactory(Config configInstance, AmazonInfoProviderFactory amazonInfoProviderFactory) {
this.configInstance = configInstance;
this.amazonInfoProvider = amazonInfoProviderFactory.get();
}

@Override
public EurekaInstanceConfig get() {
if (eurekaInstanceConfig == null) {
eurekaInstanceConfig = new Ec2EurekaArchaius2InstanceConfig(configInstance, amazonInfoProvider);
}

return eurekaInstanceConfig;
}
}

0 comments on commit c56bfb2

Please sign in to comment.