diff --git a/README.md b/README.md index f466ef4..fbd4ac8 100644 --- a/README.md +++ b/README.md @@ -41,23 +41,24 @@ spring-cloud:Finchley.SR2 ## 2. spring-boot samples -| samples | 描述 | 官方文档 | -| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | -| [spring-boot-base](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-base) | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)
[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) | -| [spring-boot-yml-profile](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-yml-profile) | yml 语法和多配置切换 | [Using YAML Instead of Properties](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml) | -| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) | -| [spring-boot-servlet](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-servlet) | 整合servlet 3.0 | [Embedded Servlet Container Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-embedded-container) | -| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 内置容器整合 jsp | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) | -| [spring-boot-data-jpa](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot data jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) | -| [spring-boot-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mybatis) | spring-boot 整合 mybatis
boot 2.x 内置HikariDataSources数据源的配置 | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)
[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) | -| [spring-boot-druid-mybtais](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-druid-mybatis) | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)
[druid-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | -| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | -| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | -| [spring-boot-memcached](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-memcached) | spring-boot 整合 memcached | [Xmemcached](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) | -| [spring-boot-rabbitmq](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-rabbitmq) | spring-boot 整合 rabbitmq | [RabbitMQ support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-rabbitmq) | -| [spring-boot-dubbo](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-dubbo) | spring-boot 整合 dubbo | [Dubbo ](http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) | -| [spring-boot-websocket](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-websocket) | spring-boot 整合 websocket | [Using @ServerEndpoint](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-create-websocket-endpoints-using-serverendpoint) | -| [spring-boot-kafka](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-kafka) | spring-boot 整合 kafka | [Apache Kafka Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-kafka) | +| samples | 描述 | 官方文档 | +| ------------------------------------------------------------ | ------------------------------------------ | ------------------------------------------------------------ | +| [spring-boot-base](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-base) | spring-boot 基础 | [spring boot 官方文档](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/)
[spring boot 中文官方文档](https://www.breakyizhan.com/springboot/3028.html) | +| [spring-boot-yml-profile](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-yml-profile) | yml 语法和多配置切换 | [Using YAML Instead of Properties](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-external-config-yaml) | +| [spring-boot-tomcat](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-tomcat) | spring-boot 整合外部容器(tomcat) | [Use Another Web Server](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-use-another-web-server) | +| [spring-boot-servlet](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-servlet) | spring boot 整合servlet 3.0 | [Embedded Servlet Container Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-embedded-container) | +| [spring-boot-jsp](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot 整合 jsp(内置容器) | [JSP Limitations](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations) | +| [spring-boot-data-jpa](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-jsp) | spring-boot data jpa 的使用 | [Spring Data JPA](https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/) | +| [spring-boot-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mybatis) | spring-boot+HikariDataSources 整合 mybatis | [Mybatis-Spring](http://www.mybatis.org/spring/zh/index.html)
[Mybatis-Spring-Boot-Autoconfigure](http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/) | +| [spring-boot-druid-mybtais](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-druid-mybatis) | spring-boot 整合druid、mybatis | [Alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)
[druid-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | +| [spring-boot-redis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-redis) | spring-boot 整合 redis | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | +| [spring-boot-mongodb](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-mongodb) | spring-boot 整合 mongodb | [Working with NoSQL Technologies](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-nosql) | +| [spring-boot-memcached](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-memcached) | spring-boot 整合 memcached | [Xmemcached](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) | +| [spring-boot-rabbitmq](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-rabbitmq) | spring-boot 整合 rabbitmq | [RabbitMQ support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-rabbitmq) | +| [spring-boot-dubbo](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-dubbo) | spring-boot 整合 dubbo | [Dubbo ](http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) | +| [spring-boot-websocket](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-websocket) | spring-boot 整合 websocket | [Using @ServerEndpoint](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#howto-create-websocket-endpoints-using-serverendpoint) | +| [spring-boot-kafka](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-kafka) | spring-boot 整合 kafka | [Apache Kafka Support](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-kafka) | +| spring-boot-actuator | actuator + Hyperic SIGAR 应用信息监控 | [Spring Boot Actuator](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#production-ready) | 更多的用例可参阅 [spring-boot 官方samples ](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples) diff --git a/pictures/actuator-customEndPoint.png b/pictures/actuator-customEndPoint.png new file mode 100644 index 0000000..a21abb0 Binary files /dev/null and b/pictures/actuator-customEndPoint.png differ diff --git a/pictures/actuator-health-up.png b/pictures/actuator-health-up.png new file mode 100644 index 0000000..aaefcc5 Binary files /dev/null and b/pictures/actuator-health-up.png differ diff --git a/pictures/actuator-heath-503.png b/pictures/actuator-heath-503.png new file mode 100644 index 0000000..09773eb Binary files /dev/null and b/pictures/actuator-heath-503.png differ diff --git a/pictures/health-fatal-200.png b/pictures/health-fatal-200.png new file mode 100644 index 0000000..b9e487e Binary files /dev/null and b/pictures/health-fatal-200.png differ diff --git a/pictures/health.png b/pictures/health.png new file mode 100644 index 0000000..e3ec119 Binary files /dev/null and b/pictures/health.png differ diff --git a/pictures/sigar.png b/pictures/sigar.png new file mode 100644 index 0000000..e22309e Binary files /dev/null and b/pictures/sigar.png differ diff --git a/pictures/spring-boot-actuator.png b/pictures/spring-boot-actuator.png new file mode 100644 index 0000000..05f2795 Binary files /dev/null and b/pictures/spring-boot-actuator.png differ diff --git a/spring-boot/spring-boot-actuator/README.md b/spring-boot/spring-boot-actuator/README.md new file mode 100644 index 0000000..2267f81 --- /dev/null +++ b/spring-boot/spring-boot-actuator/README.md @@ -0,0 +1,314 @@ +# spring boot actuator + +## 一、用例涉及到的概念综述 + +### 1.1 端点 + +执行器端点(endpoints)可用于监控应用及与应用进行交互,Spring Boot包含很多内置的端点,你也可以添加自己的。例如,health端点提供了应用的基本健康信息。 端点暴露的方式取决于你采用的技术类型,大部分应用选择HTTP监控,端点的ID映射到一个URL。例如,health端点默认映射到/health。 + +下面的端点都是可用的: + +| ID | 描述 | 是否敏感 | +| ----------- | ------------------------------------------------------------ | -------- | +| actuator | 为其他端点提供基于超文本的导航页面,需要添加Spring HATEOAS依赖 | true | +| autoconfig | 显示一个自动配置类的报告,该报告展示所有自动配置候选者及它们被应用或未被应用的原因 | true | +| beans | 显示一个应用中所有Spring Beans的完整列表 | true | +| configprops | 显示一个所有@ConfigurationProperties的集合列表 | true | +| dump | 执行一个线程转储 | true | +| env | 暴露来自Spring ConfigurableEnvironment的属性 | true | +| flyway | 显示数据库迁移路径,如果有的话 | true | +| health | 展示应用的健康信息(当使用一个未认证连接访问时显示一个简单的'status',使用认证连接访问则显示全部信息详情) | false | +| info | 显示任意的应用信息 | false | +| liquibase | 展示任何Liquibase数据库迁移路径,如果有的话 | true | +| metrics | 展示当前应用的'metrics'信息 | true | +| mappings | 显示一个所有@RequestMapping路径的集合列表 | true | +| shutdown | 允许应用以优雅的方式关闭(默认情况下不启用) | true | +| trace | 显示trace信息(默认为最新的100条HTTP请求) | true | + +如果使用Spring MVC,你还可以使用以下端点: + +| ID | 描述 | 是否敏感 | +| -------- | ------------------------------------------------------------ | -------- | +| docs | 展示Actuator的文档,包括示例请求和响应,需添加spring-boot-actuator-docs依赖 | false | +| heapdump | 返回一个GZip压缩的hprof堆转储文件 | true | +| jolokia | 通过HTTP暴露JMX beans(依赖Jolokia) | true | +| logfile | 返回日志文件内容(如果设置logging.file或logging.path属性),支持使用HTTP Range头接收日志文件内容的部分信息 | | + +注:根据端点暴露的方式,sensitive属性可用做安全提示,例如,在使用HTTP访问敏感(sensitive)端点时需要提供用户名/密码(如果没有启用web安全,可能会简化为禁止访问该端点)。 + + + +### 1.2 启用端点 + +默认情况下,除了以外的所有端点shutdown都已启用。要配置端点的启用,请使用其management.endpoint..enabled属性。以下示例启用shutdown端点: + +```properties +management.endpoint.shutdown.enabled = true +``` + + + +### 1.3 暴露端点 + +由于端点可能包含敏感信息,因此应仔细考虑何时公开它们。下表显示了内置端点的默认曝光情况: + +| ID | JMX | Web | +| -------------- | ----- | ---- | +| auditevents | 是 | 没有 | +| beans | 是 | 没有 | +| conditions | 是 | 没有 | +| configprops | 是 | 没有 | +| env | 是 | 没有 | +| flyway | 是 | 没有 | +| health | 是 | 是 | +| heapdump | N / A | 没有 | +| httptrace | 是 | 没有 | +| info | 是 | 是 | +| jolokia | N / A | 没有 | +| logfile | N / A | 没有 | +| loggers | 是 | 没有 | +| liquibase | 是 | 没有 | +| metrics | 是 | 没有 | +| mappings | 是 | 没有 | +| prometheus | N / A | 没有 | +| scheduledtasks | 是 | 没有 | +| sessions | 是 | 没有 | +| shutdown | 是 | 没有 | +| threaddump | 是 | 没有 | + +**可以选择是否暴露端点(include)或者排除端点(exclude),其中排除优先于暴露:** + +| 属性 | 默认 | +| ----------------------------------------- | ------------ | +| management.endpoints.jmx.exposure.exclude | | +| management.endpoints.jmx.exposure.include | * | +| management.endpoints.web.exposure.exclude | | +| management.endpoints.web.exposure.include | info, health | + + + +### 1.4 健康检查信息 + +您可以使用健康信息来检查正在运行的应用程序的状态。health端点公开的信息取决于management.endpoint.health.show-details可以使用以下值之一配置的属性: + +| 名称 | 描述 | +| --------------- | ------------------------------------------------------------ | +| never | 细节永远不会显示。 | +| when-authorized | 详细信息仅向授权用户显示。授权角色可以使用配置 management.endpoint.health.roles。 | +| always | 详细信息显示给所有用户。 | + + + +## 二、项目说明 + +#### 1.1 项目结构说明 + +1. CustomHealthIndicator 自定义健康指标; +2. CustomHealthAggregator:自定义健康聚合规则; +3. CustomEndPoint:自定义端点。 + +![spring-boot-actuator](D:\spring-samples-for-all\pictures\spring-boot-actuator.png) + +#### 1.2 主要依赖 + +```xml + + org.springframework.boot + spring-boot-starter-actuator + +``` + +#### 1.3 项目配置 + +```yaml +management: + endpoints: + web: + exposure: + # 这里用* 代表暴露所有端点只是为了观察效果,实际中按照需进行端点暴露 + include: "*" + endpoint: + health: + # 详细信息显示给所有用户。 + show-details: always + health: + status: + http-mapping: + # 自定义健康检查返回状态码对应的http状态码 + FATAL: 503 +``` + +#### 1.4 查看监控状态 + +导入actuator 的 start 并进行配置后,访问http://127.0.0.1:8080/actuator/health就可以看到对应的项目监控状态。 + +![health](D:\spring-samples-for-all\pictures\health.png) + +需要注意的是这里的监控状态根据实际项目所用到的技术不同而不同。因为以下HealthIndicators情况在适当时由Spring Boot自动配置的: + +| 名称 | 描述 | +| ------------------------------------------------------------ | -------------------------------- | +| [CassandraHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicator.java) | 检查Cassandra数据库是否启动。 | +| [DiskSpaceHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java) | 检查磁盘空间不足。 | +| [DataSourceHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/jdbc/DataSourceHealthIndicator.java) | 检查是否可以获得连接DataSource。 | +| [ElasticsearchHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchHealthIndicator.java) | 检查Elasticsearch集群是否启动。 | +| [InfluxDbHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/influx/InfluxDbHealthIndicator.java) | 检查InfluxDB服务器是否启动。 | +| [JmsHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/jms/JmsHealthIndicator.java) | 检查JMS代理是否启动。 | +| [MailHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mail/MailHealthIndicator.java) | 检查邮件服务器是否启动。 | +| [MongoHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/mongo/MongoHealthIndicator.java) | 检查Mongo数据库是否启动。 | +| [Neo4jHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/neo4j/Neo4jHealthIndicator.java) | 检查Neo4j服务器是否启动。 | +| [RabbitHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/amqp/RabbitHealthIndicator.java) | 检查Rabbit服务器是否启动。 | +| [RedisHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisHealthIndicator.java) | 检查Redis服务器是否启动。 | +| [SolrHealthIndicator](https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/solr/SolrHealthIndicator.java) | 检查Solr服务器是否已启动。 | + + + +## 三、自定义健康检查指标 + +```java +/** + * @author : heibaiying + * @description : 自定义健康检查指标 + */ +@Component +public class CustomHealthIndicator implements HealthIndicator { + + @Override + public Health health() { + double random = Math.random(); + // 这里用随机数模拟健康检查的结果 + if (random > 0.5) { + return Health.status("FATAL").withDetail("error code", "某健康专项检查失败").build(); + } else { + return Health.up().withDetail("success code", "自定义检查一切正常").build(); + } + + } +} +``` + +自定义检查通过的情况下: + +![actuator-health-up](D:\spring-samples-for-all\pictures\actuator-health-up.png) + +自定义检查失败的情况: + +![health-fatal-200](D:\spring-samples-for-all\pictures\health-fatal-200.png) + + + +这里我们可以看到自定义检查不论是否通过都不会影响整体的status,两种情况下都是status都是“up”。如果我们想通过自定义的检查检查去影响最终的检查结果,比如我们健康检查针对的是支付业务,在支付业务的不可用的情况下,我们就认为整个服务是不可用的。这个时候就需要实现自定义实现健康状态的聚合。 + + + +## 四、自定义健康状态聚合规则 + +```java +/** + * @author : heibaiying + * @description : 对所有的自定义健康指标进行聚合,按照自定义规则返回总和健康状态 + */ +@Component +public class CustomHealthAggregator implements HealthAggregator { + + @Override + public Health aggregate(Map healths) { + for (Health health : healths.values()) { + // 聚合规则可以自定义,这里假设我们自定义的监控状态中有一项FATAL,就认为整个服务都是不可用的,否则认为整个服务是可用的 + if (health.getStatus().getCode().equals("FATAL")) { + return Health.status("FATAL").withDetail("error code", "综合判断后服务宕机").build(); + } + } + return Health.up().build(); + } +} +``` + +这是当我们自定义健康检查不通过时候的结果如下: + +![actuator-heath-503](D:\spring-samples-for-all\pictures\actuator-heath-503.png) + +这里需要注意的是返回我们自定义的聚合状态的时候,状态码也变成了503,这是我们在配置文件中进行定义的: + +```properties +management.health.status.http-mapping.FATAL = 503 +``` + +下表显示了内置状态的默认状态映射: + +| Status | Mapping | +| -------------- | -------------------------------------------- | +| DOWN | SERVICE_UNAVAILABLE (503) | +| OUT_OF_SERVICE | SERVICE_UNAVAILABLE (503) | +| UP | No mapping by default, so http status is 200 | +| UNKNOWN | No mapping by default, so http status is 200 | + + + +## 五、@Endpoint自定义端点 + +#### 5.1 自定义端点 + +spring boot 支持使用@Endpoint来自定义端点暴露应用信息。这里我们采用第三方sigar来暴露服务所在硬件的监控信息。 + +Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件。Sigar.jar的底层是用C语言编写的,它通过本地方法来调用操作系统API来获取系统相关数据 [jar包下载地址](https://sourceforge.net/projects/sigar/)。 + +Sigar为不同平台提供了不同的库文件,下载后需要将库文件放到服务所在主机的对应位置: + +- Windows下配置:根据自己的操作系统版本选择sigar-amd64-winnt.dll或sigar-x86-winnt.dll拷贝到C:\Windows\System32中 + +- Linux下配置:将libsigar-amd64-linux.so或libsigar-x86-linux.so拷贝到/usr/lib64或/lib64或/lib或/usr/lib目录下,如果不起作用,还需要sudochmod 744修改libsigar-amd64-linux.so文件权限 + +```java +@Endpoint(id = "customEndPoint") +@Component +public class CustomEndPoint { + + @ReadOperation + public Map getCupInfo() throws SigarException { + + Map cupInfoMap = new LinkedHashMap<>(); + + Sigar sigar = new Sigar(); + + CpuInfo infoList[] = sigar.getCpuInfoList(); + CpuPerc[] cpuList = sigar.getCpuPercList(); + + for (int i = 0; i < infoList.length; i++) { + CpuInfo info = infoList[i]; + cupInfoMap.put("CPU " + i + " 的总量MHz", info.getMhz()); // CPU的总量MHz + cupInfoMap.put("CPU " + i + " 生产商", info.getVendor()); // 获得CPU的生产商,如:Intel + cupInfoMap.put("CPU " + i + " 类别", info.getModel()); // 获得CPU的类别,如:Core + cupInfoMap.put("CPU " + i + " 缓存数量", info.getCacheSize()); // 缓冲存储器数量 + cupInfoMap.put("CPU " + i + " 用户使用率", CpuPerc.format(cpuList[i].getUser())); // 用户使用率 + cupInfoMap.put("CPU " + i + " 系统使用率", CpuPerc.format(cpuList[i].getSys())); // 系统使用率 + cupInfoMap.put("CPU " + i + " 当前等待率", CpuPerc.format(cpuList[i].getWait())); // 当前等待率 + cupInfoMap.put("CPU " + i + " 当前错误率", CpuPerc.format(cpuList[i].getNice())); // 当前错误率 + cupInfoMap.put("CPU " + i + " 当前空闲率", CpuPerc.format(cpuList[i].getIdle())); // 当前空闲率 + cupInfoMap.put("CPU " + i + " 总的使用率", CpuPerc.format(cpuList[i].getCombined()));// 总的使用率 + } + return cupInfoMap; + } + +} +``` + +其中可用的方法注解由http操作决定: + +| operation | HTTP方法 | +| ---------------- | -------- | +| @ReadOperation | GET | +| @WriteOperation | POST | +| @DeleteOperation | DELETE | + +#### 5.2 访问自定义端点http://127.0.0.1:8080/actuator/customEndPoint + +![actuator-customEndPoint](D:\spring-samples-for-all\pictures\actuator-customEndPoint.png) + + + +关于 Sigar 的 更多监控参数可以参考博客:[java读取计算机CPU、内存等信息(Sigar使用)](https://blog.csdn.net/wudiazu/article/details/73829324) + +Sigar 下载包中也提供了各种参数的参考用例: + +![sigar](D:\spring-samples-for-all\pictures\sigar.png) \ No newline at end of file diff --git a/spring-boot/spring-boot-actuator/pom.xml b/spring-boot/spring-boot-actuator/pom.xml new file mode 100644 index 0000000..0601065 --- /dev/null +++ b/spring-boot/spring-boot-actuator/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + spring-boot-actuator + 0.0.1-SNAPSHOT + spring-boot-actuator + actuator project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.hyperic + sigar-dist + 1.6.4-SNAPSHOT + system + ${project.basedir}/src/main/resources/lib/sigar.jar + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + diff --git a/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/SpringBootActuatorApplication.java b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/SpringBootActuatorApplication.java new file mode 100644 index 0000000..3d89b63 --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/SpringBootActuatorApplication.java @@ -0,0 +1,14 @@ +package com.heibaiying.actuator; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootActuatorApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootActuatorApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/aggregator/CustomHealthAggregator.java b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/aggregator/CustomHealthAggregator.java new file mode 100644 index 0000000..34c5d3f --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/aggregator/CustomHealthAggregator.java @@ -0,0 +1,26 @@ +package com.heibaiying.actuator.aggregator; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthAggregator; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author : heibaiying + * @description : 对所有的自定义健康指标进行聚合,按照自定义规则返回总和健康状态 + */ +@Component +public class CustomHealthAggregator implements HealthAggregator { + + @Override + public Health aggregate(Map healths) { + for (Health health : healths.values()) { + // 聚合规则可以自定义,这里假设我们自定义的监控状态中有一项FATAL,就认为整个服务都是不可用的,否则认为整个服务是可用的 + if (health.getStatus().getCode().equals("FATAL")) { + return Health.status("FATAL").withDetail("error code", "综合判断后服务宕机").build(); + } + } + return Health.up().build(); + } +} diff --git a/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/endpoint/CustomEndPoint.java b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/endpoint/CustomEndPoint.java new file mode 100644 index 0000000..8b247c8 --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/endpoint/CustomEndPoint.java @@ -0,0 +1,48 @@ +package com.heibaiying.actuator.endpoint; + +import org.hyperic.sigar.CpuInfo; +import org.hyperic.sigar.CpuPerc; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.stereotype.Component; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * @author : heibaiying + * @description : 自定义端点 + */ +@Endpoint(id = "customEndPoint") +@Component +public class CustomEndPoint { + + @ReadOperation + public Map getCupInfo() throws SigarException { + + Map cupInfoMap = new LinkedHashMap<>(); + + Sigar sigar = new Sigar(); + + CpuInfo infoList[] = sigar.getCpuInfoList(); + CpuPerc[] cpuList = sigar.getCpuPercList(); + + for (int i = 0; i < infoList.length; i++) { + CpuInfo info = infoList[i]; + cupInfoMap.put("CPU " + i + " 的总量MHz", info.getMhz()); // CPU的总量MHz + cupInfoMap.put("CPU " + i + " 生产商", info.getVendor()); // 获得CPU的生产商,如:Intel + cupInfoMap.put("CPU " + i + " 类别", info.getModel()); // 获得CPU的类别,如:Core + cupInfoMap.put("CPU " + i + " 缓存数量", info.getCacheSize()); // 缓冲存储器数量 + cupInfoMap.put("CPU " + i + " 用户使用率", CpuPerc.format(cpuList[i].getUser())); // 用户使用率 + cupInfoMap.put("CPU " + i + " 系统使用率", CpuPerc.format(cpuList[i].getSys())); // 系统使用率 + cupInfoMap.put("CPU " + i + " 当前等待率", CpuPerc.format(cpuList[i].getWait())); // 当前等待率 + cupInfoMap.put("CPU " + i + " 当前错误率", CpuPerc.format(cpuList[i].getNice())); // 当前错误率 + cupInfoMap.put("CPU " + i + " 当前空闲率", CpuPerc.format(cpuList[i].getIdle())); // 当前空闲率 + cupInfoMap.put("CPU " + i + " 总的使用率", CpuPerc.format(cpuList[i].getCombined()));// 总的使用率 + } + return cupInfoMap; + } + +} diff --git a/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/indicator/CustomHealthIndicator.java b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/indicator/CustomHealthIndicator.java new file mode 100644 index 0000000..1855c0a --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/main/java/com/heibaiying/actuator/indicator/CustomHealthIndicator.java @@ -0,0 +1,28 @@ +package com.heibaiying.actuator.indicator; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthAggregator; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author : heibaiying + * @description : 自定义健康检查指标 + */ +@Component +public class CustomHealthIndicator implements HealthIndicator { + + @Override + public Health health() { + double random = Math.random(); + // 这里用随机数模拟健康检查的结果 + if (random > 0.5) { + return Health.status("FATAL").withDetail("error code", "某健康专项检查失败").build(); + } else { + return Health.up().withDetail("success code", "自定义检查一切正常").build(); + } + + } +} diff --git a/spring-boot/spring-boot-actuator/src/main/resources/application.yml b/spring-boot/spring-boot-actuator/src/main/resources/application.yml new file mode 100644 index 0000000..8451d6c --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/main/resources/application.yml @@ -0,0 +1,15 @@ +management: + endpoints: + web: + exposure: + # 这里用* 代表暴露所有端点只是为了观察效果,实际中按照需进行端点暴露 + include: "*" + endpoint: + health: + # 详细信息显示给所有用户。 + show-details: always + health: + status: + http-mapping: + # 自定义健康检查返回状态码对应的http状态码 + FATAL: 503 \ No newline at end of file diff --git a/spring-boot/spring-boot-actuator/src/test/java/com/heibaiying/actuator/SpringBootActuatorApplicationTests.java b/spring-boot/spring-boot-actuator/src/test/java/com/heibaiying/actuator/SpringBootActuatorApplicationTests.java new file mode 100644 index 0000000..d749972 --- /dev/null +++ b/spring-boot/spring-boot-actuator/src/test/java/com/heibaiying/actuator/SpringBootActuatorApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.actuator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootActuatorApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring-cloud/.gitignore b/spring-cloud/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/spring-cloud/eureka/eureka-client/pom.xml b/spring-cloud/eureka/eureka-client/pom.xml new file mode 100644 index 0000000..b831943 --- /dev/null +++ b/spring-cloud/eureka/eureka-client/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying.eureka + eureka-client + 0.0.1-SNAPSHOT + eureka-client + eureka-client project for Spring Boot + + + 1.8 + Greenwich.RC2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + + + diff --git a/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/EurekaClientApplication.java b/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/EurekaClientApplication.java new file mode 100644 index 0000000..7495076 --- /dev/null +++ b/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/EurekaClientApplication.java @@ -0,0 +1,16 @@ +package com.heibaiying.eurekaclient; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class EurekaClientApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaClientApplication.class, args); + } + +} + diff --git a/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/controller/InfoController.java b/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/controller/InfoController.java new file mode 100644 index 0000000..021451e --- /dev/null +++ b/spring-cloud/eureka/eureka-client/src/main/java/com/heibaiying/eurekaclient/controller/InfoController.java @@ -0,0 +1,22 @@ +package com.heibaiying.eurekaclient.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author : heibaiying + */ + +@RestController +public class InfoController { + + @Autowired + private DiscoveryClient discoveryClient; + + @GetMapping("info") + public String info() { + return discoveryClient.description(); + } +} diff --git a/spring-cloud/eureka/eureka-client/src/main/resources/application.yml b/spring-cloud/eureka/eureka-client/src/main/resources/application.yml new file mode 100644 index 0000000..beb4cb2 --- /dev/null +++ b/spring-cloud/eureka/eureka-client/src/main/resources/application.yml @@ -0,0 +1,11 @@ +server: + port: 8020 +# 指定服务命名 +spring: + application: + name: eureka-client +# 指定注册中心地址 +eureka: + client: + serviceUrl: + defaultZone: http://localhost:8010/eureka/ diff --git a/spring-cloud/eureka/eureka-client/src/test/java/com/heibaiying/eurekaclient/EurekaClientApplicationTests.java b/spring-cloud/eureka/eureka-client/src/test/java/com/heibaiying/eurekaclient/EurekaClientApplicationTests.java new file mode 100644 index 0000000..1451824 --- /dev/null +++ b/spring-cloud/eureka/eureka-client/src/test/java/com/heibaiying/eurekaclient/EurekaClientApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.eurekaclient; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class EurekaClientApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring-cloud/eureka/eureka-server/pom.xml b/spring-cloud/eureka/eureka-server/pom.xml new file mode 100644 index 0000000..fc66e27 --- /dev/null +++ b/spring-cloud/eureka/eureka-server/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying.eureka + eureka-server + 0.0.1-SNAPSHOT + eureka-server + Demo project for Spring Boot + + + 1.8 + Greenwich.RC2 + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + + + diff --git a/spring-cloud/eureka/eureka-server/src/main/java/com/heibaiying/eurekaserver/EurekaServerApplication.java b/spring-cloud/eureka/eureka-server/src/main/java/com/heibaiying/eurekaserver/EurekaServerApplication.java new file mode 100644 index 0000000..48ba385 --- /dev/null +++ b/spring-cloud/eureka/eureka-server/src/main/java/com/heibaiying/eurekaserver/EurekaServerApplication.java @@ -0,0 +1,16 @@ +package com.heibaiying.eurekaserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class EurekaServerApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaServerApplication.class, args); + } + +} + diff --git a/spring-cloud/eureka/eureka-server/src/main/resources/application.yml b/spring-cloud/eureka/eureka-server/src/main/resources/application.yml new file mode 100644 index 0000000..817356d --- /dev/null +++ b/spring-cloud/eureka/eureka-server/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + port: 8010 +eureka: + instance: + hostname: localhost + client: + # 设置为false,代表不向注册中心注册自己 + register-with-eureka: false + # 注册中心主要用于维护服务,并不需要检索服务,所以设置为false + fetch-registry: false + serviceUrl: + defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ \ No newline at end of file diff --git a/spring-cloud/eureka/eureka-server/src/test/java/com/heibaiying/eurekaserver/EurekaServerApplicationTests.java b/spring-cloud/eureka/eureka-server/src/test/java/com/heibaiying/eurekaserver/EurekaServerApplicationTests.java new file mode 100644 index 0000000..468d11d --- /dev/null +++ b/spring-cloud/eureka/eureka-server/src/test/java/com/heibaiying/eurekaserver/EurekaServerApplicationTests.java @@ -0,0 +1,17 @@ +package com.heibaiying.eurekaserver; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class EurekaServerApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/spring-cloud/eureka/pom.xml b/spring-cloud/eureka/pom.xml new file mode 100644 index 0000000..a12681e --- /dev/null +++ b/spring-cloud/eureka/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.1.RELEASE + + + com.heibaiying + eureka + 0.0.1-SNAPSHOT + eureka + eureka project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + +