最近Spring boot 2.0出来了,那么2.0最大的变化是哪些呢.
1、首先监控跟踪管理actuator 已经跟原来差距很大了.
首先原来很多的跟踪都是系统自带的,系统启动的时候就启动
现在的Spring boot 需要配置
management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env,beans
现在的Spring boot 2.0 需要根据配置来启动,同时排查不需要的启动项目
参考:
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#production-ready-endpoints-exposing-endpoints
其中我们可以自定义endpoints
package org.redlich.actuator; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; 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 person) { return this.people.get(person); } @WriteOperation public void addOrUpdatePerson(@Selector String name, 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; } } }
`localhost:8080/application` will display all the endpoints. `localhost:8080/application/person` will display all the names from the user-defined endpoint. `localhost:8080/application/person/mike` will display only my name.
如果你想把一些信息定义在info里面那么需要
import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component; import java.util.Collections; @Component class ExampleInfoContributor implements InfoContributor { @Override public void contribute(Info.Builder builder) { builder.withDetail("example", Collections.singletonMap("key", "value")); } }
如果需要自定义在health 里面那么
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } }