How to Fix Zipkin webMvcMetricsFilter Bean Creation Error in Spring Boot
Problem
When I started my Spring Boot application with Zipkin server enabled, I got this error:
Error creating bean with name 'webMvcMetricsFilter' defined in class path resource[org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcMetricsAutoConfiguration.class]
Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'prometheusMeterRegistry' defined in class path resource[io/micrometer/prometheus/PrometheusMeterRegistryAutoConfiguration.class]
Bean instantiation via factory method failed;nested exception is org.springframework.beans.BeanInstantiationException:Failed to instantiate [io.micrometer.prometheus.PrometheusMeterRegistry]:factory method 'prometheusMeterRegistry' threw exception;nested exception is java.lang.NoClassDefFoundError: zipkin2/internal/Buffer$WriterThe root cause is NoClassDefFoundError: zipkin2/internal/Buffer$Writer, which seems unrelated to the metrics filter at first glance.
Environment
- Spring Boot 2.2.4
- Spring Cloud Hoxton.SR1
- Zipkin 2.19.9
- Java 8+
What happened?
I was setting up a standalone Zipkin server using Spring Boot. My main class looked like this:
@EnableZipkinServer@SpringBootApplicationpublic class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); }}And my pom.xml dependencies:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency></dependencies>When I ran mvn spring-boot:run, the application failed to start with the bean creation error.
How to solve it?
The solution is to exclude Tomcat from spring-boot-starter-web. Zipkin server uses its own embedded server called Armeria, not Tomcat. Having both servers causes conflicts during bean initialization.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions></dependency>Now run again:
mvn spring-boot:runThe application starts successfully and Zipkin UI is accessible at http://localhost:9411.
The reason
I think the key reason is:
-
Two servers conflict:
spring-boot-starter-webbrings in Tomcat by default.@EnableZipkinServercreates a Zipkin server that uses Armeria. Both servers try to initialize, causing bean conflicts. -
Metrics registry depends on wrong server context: The
webMvcMetricsFilterfrom Spring Boot Actuator expects a standard Spring Boot web context. But Zipkin’s server configuration creates a different context, causing theNoClassDefFoundErrorfor Zipkin’s internal classes. -
Zipkin server is standalone by design: Zipkin server is meant to run as a standalone tracing collector, not as part of a regular Spring Boot web application. It needs its own server implementation for specific performance and protocol requirements.
Summary
In this post, I showed how to fix the webMvcMetricsFilter bean creation error when setting up Zipkin server with Spring Boot. The key point is to exclude spring-boot-starter-tomcat from the web starter dependency because Zipkin uses Armeria, not Tomcat.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments