Actuator 自定义端点
测试版本:Spring Boot 2.2.0.RELEASE
官网中提出
If you add a
@Bean
annotated with@Endpoint
, any methods annotated with@ReadOperation
,@WriteOperation
, or@DeleteOperation
are automatically exposed over JMX and, in a web application, over HTTP as well. Endpoints can be exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.You can also write technology-specific endpoints by using
@JmxEndpoint
or@WebEndpoint
. These endpoints are restricted to their respective technologies. For example,@WebEndpoint
is exposed only over HTTP and not over JMX.
其中就提到如何自定义端点,带有@Endpoint
注解的bean即可为自定义端点可通过JMX或者HTTP进行访问,@JmxEndpoint
和@WebEndpoint
注解的bean只能使用该有的方式进行访问。
创建测试端点
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
@WebEndpoint(id = "testEndpoint")
public class WebEndpointTest {
@ReadOperation
public Map<String, String> test() {
Map<String, String> result = new HashMap<String, String>();
result.put("name", "hello");
result.put("age", "18");
return result;
}
}
配置文件中记得公开端点(官网说明会自动暴露,可是并没有自动暴露,不知道哪里配置错了)
management.endpoints.web.exposure.include=testEndpoint
配置好即可通过 http://{url}:{port}/actuator/testEndpoint
进行访问
注解说明
端点注解
@Endpoint
可通过JMX和web应用程序访问(spring-boot-actuator
包中HealthEndpoint
类中可见例子)@JmxEndpoint
只能通过JMX访问@WebEndpoint
只能通过web访问@ServletEndpoint
Servlet端点@RestControllerEndpoint
控制器端点(spring-cloud-gateway
包中GatewayControllerEndpoint
可见例子)
方法注解
运作方式 | HTTP方法 |
---|---|
@ReadOperation | GET |
@WriteOperation | POST |
@DeleteOperation | DELETE |
在方法上配置注解即可通过对应的方式进行访问,访问地址一般是
/actuator/{EndpointID}
@RestControllerEndpoint
注解中还是用的SpringMVC的标准注释例如@RequestMapping
HTTP监控管理
自定义端点管理路径
修改/actuator路径为/base
management.endpoints.web.base-path=/base
那么修改后之前的/actuator/{ID}
就变成了/base/{ID}
/actuator/info
/base/info
注意这句话
Unless the management port has been configured to expose endpoints by using a different HTTP port,
management.endpoints.web.base-path
is relative toserver.servlet.context-path
. Ifmanagement.server.port
is configured,management.endpoints.web.base-path
is relative tomanagement.server.servlet.context-path
.
例如:修改actuator/health
为 /healthcheck
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
自定义端口
management.server.port=8081
更详细内容可查看官网