作者:惜洛妍_311 | 来源:互联网 | 2024-11-26 19:33
本文详细探讨了Java中com.codahale.metrics.servlets.AdminServlet.()方法的实现与应用,并提供了多个实际项目中的代码示例,帮助开发者更好地理解和使用这一方法。
本文详细介绍了 Java 中 com.codahale.metrics.servlets.AdminServlet.()
方法的使用场景和具体实现方式。通过分析来自 Github
、Stackoverflow
和 Maven
等平台上的多个实际项目中的代码片段,本文为读者提供了丰富的参考资料,旨在帮助开发者更好地理解并利用 AdminServlet.()
方法。
AdminServlet. 方法概述
AdminServlet.()
是一个构造函数,用于初始化 AdminServlet
类的实例。该类通常用于提供系统管理和监控功能,如健康检查、性能指标展示等。通过配置不同的参数和监听器,可以实现对应用状态的全面监控。
代码示例
示例 1 - 来源:twitter/distributedlog
public void start() throws Exception {
ServletContextHandler cOntext= new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
jettyServer.setHandler(context);
context.addEventListener(new HealthCheckServletContextListener(healthCheckRegistry));
context.addEventListener(new MetricsServletContextListener(metricRegistry));
context.addServlet(new ServletHolder(new AdminServlet()), "/*");
jettyServer.start();
}
示例 2 - 来源:dropwizard/dropwizard
protected Handler createAdminServlet(Server server,
MutableServletContextHandler handler,
MetricRegistry metrics,
HealthCheckRegistry healthChecks) {
configureSessionsAndSecurity(handler, server);
handler.setServer(server);
handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics);
handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks);
handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*");
final String allowedMethodsParam = allowedMethods.stream()
.collect(Collectors.joining(","));
handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST))
.setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam);
return handler;
}
示例 3 - 来源:io.dropwizard/dropwizard-core
protected Handler createAdminServlet(Server server,
MutableServletContextHandler handler,
MetricRegistry metrics,
HealthCheckRegistry healthChecks) {
configureSessionsAndSecurity(handler, server);
handler.setServer(server);
handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics);
handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks);
handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*");
handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST))
.setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, Joiner.on(',').join(allowedMethods));
return handler;
}
示例 4 - 来源:com.opentable.components/otj-metrics-core
@Bean
public ServletRegistrationBean getMetricsServlet() {
return new ServletRegistrationBean<>(new AdminServlet(), path + "/*");
}
示例 5 - 来源:stackoverflow.com
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new AdminServlet(),"/metrics/admin/*");
}
示例 6 - 来源:stackoverflow.com
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}
@Bean
public ServletRegistrationBean adminServletRegistrationBean() {
return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet");
}
示例 7 - 来源:org.apache.distributedlog/distributedlog-service
public void start() throws Exception {
ServletContextHandler cOntext= new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
jettyServer.setHandler(context);
context.addEventListener(new HealthCheckServletContextListener(healthCheckRegistry));
context.addEventListener(new MetricsServletContextListener(metricRegistry));
context.addServlet(new ServletHolder(new AdminServlet()), "/*");
jettyServer.start();
}
示例 8 - 来源:airbnb/billow
adminHandler.addServlet(new ServletHolder(new AdminServlet()), "/*");
示例 9 - 来源:Kixeye/chassis
context.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthCheckRegistry);
ServletHolder holder = new ServletHolder(new AdminServlet());
holder.setInitParameter("service-name", System.getProperty("app.name"));
context.addServlet(holder, "/metrics/*");
示例 10 - 来源:stackoverflow.com
public class YourApplication extends Application {
// ...
@Override
public void run(YourConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new MyResource());
environment.getApplicationContext().setAttribute(
MetricsServlet.METRICS_REGISTRY,
environment.metrics());
environment.getApplicationContext().setAttribute(
HealthCheckServlet.HEALTH_CHECK_REGISTRY,
environment.healthChecks());
environment.getApplicationContext().addServlet(
new NonblockingServletHolder(new AdminServlet()), "/admin/*");
}
}
示例 11 - 来源:apache/lens
.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", (metricsService.getMetricRegistry()));
adminCtx.setAttribute("com.codahale.metrics.servlets.HealthCheckServlet.registry", metricsService.getHealthCheck());
final ServletRegistration sgMetrics = adminCtx.addServlet("admin", new AdminServlet());
sgMetrics.addMapping("/admin/*");
adminCtx.deploy(server);