forked from Netflix/eureka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at eureka extention providing Guice bindings
- Loading branch information
Showing
6 changed files
with
487 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ Thumbs.db | |
*/target | ||
/build | ||
*/build | ||
/bin | ||
*/bin | ||
# | ||
# # IntelliJ specific files/directories | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
eureka-governator/src/main/java/com/netflix/discovery/DiscoveryStatusChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.netflix.discovery; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.google.inject.Singleton; | ||
import com.netflix.appinfo.InstanceInfo; | ||
import com.netflix.appinfo.InstanceInfo.InstanceStatus; | ||
import com.netflix.governator.annotations.Configuration; | ||
import com.netflix.governator.annotations.binding.Background; | ||
import com.netflix.governator.annotations.binding.UpStatus; | ||
|
||
/** | ||
* Background task for checking the up status of the instance in discovery. | ||
* It's important to check against the instance's status in discovery instead | ||
* of an internally managed state since an instance can be disabled externally | ||
* in Eureka. | ||
* | ||
* @author elandau | ||
* | ||
*/ | ||
@Singleton | ||
public class DiscoveryStatusChecker { | ||
private static Logger LOG = LoggerFactory.getLogger(DiscoveryStatusChecker.class); | ||
|
||
private final ScheduledExecutorService executor; | ||
private final Provider<DiscoveryClient> discoveryClientProvider; | ||
private final Provider<InstanceInfo> instanceInfoProvider; | ||
private final AtomicBoolean upStatus; | ||
|
||
@Configuration(value="discovery.status.interval") | ||
private long interval = 1; | ||
|
||
private ScheduledFuture<?> future = null; | ||
|
||
/** | ||
* @param executor | ||
* @param upStatus | ||
* @param discoveryClientProvider Provider that returns a discovery client. We use a provider | ||
* because the DiscoveryClient reference may not exist at bootstrap time | ||
*/ | ||
@Inject | ||
public DiscoveryStatusChecker( | ||
@Background ScheduledExecutorService executor, | ||
@UpStatus AtomicBoolean upStatus, | ||
Provider<DiscoveryClient> discoveryClientProvider, | ||
Provider<InstanceInfo> instanceInfoProvider) { | ||
this.executor = executor; | ||
this.discoveryClientProvider = discoveryClientProvider; | ||
this.upStatus = upStatus; | ||
this.instanceInfoProvider = instanceInfoProvider; | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
LOG.info("Updating internal instance discovery up status every " + interval + " seconds"); | ||
|
||
updateInstanceUpStatus(); | ||
this.future = executor.scheduleAtFixedRate(new Runnable(){ | ||
@Override | ||
public void run() { | ||
updateInstanceUpStatus(); | ||
}}, interval, interval, TimeUnit.SECONDS); | ||
} | ||
|
||
@PreDestroy | ||
public void shutdown() { | ||
if (future != null) | ||
future.cancel(true); | ||
} | ||
|
||
private void updateInstanceUpStatus() { | ||
DiscoveryClient client = discoveryClientProvider.get(); | ||
if (client != null) { | ||
final InstanceInfo instanceInfo = instanceInfoProvider.get(); | ||
if (instanceInfo != null) { | ||
List<InstanceInfo> discoveryInstanceInfoList = client.getInstancesById(instanceInfo.getId()); | ||
final boolean currentStatus; | ||
if ((discoveryInstanceInfoList != null) && (discoveryInstanceInfoList.size() == 1)) { | ||
InstanceInfo discoveryInstanceInfo = discoveryInstanceInfoList.get(0); | ||
currentStatus = InstanceStatus.UP.equals(discoveryInstanceInfo.getStatus()); | ||
} | ||
else { | ||
LOG.info("Instance not found"); | ||
currentStatus = false; | ||
} | ||
|
||
LOG.info("Discovery upStatus=" + currentStatus); | ||
|
||
if (upStatus.compareAndSet(!currentStatus, currentStatus)) { | ||
// TODO: Add subscribers | ||
} | ||
} | ||
else { | ||
LOG.info("Discovery InstanceInfo is null"); | ||
} | ||
} | ||
else { | ||
LOG.info("Discovery client is null"); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
eureka-governator/src/main/java/com/netflix/discovery/EurekaModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.netflix.discovery; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import com.google.common.base.Supplier; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Provides; | ||
import com.google.inject.Singleton; | ||
import com.google.inject.TypeLiteral; | ||
import com.netflix.appinfo.ApplicationInfoManager; | ||
import com.netflix.appinfo.InstanceInfo; | ||
import com.netflix.governator.annotations.binding.DownStatus; | ||
import com.netflix.governator.annotations.binding.UpStatus; | ||
|
||
/** | ||
* Guice module that provides specific bindings to Eureka components. | ||
* | ||
* Available bindings are, | ||
* | ||
* @UpStatus AtomicBoolean upStatus | ||
* @UpStatus Supplier<Boolean> upStatus | ||
* @DownStatus Supplier<Boolean> upStatus | ||
* DiscoveryClient | ||
* InstanceInfo | ||
* | ||
* @author elandau | ||
* | ||
*/ | ||
public class EurekaModule extends AbstractModule { | ||
|
||
private AtomicBoolean upStatus = new AtomicBoolean(); | ||
|
||
@Override | ||
protected void configure() { | ||
bind(AtomicBoolean.class) | ||
.annotatedWith(UpStatus.class) | ||
.toInstance(upStatus); | ||
|
||
bind(new TypeLiteral<Supplier<Boolean>>() {}) | ||
.annotatedWith(UpStatus.class) | ||
.toInstance(new Supplier<Boolean>() { | ||
@Override | ||
public Boolean get() { | ||
return upStatus.get(); | ||
} | ||
}); | ||
|
||
bind(new TypeLiteral<Supplier<Boolean>>() {}) | ||
.annotatedWith(DownStatus.class) | ||
.toInstance(new Supplier<Boolean>() { | ||
@Override | ||
public Boolean get() { | ||
return !upStatus.get(); | ||
} | ||
}); | ||
|
||
bind(DiscoveryStatusChecker.class); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public DiscoveryClient getDiscoveryClient() { | ||
return DiscoveryManager.getInstance().getDiscoveryClient(); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public InstanceInfo getInstanceInfo() { | ||
return ApplicationInfoManager.getInstance().getInfo(); | ||
} | ||
|
||
} |
Oops, something went wrong.