Dropwizard:新的管理资源
Dropwizard : New admin resource
我正在使用 Drowpizard 0.7.1,但也许我很快就会升级到 0.8.4。
有谁知道如何将管理资源添加到 dropwizard,它显示在操作菜单中,如下例所示?
Operational Menu
Metrics
Ping
Threads
Healthcheck
CustomAdminXy
我不认为你可以轻松做到这一点。
AdminServlet
is created when the ServerFactory
is built. It may be possible to extend DefaultServerFactory
and override createAdminServlet
使用您的链接等创建自定义 Admin servlet...(然后您必须通过配置设置您的服务器工厂。)
这似乎会涉及一些重复代码并且可能非常脆弱。
注册您自己的管理 servlet(除了常规的 servlet)可能更容易,例如:
environment.admin().addServlet("custom-admin", new CustomAdminServlet())
.addMapping("/custom-admin");
可能也不理想。
将 .addMapping("")
与 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>
我正在使用 Drowpizard 0.7.1,但也许我很快就会升级到 0.8.4。
有谁知道如何将管理资源添加到 dropwizard,它显示在操作菜单中,如下例所示?
Operational Menu
Metrics
Ping
Threads
Healthcheck
CustomAdminXy
我不认为你可以轻松做到这一点。
AdminServlet
is created when the ServerFactory
is built. It may be possible to extend DefaultServerFactory
and override createAdminServlet
使用您的链接等创建自定义 Admin servlet...(然后您必须通过配置设置您的服务器工厂。)
这似乎会涉及一些重复代码并且可能非常脆弱。
注册您自己的管理 servlet(除了常规的 servlet)可能更容易,例如:
environment.admin().addServlet("custom-admin", new CustomAdminServlet())
.addMapping("/custom-admin");
可能也不理想。
将 .addMapping("")
与 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>