搜索整个某度的技术论坛,说到endpoint全部是千篇一律,而且都是停留在springboot1.0+的基础上介绍,而且全部是一样的文章,一点新意也没有,springboot2.0已经出来有一段时间了,我们有必要知道在springboot2.0+中怎么自定义一个Endpoint。闲话不说,直蹦主题。
依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
核心注解
Endpoint
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Endpoint {/*** The id of the endpoint (must follow {@link EndpointId} rules).* @return the id* @see EndpointId*/String id() default "";/*** If the endpoint should be enabled or disabled by default.* @return {@code true} if the endpoint is enabled by default*/boolean enableByDefault() default true;}
WebEndpoint
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Endpoint@FilteredEndpoint(WebEndpointFilter.class)public @interface WebEndpoint {/*** The id of the endpoint.* @return the id*/@AliasFor(annotation = Endpoint.class)String id();/*** If the endpoint should be enabled or disabled by default.* @return {@code true} if the endpoint is enabled by default*/@AliasFor(annotation = Endpoint.class)boolean enableByDefault() default true;}
JmxEndpoint
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Endpoint@FilteredEndpoint(JmxEndpointFilter.class)public @interface JmxEndpoint {/*** The id of the endpoint.* @return the id*/@AliasFor(annotation = Endpoint.class)String id() default "";/*** If the endpoint should be enabled or disabled by default.* @return {@code true} if the endpoint is enabled by default*/@AliasFor(annotation = Endpoint.class)boolean enableByDefault() default true;}
翻看上面的几个注解很清楚的知道Endpoint 是基础注解而且这些注解只能标注在类上面,类比Endpoint相当于Spring中的Component。WebEndpoint和 JmxEndpoint相当于Endpoint的派生类。下面通过一个表格介绍这几个注解的区别。
| 注解 | 描述 | 类比 |
|---|---|---|
| @Endpoint | 该注解的类可以通过http查看也可以通过jmx查看,他是在两个地方注册 | 相当于springmvc中的RestController和JMX中MBean的集合 |
| @JmxEndpoint | 该注解的类开放的是JMX接口 | 相当于JMX中的MBean |
| @WebEndpoint | 该注解的类开饭的是http接口 | 相当于mvc当中的RestController |
读操作ReadOperation
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ReadOperation {/*** The media type of the result of the operation.* @return the media type*/String[] produces() default {};}
写操作 WriteOperation
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface WriteOperation {/*** The media type of the result of the operation.* @return the media type*/String[] produces() default {};}
删除操作DeleteOperation
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface DeleteOperation {/*** The media type of the result of the operation.* @return the media type*/String[] produces() default {};}
从注解中可以看到,这些标注的是方法注解顾名思义这些是关于操作方面的注解,同样下面通过一个表格来解释一下。
| 描述 | 描述 | 类比 |
|---|---|---|
| @WriteOperation | http-POST请求 | 相当于mvc中的@PostMapping |
| @ReadOperation | http- GET请求 | 相当于mvc中的@GetMapping |
| @DeleteOpretation | http- DELETE请求 | 相当于mvc中的@DeleteMapping |
代码实战
我们知道Spring中的注解扫描是依靠Component注解,但是我们上面的注解Endpoint并不是注解 Component的派生。所以如果想要使用Endpoint类我们可以通过注解Bean 实例化(当然也可以使用Component交给spring管理)。下面通过代码在操作Endpoint,WebEndpoint , JmxEndpoint注解操作。
配置config配置类
@Configurationpublic class EndPointConfig {/*** 定义webEnpoint*/@Beanpublic WebEndpointCustom webEndpointCustom() {WebEndpointCustom webEndpointCustom = new WebEndpointCustom();return webEndpointCustom;}/*** 定义jmxEndpoint*/@Beanpublic JmxEndpointCustom jmxEndpointCustom() {JmxEndpointCustom jmxEndpointCustom = new JmxEndpointCustom();return jmxEndpointCustom;}/*** 定义基础的Endpoint* @return*/@Beanpublic EndpointCustom endpointCustom() {EndpointCustom endpointCustom = new EndpointCustom();return endpointCustom;}}
编写基础Endpoint
@Endpoint(id = "endpointCustom")public class EndpointCustom {@ReadOperationpublic String endpointCustomRead(String content) {return "你请求的内容: " + content;}@WriteOperationpublic String endpointCustomWrite(String content) {return "你写的内容: " + content;}@DeleteOperationpublic String endpointCustomDelete(String content) {return "你删除的内容: " + content;}}
测试
curl -X GET http://localhost:8080/actuator/endpointCustom?content=endpointGet你请求的内容: endpointGetcurl -X POST http://localhost:8080/actuator/endpointCustom?content=endpointPost你写的内容: endpointPostcurl -X DELETE http://localhost:8080/actuator/endpointCustom?content=endpointDELETE你删除的内容:endpointDELETE
WebEndpoint JmxEndpoint 和Endpoint使用相同,在此就不在赘述,只是WebEndpoint 只会通过Http访问,而JmxEndpoint是通过JMX查看而已。
