一、配置web
endpoints.jmx.domain: myapp endpoints.jmx.uniqueNames: true endpoints.auditevents.enabled: true
二、結果:spring
自定義Bean,經過JMX暴露app
package com.dxz.actuator; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.stereotype.Component; @Component @ManagedResource(objectName = "com.dxz.jmx:type=SimpleBean", description = "這裏是描述") public class SimpleBean { private long id; private String name; private int age; /** * 暴露屬性 */ @ManagedAttribute(description = "這是屬性id") public long getId() { return id; } public void setId(long id) { this.id = id; } /** * 暴露屬性 */ @ManagedAttribute(description = "這是屬性name") public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 暴露屬性 */ @ManagedAttribute(description = "這是屬性age") public int getAge() { return age; } public void setAge(int age) { this.age = age; } /** * 暴露方法 */ @ManagedOperation(description = "這裏是操做") public String display() { return this.toString(); } @Override public String toString() { return "SimpleBean{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
下面的controller爲了測試,改變數據,dom
package com.dxz.actuator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class JmxController { @Autowired private SimpleBean simpleBean; @GetMapping("/jmx") public SimpleBean simpleBean(@RequestParam(required = false) Long id, @RequestParam(required = false) String name, @RequestParam(required = false) Integer age) { if (id != null) { simpleBean.setId(id); } if (name != null) { simpleBean.setName(name); } if (age != null) { simpleBean.setAge(age); } return simpleBean; } }
結果:ide