简体   繁体   English

Dropwizard:新的管理资源

[英]Dropwizard : New admin resource

I'm using Drowpizard 0.7.1, but perhaps I will upgrade to 0.8.4 very soon. 我正在使用Drowpizard 0.7.1,但也许我很快会升级到0.8.4。

Does anyone know how to add a admin resource to dropwizard, that is shown in Operational Menu like the example below? 有没有人知道如何向dropwizard添加管理资源,这在操作菜单中显示,如下例所示?

Operational Menu

    Metrics
    Ping
    Threads
    Healthcheck
    CustomAdminXy

I don't think you can do this easily. 我认为你不能轻易做到这一点。

The AdminServlet is created when the ServerFactory is built. AdminServlet是在构建ServerFactory时创建的。 It may be possible to extend DefaultServerFactory and override createAdminServlet to create a custom Admin servlet with your links etc... (You would then have to set your server factory via configuration.) 可以扩展DefaultServerFactory并覆盖createAdminServlet以创建带有链接等的自定义Admin servlet ...(然后,您必须通过配置设置服务器工厂。)

It seems like this would involve some duplication of code and could be quite fragile. 看起来这会涉及一些代码重复,可能非常脆弱。

It might be easier to just register your own admin servlet (in addition to the regular one), eg: 注册自己的管理servlet(除常规管理servlet之外)可能更容易,例如:

environment.admin().addServlet("custom-admin", new CustomAdminServlet())
    .addMapping("/custom-admin");

Probably not ideal either. 可能也不理想。

Using .addMapping("") with Dropwizard version 0.9.1 allows you to override the menu without conflicting with the default AdminServlet mapping at "/*" . 使用.addMapping("")版本0.9.1的.addMapping("")允许您覆盖菜单而不会与"/*"处的默认AdminServlet映射冲突。

In the Application: 在申请中:

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");
}

Extending AdminServlet isn't very useful since all the properties are private. 扩展AdminServlet并不是非常有用,因为所有属性都是私有的。 I built an HTTPServlet that reads a resource as a template: 我构建了一个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);
  }
}

My /admin.html resource looks like this: 我的/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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM