diff --git a/README.md b/README.md index c3c3283..96cce47 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ spring boot demo 是整合 logback、mybatis的 demo 项目,后续会加整合 ../spring-boot-demo-helloworld + ../spring-boot-demo-actuator ../spring-boot-demo-logback ../spring-boot-demo-mybatis @@ -107,6 +108,7 @@ spring boot demo 是整合 logback、mybatis的 demo 项目,后续会加整合 | Module 名称 | Module 介绍 | | ---------------------------------------- | ---------------------------------------- | | [spring-boot-demo-helloworld](./spring-boot-demo-helloworld) | spring-boot 的一个 helloworld | +| [spring-boot-demo-actuator](./spring-boot-demo-actuator) | spring-boot 集成 spring-boot-starter-actuator 用于监控 spring-boot 的启动和运行状态 | | [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | [spring-boot-demo-mybatis](./spring-boot-demo-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | diff --git a/spring-boot-demo-actuator/README.md b/spring-boot-demo-actuator/README.md new file mode 100644 index 0000000..b411aaa --- /dev/null +++ b/spring-boot-demo-actuator/README.md @@ -0,0 +1,106 @@ +# spring-boot-demo-actuator + +依赖 [spring-boot-demo-helloworld](../spring-boot-demo-parent)、spring-boot-starter-actuator + +### pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-actuator + 0.0.1-SNAPSHOT + war + + spring-boot-demo-actuator + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + spring-boot-demo-actuator + + + +``` + +### application.yml + +```yml +server: + port: 8080 + context-path: /demo +# actuator 配置 +management: + security: + # 关闭 actuator 的身份验证 + enabled: false + # 配置 actuator 的访问路径前缀 + context-path: /sys + # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离 + port: 1111 +``` + +### actuator 提供的端点信息 + +| 端点 | 描述 | HTTP 方法 | +| ----------- | ---------------------------------------- | ------- | +| autoconfig | 显示自动配置的信息 | GET | +| beans | 显示应用程序上下文所有的Spring bean | GET | +| configprops | 显示所有 `@ConfigurationProperties` 的配置属性列表 | GET | +| dump | 显示线程活动的快照 | GET | +| env | 显示应用的环境变量 | GET | +| health | 显示应用程序的健康指标,这些值由HealthIndicator的实现类提供。常见取值:`UP` / `DOWN` / `UNKNOWN` / `OUT_OF_SERVICE` | GET | +| info | 显示应用的信息,可使用 `info.*` 属性自定义info端点公开的数据 | GET | +| mappings | 显示所有的URL路径 | GET | +| metrics | 显示应用的度量标准信息 | GET | +| shutdown | 关闭应用(默认情况下不启用,如需启用,需设置`endpoints.shutdown.enabled=true`) | POST | +| trace | 显示跟踪信息(默认情况下为最近100个HTTP请求) | GET | + +### actuator 的访问权限 + +#### 方法一:(本 demo 中使用的是这种) + +```yml +management: + security: + enabled: false +``` + +#### 方法二: + +pom.xml 中添加以下 `spring-boot-starter-security` 依赖: + +```xml + + org.springframework.boot + spring-boot-starter-security + +``` + +并在 `application.yml` 文件中设置访问的密码 + +```yml +security: + basic: + enabled: true + user: + name: user + password: 123 +``` + +如果不设置,则用户名是 user,密码是一个随机值,在启动时会在控制台打印。 \ No newline at end of file diff --git a/spring-boot-demo-actuator/pom.xml b/spring-boot-demo-actuator/pom.xml new file mode 100644 index 0000000..c2d2e6d --- /dev/null +++ b/spring-boot-demo-actuator/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + spring-boot-demo-actuator + 0.0.1-SNAPSHOT + war + + spring-boot-demo-actuator + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + spring-boot-demo-actuator + + + diff --git a/spring-boot-demo-actuator/src/main/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplication.java b/spring-boot-demo-actuator/src/main/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplication.java new file mode 100644 index 0000000..71b9fcf --- /dev/null +++ b/spring-boot-demo-actuator/src/main/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplication.java @@ -0,0 +1,12 @@ +package com.xkcoding.springbootdemoactuator; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootDemoActuatorApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoActuatorApplication.class, args); + } +} diff --git a/spring-boot-demo-actuator/src/main/resources/application.yml b/spring-boot-demo-actuator/src/main/resources/application.yml new file mode 100644 index 0000000..46e977e --- /dev/null +++ b/spring-boot-demo-actuator/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + port: 8080 + context-path: /demo +# actuator 配置 +management: + security: + # 关闭 actuator 的身份验证 + enabled: false + # 配置 actuator 的访问路径前缀 + context-path: /sys + # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离 + port: 1111 \ No newline at end of file diff --git a/spring-boot-demo-actuator/src/test/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplicationTests.java b/spring-boot-demo-actuator/src/test/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplicationTests.java new file mode 100644 index 0000000..752f6fe --- /dev/null +++ b/spring-boot-demo-actuator/src/test/java/com/xkcoding/springbootdemoactuator/SpringBootDemoActuatorApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.springbootdemoactuator; + +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 SpringBootDemoActuatorApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index dd4d3ee..c09a9c0 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -13,6 +13,7 @@ ../spring-boot-demo-helloworld + ../spring-boot-demo-actuator ../spring-boot-demo-logback ../spring-boot-demo-mybatis