use of com.netflix.discovery.shared.Application in project jhipster-registry by jhipster.
the class ZuulUpdaterService method updateZuulRoutes.
@Scheduled(fixedDelay = 5_000)
public void updateZuulRoutes() {
boolean isDirty = false;
List<Application> applications = EurekaServerContextHolder.getInstance().getServerContext().getRegistry().getApplications().getRegisteredApplications();
for (Application application : applications) {
for (InstanceInfo instanceInfos : application.getInstances()) {
if (!instanceInfos.getStatus().equals(InstanceInfo.InstanceStatus.UP) && !instanceInfos.getStatus().equals(InstanceInfo.InstanceStatus.STARTING))
continue;
String instanceId = instanceInfos.getId();
String url = instanceInfos.getHomePageUrl();
log.debug("Checking instance {} - {} ", instanceId, url);
ZuulRouteDTO route = new ZuulRouteDTO(instanceId, application.getName().toLowerCase() + "/" + instanceId + "/**", null, url, zuulProperties.isStripPrefix(), zuulProperties.getRetryable(), null, instanceInfos.getStatus().toString());
if (zuulProperties.getRoutes().containsKey(instanceId)) {
log.debug("Instance '{}' already registered", instanceId);
if (!zuulProperties.getRoutes().get(instanceId).getUrl().equals(url) || !((ZuulRouteDTO) zuulProperties.getRoutes().get(instanceId)).getStatus().equals(instanceInfos.getStatus().toString())) {
log.debug("Updating instance '{}' with new URL: {}", instanceId, url);
zuulProperties.getRoutes().put(instanceId, route);
isDirty = true;
}
} else {
log.debug("Adding instance '{}' with URL: {}", instanceId, url);
zuulProperties.getRoutes().put(instanceId, route);
isDirty = true;
}
}
}
List<String> zuulRoutesToRemove = new ArrayList<>();
for (String key : zuulProperties.getRoutes().keySet()) {
if (applications.stream().flatMap(application -> application.getInstances().stream()).filter(instanceInfo -> instanceInfo.getId().equals(key)).count() == 0) {
log.debug("Removing instance '{}'", key);
zuulRoutesToRemove.add(key);
isDirty = true;
}
}
for (String key : zuulRoutesToRemove) {
zuulProperties.getRoutes().remove(key);
}
if (isDirty) {
log.info("Zuul routes have changed - refreshing the configuration");
this.publisher.publishEvent(new RoutesRefreshedEvent(routeLocator));
}
}
use of com.netflix.discovery.shared.Application in project jhipster-registry by jhipster.
the class EurekaResource method getApplications.
private List<Map<String, Object>> getApplications() {
List<Application> sortedApplications = getRegistry().getSortedApplications();
ArrayList<Map<String, Object>> apps = new ArrayList<>();
for (Application app : sortedApplications) {
LinkedHashMap<String, Object> appData = new LinkedHashMap<>();
apps.add(appData);
appData.put("name", app.getName());
List<Map<String, Object>> instances = new ArrayList<>();
for (InstanceInfo info : app.getInstances()) {
Map<String, Object> instance = new HashMap<>();
instance.put("instanceId", info.getInstanceId());
instance.put("homePageUrl", info.getHomePageUrl());
instance.put("healthCheckUrl", info.getHealthCheckUrl());
instance.put("statusPageUrl", info.getStatusPageUrl());
instance.put("status", info.getStatus().name());
instance.put("metadata", info.getMetadata());
instances.add(instance);
}
appData.put("instances", instances);
}
return apps;
}
use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.
the class EurekaControllerTests method setup.
@Before
public void setup() throws Exception {
PeerEurekaNodes peerEurekaNodes = mock(PeerEurekaNodes.class);
when(peerEurekaNodes.getPeerNodesView()).thenReturn(Collections.<PeerEurekaNode>emptyList());
InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("test").setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).build();
this.infoManager = mock(ApplicationInfoManager.class);
this.original = ApplicationInfoManager.getInstance();
setInstance(this.infoManager);
when(this.infoManager.getInfo()).thenReturn(instanceInfo);
Application myapp = new Application("myapp");
myapp.addInstance(InstanceInfo.Builder.newBuilder().setAppName("myapp").setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn)).setInstanceId("myapp:1").build());
ArrayList<Application> applications = new ArrayList<>();
applications.add(myapp);
PeerAwareInstanceRegistry registry = mock(PeerAwareInstanceRegistry.class);
when(registry.getSortedApplications()).thenReturn(applications);
EurekaServerContext serverContext = mock(EurekaServerContext.class);
EurekaServerContextHolder.initialize(serverContext);
when(serverContext.getRegistry()).thenReturn(registry);
when(serverContext.getPeerEurekaNodes()).thenReturn(peerEurekaNodes);
when(serverContext.getApplicationInfoManager()).thenReturn(this.infoManager);
}
use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.
the class EurekaController method populateApps.
private void populateApps(Map<String, Object> model) {
List<Application> sortedApplications = getRegistry().getSortedApplications();
ArrayList<Map<String, Object>> apps = new ArrayList<>();
for (Application app : sortedApplications) {
LinkedHashMap<String, Object> appData = new LinkedHashMap<>();
apps.add(appData);
appData.put("name", app.getName());
Map<String, Integer> amiCounts = new HashMap<>();
Map<InstanceInfo.InstanceStatus, List<Pair<String, String>>> instancesByStatus = new HashMap<>();
Map<String, Integer> zoneCounts = new HashMap<>();
for (InstanceInfo info : app.getInstances()) {
String id = info.getId();
String url = info.getStatusPageUrl();
InstanceInfo.InstanceStatus status = info.getStatus();
String ami = "n/a";
String zone = "";
if (info.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
AmazonInfo dcInfo = (AmazonInfo) info.getDataCenterInfo();
ami = dcInfo.get(AmazonInfo.MetaDataKey.amiId);
zone = dcInfo.get(AmazonInfo.MetaDataKey.availabilityZone);
}
Integer count = amiCounts.get(ami);
if (count != null) {
amiCounts.put(ami, count + 1);
} else {
amiCounts.put(ami, 1);
}
count = zoneCounts.get(zone);
if (count != null) {
zoneCounts.put(zone, count + 1);
} else {
zoneCounts.put(zone, 1);
}
List<Pair<String, String>> list = instancesByStatus.get(status);
if (list == null) {
list = new ArrayList<>();
instancesByStatus.put(status, list);
}
list.add(new Pair<>(id, url));
}
appData.put("amiCounts", amiCounts.entrySet());
appData.put("zoneCounts", zoneCounts.entrySet());
ArrayList<Map<String, Object>> instanceInfos = new ArrayList<>();
appData.put("instanceInfos", instanceInfos);
for (Iterator<Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>>> iter = instancesByStatus.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<InstanceInfo.InstanceStatus, List<Pair<String, String>>> entry = iter.next();
List<Pair<String, String>> value = entry.getValue();
InstanceInfo.InstanceStatus status = entry.getKey();
LinkedHashMap<String, Object> instanceData = new LinkedHashMap<>();
instanceInfos.add(instanceData);
instanceData.put("status", entry.getKey());
ArrayList<Map<String, Object>> instances = new ArrayList<>();
instanceData.put("instances", instances);
instanceData.put("isNotUp", status != InstanceInfo.InstanceStatus.UP);
for (Pair<String, String> p : value) {
LinkedHashMap<String, Object> instance = new LinkedHashMap<>();
instances.add(instance);
instance.put("id", p.first());
String url = p.second();
instance.put("url", url);
boolean isHref = url != null && url.startsWith("http");
instance.put("isHref", isHref);
/*
* String id = p.first(); String url = p.second(); if(url != null &&
* url.startsWith("http")){
* buf.append("<a href=\"").append(url).append("\">"); }else { url =
* null; } buf.append(id); if(url != null){ buf.append("</a>"); }
* buf.append(", ");
*/
}
}
// out.println("<td>" + buf.toString() + "</td></tr>");
}
model.put("apps", apps);
}
use of com.netflix.discovery.shared.Application in project spring-cloud-netflix by spring-cloud.
the class RestTemplateEurekaHttpClient method getApplication.
@Override
public EurekaHttpResponse<Application> getApplication(String appName) {
String urlPath = serviceUrl + "apps/" + appName;
ResponseEntity<Application> response = restTemplate.exchange(urlPath, HttpMethod.GET, null, Application.class);
Application application = response.getStatusCodeValue() == HttpStatus.OK.value() && response.hasBody() ? response.getBody() : null;
return anEurekaHttpResponse(response.getStatusCodeValue(), application).headers(headersOf(response)).build();
}
Aggregations