我正在使用Drowpizard 0.7.1,但也許我會很快升級到0.8.4。Dropwizard:新的管理資源
有沒有人知道如何添加一個管理資源dropwizard,這是顯示在操作菜單中,如下面的例子?
Operational Menu
Metrics
Ping
Threads
Healthcheck
CustomAdminXy
我正在使用Drowpizard 0.7.1,但也許我會很快升級到0.8.4。Dropwizard:新的管理資源
有沒有人知道如何添加一個管理資源dropwizard,這是顯示在操作菜單中,如下面的例子?
Operational Menu
Metrics
Ping
Threads
Healthcheck
CustomAdminXy
我不認爲你可以輕鬆做到這一點。
AdminServlet
是在建立ServerFactory
時創建的。它可能會延長DefaultServerFactory
並覆蓋createAdminServlet
來創建你的鏈接等自定義管理員的servlet ......(你將不得不設置通過配置您的服務器的工廠。)
看起來這將涉及一些重複代碼並且可能相當脆弱。
它可能更容易只登記自己管理的servlet(除了常規的一個),例如:
environment.admin().addServlet("custom-admin", new CustomAdminServlet())
.addMapping("/custom-admin");
可能並不理想要麼。
覆蓋ServerFactory似乎是最簡單的方法。不幸的是,沒有更好的方法。但通過簡單地擴展Default,這應該是相當直接的。 – pandaadb
在Dropwizard 0.9.1版本中使用可以覆蓋菜單,而不會與"/*"
上的默認AdminServlet映射衝突。
中的應用:因爲所有的屬性是私有的
public void run(final NetworkModelApplicationConfiguration configuration, final Environment environment) {
environment.admin().addServlet("my-admin-menu", new MyAdminServlet()).addMapping("");
environment.admin().addServlet("my-admin-feature", new MyAdminFeatureServlet()).addMapping("/myAdminFeature");
}
擴展AdminServlet不是非常有用。我建立了一個HTTPServlet的讀取資源作爲模板:
public class MyAdminServlet extends HttpServlet {
private String serviceName;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.serviceName = config.getInitParameter("service-name");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getContextPath() + req.getServletPath();
resp.setStatus(200);
resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
try {
String template = getResourceAsString("/admin.html", "UTF-8");
String serviceName = this.serviceName == null?"":" (" + this.serviceName + ")";
writer.println(MessageFormat.format(template, new Object[] { path, serviceName }));
} finally {
writer.close();
}
}
String getResourceAsString(String resource, String charSet) throws IOException {
InputStream in = this.getClass().getResourceAsStream(resource);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
return out.toString(charSet);
}
}
我/admin.html
資源是這樣的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Operational Menu{1}</title>
</head>
<body>
<h1>Operational Menu{1}</h1>
<ul>
<li><a href="{0}/metrics?pretty=true">Metrics</a></li>
<li><a href="{0}/ping">Ping</a></li>
<li><a href="{0}/threads">Threads</a></li>
<li><a href="{0}/healthcheck?pretty=true">Healthcheck</a></li>
<li><a href="{0}/myAdminFeature">My Admin Feature</a></li>
</ul>
</body>
</html>
對此沒有新的想法? – heaphach
還是有人知道如何在DW 0.9.x中做到這一點? – heaphach