为什么 Spring Boot 2.5.0 JMX Beans 没有显示在 JConsole 中

Why is Spring Boot 2.5.0 JMX Beans not showing in JConsole

Using Spring Boot 2.1.6.RELEASE for JMX Beans 工作正常,或者它们显示在 Jconsole 中,但在 2.5.0 中却没有。下面是我使用spring boot 2.5.0.

的简单应用

如果您想尝试版本 2.1.6.RELEASE,只需将 spring-boot-starter-parent 版本更改为 2.1.6.RELEASE,JMX Beans 就可以工作了。

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(DemoApplication.class, args);

        System.out.print("Press [ENTER] to quit:");
        System.in.read();
    }
}

JmxEndpoint.java

package com.example.demo;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;

@Component
@ManagedResource
public class JmxEndpoint {
    @ManagedOperation
    public void printMessage() {
    }
}

application.properties 文件中设置 spring.jmx.enabled=true 是解决方案。 他们在包括 2.5.0.

在内的较新版本中默认禁用了它

参考:https://github.com/spring-projects/spring-boot/issues/16090