Spring Boot+Vertx应用Prometheus监控异常:指标未找到求助
Hey there, let's work through that Prometheus metrics-not-found error you're hitting with your Spring Boot + Vert.x app. I’ve debugged similar setups before, so here’s a step-by-step breakdown of fixes and checks to get your metrics flowing properly:
1. Double-Check Your Dependencies
First, make sure you have all the required libraries in your build file—missing dependencies are a common culprit here. For Spring Boot + Prometheus + Vert.x monitoring, add these:
- Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_boot</artifactId> <version>0.16.0</version> <!-- Use a version compatible with your Spring Boot release --> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.16.0</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-micrometer-metrics</artifactId> </dependency> - Gradle:
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.prometheus:simpleclient_spring_boot:0.16.0' implementation 'io.prometheus:simpleclient_hotspot:0.16.0' implementation 'io.vertx:vertx-micrometer-metrics'
Note: Verify version compatibility with your Spring Boot (2.x works best with 0.1x.x series Prometheus clients) and Vert.x versions.
2. Expose the Prometheus Endpoint
Spring Boot Actuator hides most endpoints by default. Add these settings to your application.properties or application.yml to make the metrics endpoint accessible:
- .properties:
management.endpoints.web.exposure.include=prometheus,health management.endpoint.prometheus.enabled=true management.metrics.export.prometheus.enabled=true - .yml:
management: endpoints: web: exposure: include: prometheus,health endpoint: prometheus: enabled: true metrics: export: prometheus: enabled: true
This unlocks the /actuator/prometheus endpoint that Prometheus needs to scrape.
3. Fix Your MonitoringConfig (And Add Vert.x Metrics)
Your current config handles Spring Boot metrics, but it’s missing Vert.x-specific setup. Here’s an updated version that covers both:
@Configuration public class MonitoringConfig { // Register Spring Boot metrics with Prometheus @Bean SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) { SpringBootMetricsCollector collector = new SpringBootMetricsCollector(publicMetrics); collector.register(); return collector; } // Configure Vert.x to use Prometheus metrics @Bean public Vertx vertx() { PrometheusMetricsOptions metricsOptions = new PrometheusMetricsOptions() .setEnabled(true) .setStartEmbeddedServer(false); // Let Spring Boot manage the endpoint return Vertx.vertx(new VertxOptions().setMetricsOptions(metricsOptions)); } // Bind Vert.x metrics to the shared meter registry @Bean public MeterRegistryCustomizer<MeterRegistry> vertxMetricsBinder(Vertx vertx) { return registry -> { VertxPrometheusMetrics vertxMetrics = (VertxPrometheusMetrics) vertx.metrics(); if (vertxMetrics != null) { vertxMetrics.bindToRegistry(registry); } }; } }
If you’re relying on Spring Boot’s auto-configured Vertx instance, you can adjust the vertx() bean to use the auto-configured options instead of creating a new one.
4. Verify the Endpoint is Accessible
After updating configs, start your app and test the endpoint directly:
- Hit
http://localhost:<your-app-port>/actuator/prometheusin a browser or runcurl http://localhost:<your-app-port>/actuator/prometheus - You should see a raw list of metrics (like
jvm_memory_used_bytes,vertx_http_server_requests_total) if everything works.
If you get a 404:
- Check that your app is running on the port you expect.
- If you use Spring Security, add a rule to allow unauthenticated access to actuator endpoints:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/**").permitAll() .anyRequest().authenticated(); } }
5. Fix Prometheus Scrape Configuration
Make sure your prometheus.yml is pointing to the correct endpoint:
scrape_configs: - job_name: 'spring-boot-vertx-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['localhost:8080'] # Replace with your app's host and port
Restart Prometheus after updating this file.
If you’ve gone through all these steps and still see the error, share your exact Spring Boot/Vert.x versions and full error log—those details will help narrow down the issue further!
内容的提问来源于stack exchange,提问作者Bambus

