image
NightSnow

池浅王八多 遍地是大哥

Spring Boot Acturator  监控

NightSnow    2019-04-24 14:05

Spring Boot Acturator  配置  https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/

## security
#spring.security.basic.enabled=false
#spring.security.user.name=mdf-framework-admin
#spring.security.user.password=fadminpwd


## actuator
# actuator的访问路径
management.endpoints.web.base-path=/manage
#management.server.servlet.context-path=/monitor
# 管理的端口调整成1234
management.server.port=1234

# shutdown 可以关闭制定的端点
management.endpoint.shutdown.enabled=false
management.endpoints.enabled-by-default = false
management.endpoint.info.enabled = true
management.endpoint.health.enabled = true
#展示详情  可配 never|when-authorized|always
management.endpoint.health.show-details=always
#关闭默认配置的db检查
management.health.db.enabled=false
#关闭默认配置的redis检查
management.health.redis.enabled=false

# 加载所有的端点/默认只加载了 info / health
#management.endpoints.web.exposure.include=*  全部打开有安全漏洞问题 ;  暴漏接口需要此属性
#management.endpoints.web.exposure.exclude=*  搭配include 使用


## 自定义扩展
# 扩展info http:ip:1234/manage/info
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component
public class ApplicationInfoContributor implements InfoContributor {

    @Value("${spring.application.name}")
    private String applicationName;

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("Application-info",
                Collections.singletonMap("name", applicationName));
    }

}


# 扩展 health 
#实现接口方式

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class ApplicationHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        //dubbo 注册中心监控, redis ,各种db
        int errorCode = 0;//check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

#继承抽象父类的方式

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

@Component("okekek")
public class CustomHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        builder.up().withDetail("xxx", "xxxyyy");
    }
}

# 自定义端点 暴漏此端点需要设置 management.endpoints.web.exposure.include=* 

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

@Endpoint(id = "person")
@Component
public class PersonEndpoint {

    private final Map<String, Person> people = new ConcurrentHashMap<>();

    PersonEndpoint() {
        this.people.put("mike", new Person("Michael Redlich"));
        this.people.put("rowena", new Person("Rowena Redlich"));
        this.people.put("barry", new Person("Barry Burd"));
    }

    @ReadOperation
    public List<Person> getAll() {
        return new ArrayList<>(this.people.values());
    }

    @ReadOperation
    public Person getPerson(@Selector String name) {
        return this.people.get(name);
    }

    @WriteOperation
    public void addOrUpdatePerson(@Selector String name, @Selector String person) {
        this.people.put(name, new Person(person));
    }

    public static class Person {
        private String name;

        Person(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }
    }
}
 

Views: 2.6K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment