弹簧引导执行器以编程方式连接 jmx [英] spring boot actuator connect jmx programmatically

查看:56
本文介绍了弹簧引导执行器以编程方式连接 jmx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行使用我的 Spring Boot 2.0.1 应用程序的 shutdown 端点.为此,我只将 spring-boot-starter-actuator 添加到我的 Gradle 文件中,并在配置中启用了 shutdown 端点.

I'd like to use the shutdown endpoint of my Spring Boot 2.0.1 application from the command line. For that I have only added the spring-boot-starter-actuator to my Gradle file and enabled the shutdown endpoint in the configuration.

我还创建了一个非常简单的工具,尝试通过 JMX 连接到正在运行的应用程序.

I also created a very simple tool that tries to connect via JMX to the running application.

片段:

String url = "service:jmx:rmi:///jndi/rmi://127.0.01:<which port?>/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
JMXConnectorFactory.connect(serviceUrl, null); <-- KAPOW!

JMX 正在工作,因为我可以使用 jconsole 进行本地连接.我只是不知道如何以编程方式做到这一点.

JMX is working because I can use jconsole to connect locally. I just have no clue how to do it programmatically.

此处所述,任何其他显式设置端口的尝试均无效.有什么提示吗?

Any other attempts to explicitly set a port as mentioned here didn't work. Any hints?

推荐答案

启用 jolokia 而不是使用 RMI;那么你可以简单地

It's probably easier to enable jolokia rather than using RMI; then you can simply

curl http://localhost:8080/actuator/jolokia/exec/org.springframework.boot:type=Admin,name=SpringApplication/shutdown

编辑

如果您更喜欢使用 RMI,请参阅 Spring 框架 JMX 文档.

If you prefer to use RMI, refer to the Spring Framework JMX Documentation.

服务器应用:

@SpringBootApplication
public class So50392589Application {

    public static void main(String[] args) {
        SpringApplication.run(So50392589Application.class, args);
    }

    @Bean
    public RmiRegistryFactoryBean rmi() {
        RmiRegistryFactoryBean rmi = new RmiRegistryFactoryBean();
        rmi.setPort(1099);
        return rmi;
    }

    @Bean
    public ConnectorServerFactoryBean server() throws Exception {
        ConnectorServerFactoryBean fb = new ConnectorServerFactoryBean();
        fb.setObjectName("connector:name=rmi");
        fb.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector");
        return fb;
    }

}

客户端应用:

@SpringBootApplication
public class JmxClient {

    public static void main(String[] args) {
        new SpringApplicationBuilder(JmxClient.class)
            .web(WebApplicationType.NONE)
            .run(args);
    }

    @Bean
    public ApplicationRunner runner(MBeanServerConnection jmxConnector) {
        return args -> {
            jmxConnector.invoke(new ObjectName("org.springframework.boot:type=Admin,name=SpringApplication"),
                    "shutdown", new Object[0], new String[0]);
        };
    }

    @Bean
    public MBeanServerConnectionFactoryBean jmxConnector() throws Exception {
        MBeanServerConnectionFactoryBean jmx = new MBeanServerConnectionFactoryBean();
        jmx.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector");
        return jmx;
    }

}

这篇关于弹簧引导执行器以编程方式连接 jmx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆