org.nutz.boot.starter.eureka.server.EurekaServletStarter Maven / Gradle / Ivy
package org.nutz.boot.starter.eureka.server;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.nutz.boot.starter.WebServletFace;
import org.nutz.conf.NutConf;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;
import com.netflix.appinfo.AmazonInfo;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.ConfigurationManager;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Pair;
import com.netflix.eureka.EurekaServerContext;
import com.netflix.eureka.EurekaServerContextHolder;
import com.netflix.eureka.cluster.PeerEurekaNode;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
import com.netflix.eureka.resources.StatusResource;
import com.netflix.eureka.util.StatusInfo;
@SuppressWarnings("serial")
@IocBean
public class EurekaServletStarter extends HttpServlet implements WebServletFace {
@Override
public String getName() {
return "eureka";
}
@Override
public String getPathSpec() {
return "/action/status.json";
}
@Override
public Servlet getServlet() {
return this;
}
public void init(ServletConfig config) {
NutConf.USE_FASTCLASS = false;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String pathInfo = request.getRequestURI();
NutMap result = NutMap.NEW();
result.put("basePath",
request.getScheme()
+ "://"
+ request.getServerName()
+ ":"
+ request.getServerPort()
+ request.getContextPath()
+ "/");
populateBase(result);
if (pathInfo.endsWith("/status.json")) {
status(result);
lastn(result);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().println(Json.toJson(result, JsonFormat.tidy()));
} else {
response.setStatus(404);
}
}
private void status(NutMap result) {
populateApps(result);
StatusInfo statusInfo;
try {
statusInfo = new StatusResource().getStatusInfo();
}
catch (Exception e) {
statusInfo = StatusInfo.Builder.newBuilder().isHealthy(false).build();
}
result.put("statusInfo", statusInfo);
populateInstanceInfo(result, statusInfo);
filterReplicas(result, statusInfo);
}
private void lastn(NutMap result) {
PeerAwareInstanceRegistryImpl registry = (PeerAwareInstanceRegistryImpl) getRegistry();
List lastNCanceled = registry.getLastNCanceledInstances()
.stream()
.map(entry -> NutMap.NEW()
.setv("id", entry.second())
.setv("date",
new Date(entry.first())))
.collect(Collectors.toList());
result.put("lastNCanceled", lastNCanceled);
List lastNRegistered = registry.getLastNRegisteredInstances()
.stream()
.map(entry -> NutMap.NEW()
.setv("id", entry.second())
.setv("date",
new Date(entry.first())))
.collect(Collectors.toList());
result.put("lastNRegistered", lastNRegistered);
}
private PeerAwareInstanceRegistry getRegistry() {
return getServerContext().getRegistry();
}
private EurekaServerContext getServerContext() {
return EurekaServerContextHolder.getInstance().getServerContext();
}
private void populateBase(NutMap result) {
result.put("time", new Date());
populateHeader(result);
populateNavbar(result);
}
private void populateNavbar(NutMap result) {
List replicas = new ArrayList<>();
List list = getServerContext().getPeerEurekaNodes().getPeerNodesView();
for (PeerEurekaNode node : list) {
try {
URI uri = new URI(node.getServiceUrl());
String href = scrubBasicAuth(node.getServiceUrl());
replicas.add(NutMap.NEW().setv("key", uri.getHost()).setv("value", href));
}
catch (Exception ex) {}
}
result.put("replicas", replicas);
}
private void populateHeader(NutMap result) {
result.put("currentTime", StatusResource.getCurrentTimeAsString());
result.put("upTime", StatusInfo.getUpTime());
result.put("environment",
ConfigurationManager.getDeploymentContext().getDeploymentEnvironment());
result.put("datacenter",
ConfigurationManager.getDeploymentContext().getDeploymentDatacenter());
PeerAwareInstanceRegistry registry = getRegistry();
NutMap registryMap = NutMap.NEW()
.setv("leaseExpirationEnabled",
registry.isLeaseExpirationEnabled())
.setv("numOfRenewsPerMinThreshold",
registry.getNumOfRenewsPerMinThreshold())
.setv("numOfRenewsInLastMin", registry.getNumOfRenewsInLastMin())
.setv("selfPreservationModeEnabled",
registry.isSelfPreservationModeEnabled());
result.put("registry", registryMap);
result.put("isBelowRenewThresold", registry.isBelowRenewThresold() == 1);
DataCenterInfo info = getServerContext().getApplicationInfoManager()
.getInfo()
.getDataCenterInfo();
result.put("amazonInfo", false);
if (info.getName() == DataCenterInfo.Name.Amazon) {
AmazonInfo amazonInfo = (AmazonInfo) info;
result.put("amazonInfo", true);
result.put("amiId", amazonInfo.get(AmazonInfo.MetaDataKey.amiId));
result.put("availabilityZone", amazonInfo.get(AmazonInfo.MetaDataKey.availabilityZone));
result.put("instanceId", amazonInfo.get(AmazonInfo.MetaDataKey.instanceId));
}
}
private void populateApps(NutMap result) {
List sortedApplications = getRegistry().getSortedApplications();
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy