随笔- 2038  评论- 174  文章- 4 

Springboot Actuator之九:actuator jmx endpoint

 

1、配置

endpoints.jmx.domain: myapp 
endpoints.jmx.uniqueNames: true
endpoints.auditevents.enabled: true

2、结果:

自定义Bean,通过JMX暴露

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为了测试,改变数据,

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

结果:

 

posted on 2019-08-06 17:54 duanxz 阅读(...) 评论(...) 编辑 收藏