暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Prometheus+Micrometer集成SpringBoot遇到的空指针问题及解决方案

工匠人生 2019-07-17
1864

一期一会,一个执着于技术的公众号


历史Prometheus文章:

【译文】Thanos 灭霸- a Scalable Prometheus with Unlimited Storage

Prometheus 的九个坑【评注版】

全面学习Prometheus

Prometheus 入门与实践

性能工具之JMeter+InfluxDB+Grafana打造压测可视化实时监控

Prometheus实战:关于Redis序列化及压缩对性能的影响

问题:java.lang.NullPointerException org.apache.catalina.authenticator.AuthenticatorBase.getJaspicProvider(AuthenticatorBase.java:1192)

猪猪今天基于SpringBoot 1.5.X集成Prometheus的过程中,没有任何问题,示例代码托管在GitHub

https://github.com/CharlesMaster/prometheus-springboot1.5.x-demo

核心依赖如下:

<!--springboot单元测试工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!--监控系统健康情况的工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--web模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--低版本1.5.x版本springboot兼容micrometer-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-spring-legacy</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!--micrometer核心包,桥接Prometheus-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!--micrometer获取jvm相关信息,用于展示在Grafana上-->
        <dependency>
            <groupId>io.github.mweirauch</groupId>
            <artifactId>micrometer-jvm-extras</artifactId>
            <version>0.1.4</version>
        </dependency>

这里可以特别强调一下,最新版本的Micrometer已经不需要手动设置MeterRegistryCustomizer的Bean,只需要application.properties配置即可。


但是我在集成复杂的支付系统的过程中,我们是k8s集群,第一次部署测试环境报了如下错误:

<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.11 - Error report</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style> </head><body><h1>HTTP Status 500 - </h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>java.lang.NullPointerException
        org.apache.catalina.authenticator.AuthenticatorBase.getJaspicProvider(AuthenticatorBase.java:1192)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)
        org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:93)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:748)
</pre><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/8.5.11 logs.</u></p><hr class="line"><h3>Apache Tomcat/8.5.11</h3></body></html>

为了摆脱skywalking的影响,我在不启动skywalking的情况下重启了支付应用,报了不一样的错误:

<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.11 - Error report</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style> </head><body><h1>HTTP Status 500 - </h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>java.lang.NullPointerException
        org.apache.catalina.authenticator.AuthenticatorBase.getJaspicProvider(AuthenticatorBase.java:1192)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:465)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:748)
</pre><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/8.5.11 logs.</u></p><hr class="line"><h3>Apache Tomcat/8.5.11</h3></body></html>

但是这两个错误一致的原因都是

org.apache.catalina.authenticator.AuthenticatorBase.getJaspicProvider(AuthenticatorBase.java:1192)

是不是一脸懵逼?

解决方法很简单,将支付应用里的javaee-api依赖删除即可

然后就搞定啦。

这个问题可以参看https://stackoverflow.com/questions/38802437/upgrading-spring-boot-from-1-3-7-to-1-4-0-causing-nullpointerexception-in-authen

解决方法是三个:

方法一

Set authConfigFactory to default AuthConfigFactory implementation used by Tomcat 8.5 (example basic implementation):

package com.example;

import org.apache.catalina.authenticator.jaspic.AuthConfigFactoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.security.auth.message.config.AuthConfigFactory;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        if (AuthConfigFactory.getFactory() == null) {
            AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());
        }
        SpringApplication.run(DemoApplication.class, args);
    }
}

方法二

Remove duplicated AuthConfigFactory class from your classpath. In my case there were two different implementations of the same class:

org.apache.tomcat.embed/tomcat-embed-core/8.5.4/tomcat-embed-core-8.5.4.jar!/javax/security/auth/message/config/AuthConfigFactory.class
javax/javaee-api/7.0/javaee-api-7.0.jar!/javax/security/auth/message/config/AuthConfigFactory.class

javaee-api-7.0.jar has it's own AuthConfigFactory implementation which is not fully compatible with Tomcat 8.5 and causes that NullPointerException (in Tomcat's version there is constant which defines default jaspic implementation class) Remove javaee-api dependency (or any other which contains different AuthConfigFactory implementation) from your gradle/mvn project (if you can)

方法三

Downgrade Tomcat to 8.0 or 7.0:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-use-tomcat-

Explanation:

The problem is related with Tomcat upgrade (from 8.0.x to 8.5.x) which has been made during Spring Boot upgrade from 1.3.x to 1.4. The problem is that Tomcat 8.5 introduces jaspic support (https://tomcat.apache.org/tomcat-8.5-doc/config/jaspic.html) and provides it's own implementation of AuthConfigFactory. This implementation defines default jaspic auth factory implementation:

private static final String DEFAULT_JASPI_AUTHCONFIGFACTORYIMPL =
            "org.apache.catalina.authenticator.jaspic.AuthConfigFactoryImpl";

which is not defined in other implementations (e.g. that one from javaee-api-7.0) and causes NullPointerException because no AuthConfigFactory were instantiated.



欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢



知识星球是 公众号 工匠人生 忠实读者私密进阶学习圈。这里会有很多超越公众号技术深度的架构原创实战经验,也有私密微信群,分享行业深度洞见,交流碰撞,沉淀优质内容。


我的星球开通了分销功能,邀请一位朋友进入星球,他能打折,你还可以获得 41.79元。这也算给星球原始居民的福利哈,分享星球已经升级为橙色了,分享有赏分享可以赚钱赚佣金这件事和抢红包一样有意思 



       漫画:程序员小赵的架构师之路

       那些年,阿里教会我的事

     【感恩】当年,那个领我入门的项目老大......

       重新定义淘宝面试中遇到的架构问题

       Java5~11各个版本新特性史上最全总结     

       史上最全JUC脑图文字大纲珍藏版

     【我和世界不一样】复盘猪猪的知识星球

      Java与Netty实现高性能高并发

      Java并发编程73道面试题及答案 —— 面试稳了

     【BAT面试题系列】面试官:你了解乐观锁和悲观锁吗?

     【面试专题】昨晚面试过程中关于Maven依赖排除的问题和答案

     【面试题及答案】百度亿万级数据处理及面试现场系列合辑

       大数据十道经典海量数据处理面试题与十个方法大总结

     【猪猪原创时间】车的哲学故事——远光灯和近光灯,当蛮横遇上谦和

       微服务Dubbo全链路追踪,使用MDC和SPI生成全局TraceID

       RSocket思潮

     【朝花夕拾】Maven拾遗

        IDEA构建SpringBoot生成的maven-wrapper是什么?

        UUID会重复吗?

        浅谈项目管理

文章转载自工匠人生,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论