【问题标题】:how could access actuator endpoints in non-web application如何在非 Web 应用程序中访问执行器端点
【发布时间】:2015-12-31 13:00:49
【问题描述】:

这不是一个web项目,但我还是在pom中添加spring-boot-starter-actuator,并指定management.port=8081,然后在CommandLineRunner的run方法的最后一个,有以下代码,

System.in.read();

当开始这个项目时,它会坚持下去。然后我将访问http://localhost:8081/configprops,但显示以下消息:

This webpage is not available

那么如何在非 Web 应用程序中访问执行器端点? 顺便说一句,我为什么要这样做,因为在启动我的项目时,它无法成功加载 yml 数据并导致 NPE。所以我想访问/configprops查看。

我的 yml 是这样的:

spring:
    profiles.active: default

---

spring:
    profiles: default

user: 
   foo: 
      aaa: aaa@foo.com
---

spring:
   profiles: test        

user: 
   foo: 
       aaa: aaa@foo.com
       bbb: bbb@foo.com

@ConfigurationProperties(prefix="user", locations="user.yml")
public class UserConfig {
    private HashMap<String, String> foo;    
}

【问题讨论】:

  • 您甚至可以在非 Web 应用程序中使用执行器。我有许多基于 CommandLineRunner 的 Spring Boot 应用程序,它们只启动一个嵌入式 Tomcat 来为执行器端点提供服务
  • @Marged 你能解释一下如何从 CommandLineRunner 启动一个嵌入式 Tomcat 来服务 /health 端点吗?

标签: spring-boot


【解决方案1】:

在非 Web 应用程序中,您可以使用 JMX 访问 Actuator 端点。使用 JConsole 或 JVisualVM 来读取 MBean。有关详细信息,请参阅 Spring Boot 文档:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-jmx

【讨论】:

  • 谢谢!但在非网络项目中,您仍然必须通过某种方法让进程保持下去,例如Thread.sleep()System.in.read()
  • 另外通过jmx查询,确实存在其他方式,例如System.getProperty("user.foo")可以编程输出
【解决方案2】:

感谢@dunni 在 jconsole MBeans 选项卡中您可以看到

当点击 getData 我得到一个列表,一行告诉我

userConfig={prefix=user, properties={foo=null}}

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多