在Spring MVC 3项目中配置监控Micrometer/Prometheus/Grafana

x9ybnkn6  于 8个月前  发布在  Spring
关注(0)|答案(1)|浏览(70)

我想配置监控。我在互联网上找了找,找到了很多实现Spring Bout+ Micrometer + Prometheus + Grafana的例子。
但我的问题是我在Spring MVC 3. 2. 9上有一个老项目,我有一个多模块的项目:

  • 模块ui
  • 模块请求
  • 模块员工

Spring MVC上的模块ui。其余2个模块作为SOAP Web Services与它连接(我使用注解**@WebService**)。
我尝试手动配置监控,首先在ui-modulepom-file中添加了几个依赖:

<!-- client prometheus -->
<dependency>
     <groupid> io.prometheus </ groupid>
     <artifactid> simpleclient </rtifactid>
     <version> 0.6.0 </ version>
</fendency>

<!-- Servlet for data export in Prometheus -->
<dependency>
     <groupid> io.prometheus </ groupid>
     <artifactid> simpleclient_servlet </ artifactid>
     <version> 0.6.0 </ version>
</fendency>

<!-- Micrometer library -->
<dependency>
     <groupid> io.micrometer </ groupid>
     <artifactid> micrometer-core </rtifactid>
     <version> 1.1.3 </ version>
</fendency>

<!-- Allows to Micrometer work with Prometheus -->
<dependency>
     <groupid> io.micrometer </ groupid>
     <artifactid> micrometer-segistry-prometheus </ artifactid>
     <version> 1.1.3 </ version>
</fendency>

接下来,在web.xml文件中,我为导出指标创建了一个servlet:

<!-- Servlet for export metrics -->
<servlet>
     <servlet-name> prometheus </ servlet-name>
     <servlet-class> io.prometheus.client.exporter.MetricsServlet </sevlet-class>
     <load-on-startup> 1 </laad-on-startup>
</ servlet>

<!-- Path in which the metrics will be available -->
<servlet-mapping>
     <servlet-name> prometheus </ servlet-name>
     <url-pattern>/metrics </rl-pattern>
</ servlet-mapping>

然后我创建一个类,在其中注册必要的度量收集器:

@WebListener
public class PrometheusInitListener implements ServletContextListener {

     /*
      * Register the necessary metric collectors
      */
     @Override
     public void contextInitialized(ServletContextEvent ServletContextEvent) {
         PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry (PrometheusConfig.DEFAULT, CollectorRegistry.defaultRegistry, Clock.SYSTEM);
         new ClassLoaderMetrics (). bindTo (meterRegistry);
         new JvmMemoryMetrics(). bindTo (meterRegistry);
         new JvmGcMetrics(). bindTo (meterRegistry);
         new ProcessorMetrics (). bindTo (meterRegistry);
         new JvmThreadMetrics (). bindTo (meterRegistry);
     }

     @Override
     public void contextInitialized(ServletContextEvent servletContextEvent) {
     }
}

最后,我开始这个项目。我打开地址:
http://localhost:8080/core/metrics
我看到了标准的JVM指标:

# HELP JVM_GC_MEMORY_PROMOTED_BYTES_TOTAL COUNT of POSITISE InCreases in the SIZE of the Old Generation Memory Pool Before Gc TOM TOM
# Type JVM_GC_MEMERY_PROMOTED_BYTES_TOTAL COUNTER
JVM_GC_MEMORY_PROMOTED_BYTES_TOTAL 2.6146872E7
# HELP JVM_BUFFER_COUNT_BUFFERS ASTEMATE Of the Number of Buffers in the Pool
# Type jvm_buffer_count_buffers gauge
JVM_BUFFER_COUNT_BUFFERS {ID = "DIRECT",} 14.0
jvm_buffer_count_buffers {id = "mapped",} 0.0
. . . . .

现在我有这样一个问题:我想在另外两个模块中为一些业务逻辑配置自定义指标:requestsemployees。但我一时想不明白:如何将这些指标转移到ui-module中?请告诉我:-)。
现在我在Micrometer文档中读到了这样一句话:“Micrometer是支撑Sping Boot 2或更高版本metrics集合的仪器库。"。也许Micrometer不适用于旧的Spring 3项目?也许我应该使用另一个库,而不是Micrometer?

vpfxa7rd

vpfxa7rd1#

Spring WebMVC 3.2.9.RELEASE是在9年多前(2014-05-20)发布的,所以你应该升级。Micrometer比那更年轻,所以Spring Framework 3.x中没有Instrumentation。
你需要自己检测你的组件,你可以查看Spring Framework 6中的检测,但是考虑到你使用的是一个超过9年的版本,你可能无法按原样复制检测。
所以我认为你最好的做法是升级应用程序,你可以享受Spring Framework 6提供的工具。

相关问题