springboot actuator

1、通过http监控
        添加jar包
               <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
            <dependency>
                <groupId>org.springframework.hateoas</groupId>
                <artifactId>spring-hateoas</artifactId>
                <version>0.19.0.RELEASE</version>
            </dependency>
     启动项目 访问
          http://localhost:8180/actuator  --获取actuator提供的主要功能
{
    "links": [
        {
            "rel": "self",
            "href": "http://localhost:8180/actuator"
        },
        {
            "rel": "autoconfig",
            "href": "http://localhost:8180/autoconfig"
        },
        {
            "rel": "dump",
            "href": "http://localhost:8180/dump"
        },
        {
            "rel": "mappings",
            "href": "http://localhost:8180/mappings"
        },
        {
            "rel": "metrics",
            "href": "http://localhost:8180/metrics"
        },
        {
            "rel": "health",
            "href": "http://localhost:8180/health"
        },
        {
            "rel": "trace",
            "href": "http://localhost:8180/trace"
        },
        {
            "rel": "beans",
            "href": "http://localhost:8180/beans"
        },
        {
            "rel": "loggers",
            "href": "http://localhost:8180/loggers"
        },
        {
            "rel": "auditevents",
            "href": "http://localhost:8180/auditevents"
        },
        {
            "rel": "configprops",
            "href": "http://localhost:8180/configprops"
        },
        {
            "rel": "heapdump",
            "href": "http://localhost:8180/heapdump"
        },
        {
            "rel": "env",
            "href": "http://localhost:8180/env"
        },
        {
            "rel": "info",
            "href": "http://localhost:8180/info"
        }
    ]
}


   访问http://localhost:8180/autoconfig---查看自动配置的情况
          返回内容

   Whitelabel Error Page

     This application has no explicit mapping for /error, so you are seeing this as a fallback.

     Mon Apr 24 14:35:37 CST 2017
     There was an unexpected error (type=Unauthorized, status=401).
     Full authentication is required to access this resource.
        需要身份认证 application.properties中添加management.security.enabled=false 访问即可
2、原理了解
  从上图看出 映射主要由EndpointHandlerMapping完成
http://www.cnblogs.com/davidwang456/p/6678982.html---原理介绍
3、自定义端点
       定义业务类:
                  @Service
public class StatusService {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
     自定义端点:
      public class StatusEndPoint extends AbstractEndpoint<String> implements ApplicationContextAware{
ApplicationContext context;
public StatusEndPoint() {
super("ff");
}
@Override
public String invoke() {
StatusService bean = context.getBean(StatusService.class);
return "The current Status is:" + bean.getStatus(); 
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
}
     注册端点
      @Bean
public Endpoint<String> status(){
Endpoint<String> status = new StatusEndPoint();
return status;
}

   应用端点:
     @Autowired
StatusService statusService;
@RequestMapping("/change")
public String changeStatus(String status){
statusService.setStatus(status);
return "ok";
}
         依次方位,即可看到效果
http://localhost:8081/ff ------http://localhost:8081/change?status=running ------ http://localhost:8081/ff



猜你喜欢

转载自blog.csdn.net/u011220648/article/details/70598027