【问题标题】:When i use Spring Boot with shiro , Spring Boot Actuator doesnt publish jvm metric当我将 Spring Boot 与 shiro 一起使用时,Spring Boot Actuator 不会发布 jvm 指标
【发布时间】:2019-03-27 08:09:22
【问题描述】:

我将 Spring Boot 与 Shiro 一起使用,但我启动 Application 并导航到 /actuator/metrics 会显示可用仪表名称的列表:

{
    names: [
        "http.server.requests",
        "jdbc.connections.max",
        "jdbc.connections.min"
    ]
}

只有 3 件!!!

然后,我屏蔽 shiro 配置:

//@Configuration
public class ShiroConfig {
      ...
}

我再次启动应用程序,我得到了所有:

{
names: [
"jvm.memory.max",
"process.files.max",
"jvm.gc.memory.promoted",
"tomcat.cache.hit",
"system.load.average.1m",
"tomcat.cache.access",
"jvm.memory.used",
"jvm.gc.max.data.size",
"jdbc.connections.max",
"jdbc.connections.min",
"jvm.gc.pause",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"tomcat.global.request",
"tomcat.sessions.expired",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"tomcat.servlet.request.max",
"tomcat.threads.current",
"tomcat.servlet.request",
"process.files.open",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"tomcat.threads.busy",
"process.start.time",
"tomcat.servlet.error"
]
}

我想同时使用 Actuator 和 Shiro。 感谢您的帮助。

【问题讨论】:

  • 你能分享你的 Pom.xml 和其他配置吗?
  • 我缩小了问题范围:当我使用shiro时,我在我的CustomRealm.java文件中注入了mybatis的xxxMapper,因为我需要通过mybatis从数据库中获取数据。我使用@Autowired注入了mybatis的xxxMapper,我尽量不注入mybatis的xxxMapper,所以我删除了注解,虽然我无法从数据库中获取数据。没想到,我删除注解后/actuator/metrics显示了一个列表可用的仪表名称!!!
  • 我认为可能是由于 Beans 的加载顺序。
  • 我明天会尝试手动注入[mybatis mapper]。 【mybatis mapper】只需要在加载数据库数据的具体方法中手动注入即可。

标签: java spring spring-boot shiro spring-boot-actuator


【解决方案1】:

正如我所料,这个问题是由 bean 的加载顺序引起的。

我在项目中使用了 Shiro。

Shiro 的验证方法使用 MyBatis 从数据库中读取数据。

我对MyBatis的Mapper文件使用了@Autowired,导致SpringBoot无法组装Actuator metrics相关的bean(不知道具体是什么原因)。

所以我通过手动组装禁用了 Mapper 文件的自动组装

代码如下:

public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String beanId) throws BeansException {
        return applicationContext.getBean(beanId);
    }
}

然后

StoreMapper userMapper = (UserMapper) SpringContextUtil.getBean("userMapper");
UserModel userModel = userMapper.findUserByName(name);

问题暂时可以解决。这只是权宜之计,但目前我没有更好的办法。

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 2021-06-23
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2016-12-20
    相关资源
    最近更新 更多