How solve Error: java.lang.ClassNotFoundException: io.netty.util.concurrent.GenericFutureListener?

How solve Error: java.lang.ClassNotFoundException: io.netty.util.concurrent.GenericFutureListener?

昨天我第一次尝试用 Java 制作 Prometheus 客户端(从 Python 开始,最后做的是 GoLang)。

找到示例

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.Summary;
import io.prometheus.client.vertx.MetricsHandler;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
//import io.prometheus.client.vertx.MetricsHandler;

public class ExampleExporter {

    static final Gauge g = Gauge.build().name("gauge").help("blah").register();
    static final Counter c = Counter.build().name("counter").help("meh").register();
    static final Summary s = Summary.build().name("summary").help("meh").register();
    static final Histogram h = Histogram.build().name("histogram").help("meh").register();
    static final Gauge l = Gauge.build().name("labels").help("blah").labelNames("l").register();

    public static  void main(String[] args) throws Exception {
        final Vertx vertx = Vertx.vertx();
        final Router router = Router.router(vertx);

        router.route("/metrics").handler(new MetricsHandler());

        vertx.createHttpServer().requestHandler(router::accept).listen(8001);

        g.set(1);
        c.inc(2);
        s.observe(3);
        h.observe(4);
        l.labels("foo").inc(5);
    }



}

是否从 Maven 库加载。

尝试运行客户端后,出现return错误:

Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/util/concurrent/GenericFutureListener
    at io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:30)
    at io.vertx.core.Vertx.vertx(Vertx.java:78)
    at ExampleExporter.main(ExampleExporter.java:22)
Caused by: java.lang.ClassNotFoundException: io.netty.util.concurrent.GenericFutureListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

所有代码和库都已上传 Git Repository

请帮忙解决这个问题。

看起来您缺少依赖项。你可以从

https://mvnrepository.com/artifact/io.netty/netty-all/5.0.0.Alpha2

您可以添加

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>5.0.0.Alpha2</version>
</dependency>

添加到您的 pom 文件或从站点下载 jar 并将其添加到 class 路径或您的 lib 文件夹。