三十一、Springboot整合JMX(监控)

(一)JMX规范和应用场景介绍
  参见Java分布式应用学习笔记08JMX规范与常用的监控场景

(二)Springboot整合JMX
1、暴露MBean

@Component
@ManagedResource(
        objectName = "org.pc.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 +
                '}';
    }
}

2、调用MBean

@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;
    }
}

3、使用 jconsole 监控该 MBean

你可能感兴趣的:(Spring,Boot,从入门到放弃)