File tree 8 files changed +156
-1
lines changed
lab-01/lab-01-springsecurity-demo-role/src/main/java/cn/iocoder/springboot/lab01/springsecurity/config
lab-34-actuator-demo-metrics
java/cn/iocoder/springboot/lab34/actuatordemo
8 files changed +156
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ protected void configure(HttpSecurity http) throws Exception {
16
16
http
17
17
// 配置请求地址的权限
18
18
.authorizeRequests ()
19
- .antMatchers ("/test/echo " ).permitAll () // 所有用户可访问
19
+ .antMatchers ("/test/demo " ).permitAll () // 所有用户可访问
20
20
.antMatchers ("/test/admin" ).hasRole ("ADMIN" ) // 需要 ADMIN 角色
21
21
.antMatchers ("/test/normal" ).access ("hasRole('ROLE_NORMAL')" ) // 需要 NORMAL 角色。
22
22
// 任何请求,访问的用户都需要经过认证
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 15
15
<module >lab-34-actuator-demo</module >
16
16
<module >lab-34-actuator-demo-health</module >
17
17
<module >lab-34-actuator-demo-info</module >
18
+ <module >lab-34-actuator-demo-metrics</module >
18
19
</modules >
19
20
20
21
You can’t perform that action at this time.
0 commit comments