SpingBoot 整合 Actuator

上一篇我们已经整合了springboot和h2数据库,并建立一个服务生产者和消费者

我们在此基础上继续整合Actuator,Actuator为项目监控断点,可以了解应用程序的运行状况

整合过程非常简单,只需要加上一个依赖就可以了

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

加上这个依赖就算整合好了

此时我们可以了解当前项目的健康状况

1./health端点

访问http://localhost:8010/health,可以获得如下结果

{status: "UP"}

此时,可以展现当前应用的健康状况。

UP   运行正常

默认是展示概要情况,要显示详情,需要在配置文件里面添加如下信息

management:
  security:
    enabled: false

此时再次访问,可以得到下面如下结果

{
    "db": {
        "database": "H2",
        "hello": 1,
        "status": "UP"
    },
    "description": "Composite Discovery Client",
    "discoveryComposite": {
        "description": "Composite Discovery Client",
        "discoveryClient": {
            "description": "Composite Discovery Client",
            "services": [
                "microservice-simple-provider-user",
                "microservice-simple-consumer-movie"
            ],
            "status": "UP"
        },
        "eureka": {
            "applications": {
                "MICROSERVICE-SIMPLE-CONSUMER-MOVIE": 1,
                "MICROSERVICE-SIMPLE-PROVIDER-USER": 1
            },
            "description": "Remote status from Eureka server",
            "status": "UP"
        },
        "status": "UP"
    },
    "diskSpace": {
        "free": 36403810304,
        "status": "UP",
        "threshold": 10485760,
        "total": 147346944000
    },
    "refreshScope": {
        "status": "UP"
    },
    "status": "UP"
}

由上面可知,/health的本质,是检查springboot应用程序的资源,判断是否正常

2./info端点

直接访问  http://localhost:8010/info,会得到如下内容

{}

由此可知,info端点并没有返回任何数据给我们。  我们可以通过info.* 属性来自定义info端点公开的数据,例如:

info:
  app:
    name: @project.artifactId@
    encoding: @project.build.sourceEncoding@
    java:
      source: @java.version@
      target: @java.version@

这样,重启后,再次访问 http://localhost:8010/info,就会得到类似如下的结果

{
    "app": {
        "encoding": "UTF-8",
        "java": {
            "source": "1.8.0_152",
            "target": "1.8.0_152"
        },
        "name": "microservice-simple-provider-user"
    }
}

由此可知,info端点返回了项目名称,编码,java版本等信息

Actuator端点很多,感兴趣的可以参考springboot文档自行学习,在此不做赘述

猜你喜欢

转载自www.cnblogs.com/codessuperman/p/12107148.html