Skip to content

Commit 988a74c

Browse files
author
YunaiV
committed
增加 actuate 示例
1 parent e3dfab0 commit 988a74c

File tree

8 files changed

+156
-1
lines changed

8 files changed

+156
-1
lines changed

lab-01/lab-01-springsecurity-demo-role/src/main/java/cn/iocoder/springboot/lab01/springsecurity/config/SecurityConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected void configure(HttpSecurity http) throws Exception {
1616
http
1717
// 配置请求地址的权限
1818
.authorizeRequests()
19-
.antMatchers("/test/echo").permitAll() // 所有用户可访问
19+
.antMatchers("/test/demo").permitAll() // 所有用户可访问
2020
.antMatchers("/test/admin").hasRole("ADMIN") // 需要 ADMIN 角色
2121
.antMatchers("/test/normal").access("hasRole('ROLE_NORMAL')") // 需要 NORMAL 角色。
2222
// 任何请求,访问的用户都需要经过认证
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.2.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-34-actuator-demo-metrics</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对 Actuator 的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-actuator</artifactId>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab34.actuatordemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.iocoder.springboot.lab34.actuatordemo.controller;
2+
3+
import io.micrometer.core.instrument.Counter;
4+
import io.micrometer.core.instrument.Metrics;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping("/demo")
11+
public class DemoController {
12+
13+
/**
14+
* DEMO 访问次数 Metrics
15+
*/
16+
// private static final Counter METRICS_DEMO_COUNT = Metrics.counter("demo.visit.count");
17+
/**
18+
* DEMO 访问次数 Metrics
19+
*/
20+
private static final Counter METRICS_DEMO_COUNT = Counter
21+
.builder("demo.visit.count") // 指标的名字
22+
.description("demo 访问次数") // 指标的描述
23+
.baseUnit("次") // 指标的单位
24+
.tag("test", "nicai") // 自定义标签
25+
.register(Metrics.globalRegistry); // 注册到全局 MeterRegistry 指标注册表
26+
27+
@GetMapping("/visit")
28+
public String visit() {
29+
// 增加次数
30+
METRICS_DEMO_COUNT.increment();
31+
return "Demo 示例";
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.iocoder.springboot.lab34.actuatordemo.controller;
2+
3+
import io.micrometer.core.annotation.Counted;
4+
import io.micrometer.core.annotation.Timed;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
// TODO 暂时没生效 https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-spring-mvc
10+
@RestController
11+
@RequestMapping("/example")
12+
@Timed
13+
public class ExampleController {
14+
15+
@GetMapping("/visit")
16+
@Counted(value = "example.visit.count", description = "example 访问次数")
17+
public String visit() {
18+
return "Example 示例";
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#spring:
2+
# application:
3+
# name: demo-application
4+
5+
management:
6+
endpoint:
7+
# Metrics 端点配置项
8+
metrics:
9+
enabled: true # 是否开启。默认为 true 开启。
10+
# Metrics 的具体配置项,对应 MetricsProperties 配置类
11+
metrics:
12+
# 设置指定前缀的指标是否开启
13+
enable:
14+
xxx: false
15+
# 通用 tag
16+
tags:
17+
application: demo-application
18+
19+
endpoints:
20+
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
21+
web:
22+
base-path: /actuator # Actuator 提供的 API 接口的根目录。默认为 /actuator
23+
exposure:
24+
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
25+
exclude: # 在 include 的基础上,需要排除的端点。通过设置 * ,可以排除所有端点。
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#spring:
2+
# application:
3+
# name: demo-application
4+
5+
management:
6+
endpoint:
7+
# Metrics 端点配置项
8+
metrics:
9+
enabled: true # 是否开启。默认为 true 开启。
10+
# Metrics 的具体配置项,对应 MetricsProperties 配置类
11+
metrics:
12+
# 设置指定前缀的指标是否开启
13+
enable:
14+
xxx: false
15+
# 通用 tag
16+
tags:
17+
application: demo-application
18+
web:
19+
server:
20+
request:
21+
autotime:
22+
enabled: false
23+
24+
endpoints:
25+
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
26+
web:
27+
base-path: /actuator # Actuator 提供的 API 接口的根目录。默认为 /actuator
28+
exposure:
29+
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
30+
exclude: # 在 include 的基础上,需要排除的端点。通过设置 * ,可以排除所有端点。
31+

lab-34/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>lab-34-actuator-demo</module>
1616
<module>lab-34-actuator-demo-health</module>
1717
<module>lab-34-actuator-demo-info</module>
18+
<module>lab-34-actuator-demo-metrics</module>
1819
</modules>
1920

2021

0 commit comments

Comments
 (0)