使用SpringBoot的Actuator实现带参数的自定义Endpoint - Go语言中文社区

使用SpringBoot的Actuator实现带参数的自定义Endpoint


最近要使用SpringBoot的Actuator功能特性,要自定义一个Endpoint,但是要根据外界参数来产生结果,以下是我的研究成果:

首先,当然是引入SpringBoot必须的依赖包,其中以下几个包需要提醒一下:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


第一步,定义一个Endpoint,本示例为RecalcEndpoint,如下:


import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "endpoints.recalc")
@Component
public class RecalcEndpoint extends AbstractEndpoint<Object> {

  public RecalcEndpoint() {
    super("recalc");
  }

  @Override
  public Object invoke() {
    throw new UnsupportedOperationException("不适用的操作。");
  }

}

在invoke中,设计为直接抛出异常,因为本要求,只是要使用Actuator的所有功能特性。


第二步,定义一个EndpointMvcAdapter,目的是为了解决请求时传参数的问题,本示例为RecalcMvcEndpoint,如下:

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.mvc.ActuatorMediaTypes;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.HypermediaDisabled;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@ConfigurationProperties(prefix = "endpoints.recalc")
public class RecalcMvcEndpoint extends EndpointMvcAdapter {
  
  private final RecalcEndpoint delegate;

  public RecalcMvcEndpoint(RecalcEndpoint delegate) {
    super(delegate);
    this.delegate = delegate;
  }

  @RequestMapping(method = RequestMethod.GET, //
      produces = { //
          ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON_VALUE, //
          MediaType.APPLICATION_JSON_VALUE //
      }, //
      value = "/{name:.*}" //
  )
  @ResponseBody
  @HypermediaDisabled
  public Object value(@PathVariable String name) {
    if (!this.delegate.isEnabled()) {
      return getDisabledResponse();
    }
    Map<String, Object> result = new HashMap<>();
    if (StringUtils.isBlank(name)) {
      return result;
    }
    Date myDate = null;
    try {
      myDate = DateUtils.parseDate(name, "yyyy-MM-dd");
    } catch (Exception e) {
      result.put("消息", MessageFormat.format( //
          "检查到有日期,但是日期不合法:yyyy-MM-dd->[{0}]", name));
      return result;
    }
    long start = System.currentTimeMillis();
    // TODO 你的业务逻辑
    result.put("运算结果", MessageFormat.format( //
        "业务逻辑计算完毕,时长[{0}]ms", System.currentTimeMillis() - start));
    return result;
  }

}

本示例,根据请求的参数,例如:http://localhost:8080/recalc/2018-03-21,来接收到请求的参数。

第三步,配置EndpointMvc,示例为EndpointWebMvcManagementConfig,如下:

import org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;

@ManagementContextConfiguration
public class EndpointWebMvcManagementConfig {

  @Bean
  @ConditionalOnBean(RecalcEndpoint.class)
  @ConditionalOnEnabledEndpoint("recalc")
  public RecalcMvcEndpoint recalcMvcEndpoint(RecalcEndpoint delegate) {
    return new RecalcMvcEndpoint(delegate);
  }

}

第四步,启动SpringBoot,访问地址:http://localhost:8080/recalc/2018-03-21,看看参数是否已经传进来了。然后再试一下,配置spring.recalc.enabled=false,重新启动,再看看效果。


第五步,如果您觉得我写的帮助到了您,请按照您的意愿随意小额打赏一下哦 :)



或者





版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/HunnyHu/article/details/79644264
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-17 02:44:52
  • 阅读 ( 828 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢