Skip to content

Commit cdab23c

Browse files
author
YunaiV
committed
增加 admin 示例
1 parent 9515c86 commit cdab23c

File tree

25 files changed

+386
-11
lines changed

25 files changed

+386
-11
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
## 监控管理
6666

6767
* [《芋道 Spring Boot 监控端点 Actuator 入门》](http://www.iocoder.cn/Spring-Boot/Actuator/?github) 对应 [lab-34](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-34)
68-
* [《芋道 Spring Boot 监控系统 Admin 入门》](http://www.iocoder.cn/Spring-Boot/Admin/?github) 对应 [lab-35](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-35)
69-
* [《芋道 Spring Boot 监控平台 Prometheus + Grafana 入门》](http://www.iocoder.cn/Spring-Boot/Prometheus-Grafana/?github) 对应 [lab-36](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-36)
68+
* [《芋道 Spring Boot 监控工具 Admin 入门》](http://www.iocoder.cn/Spring-Boot/Admin/?github) 对应 [lab-35](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-35)
69+
* [《芋道 Spring Boot 监控平台 Prometheus + Grafana 入门》](http://www.iocoder.cn/Spring-Boot/Prometheus-and-Grafana/?github) 对应 [lab-36](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-36)
7070

7171
## 日志管理
7272

@@ -77,6 +77,7 @@
7777

7878
* [《芋道 Spring Boot 链路追踪 SkyWalking 入门》](http://www.iocoder.cn/Spring-Boot/SkyWalking/?github) 对应 [lab-37](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-37)
7979
* [《芋道 Spring Boot 链路追踪 Zipkin 入门》](http://www.iocoder.cn/Spring-Boot/Zipkin/?github) 对应 [lab-38](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-38)
80+
* [《芋道 Spring Boot 链路追踪 Pinpoint 入门》](http://www.iocoder.cn/Spring-Boot/Pinpoint/?github) 对应 [lab-39](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-38)
8081

8182
## 性能测试
8283

lab-35/lab-35-admin-02-adminserver/src/main/resources/application.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ eureka:
22
client:
33
service-url:
44
defaultZone: http://127.0.0.1:8761/eureka
5-
register-with-eureka: false
5+
register-with-eureka: false # 不注册到 Eureka 中

lab-35/lab-35-admin-02-demo-application/src/main/java/cn/iocoder/springboot/lab35/demo/Demo01Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class Demo01Application {
1010

1111
public static void main(String[] args) {
12-
System.setProperty("server.port", "18081");
12+
System.setProperty("server.port", "18081"); // 端口 18081
1313
SpringApplication.run(Demo01Application.class, args);
1414
}
1515

lab-35/lab-35-admin-02-demo-application/src/main/java/cn/iocoder/springboot/lab35/demo/Demo02Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class Demo02Application {
1010

1111
public static void main(String[] args) {
12-
System.setProperty("server.port", "18082");
12+
System.setProperty("server.port", "18082"); // 端口 18082
1313
SpringApplication.run(Demo02Application.class, args);
1414
}
1515

lab-35/lab-35-admin-02-eurekaserver/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<artifactId>lab-35-admin-02-eurekaserver</artifactId>
1414

1515
<dependencies>
16+
<!-- 实现对 Eureka Server 的自动化配置 -->
1617
<dependency>
1718
<groupId>org.springframework.cloud</groupId>
1819
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
spring:
22
application:
3-
name: eureka-server
3+
name: eureka-server # 应用名
44

55
server:
6-
port: 8761
6+
port: 8761 # 自定义服务器端口,避免冲突
77

88
eureka:
99
client:
10-
# service-url:
11-
# defaultZone: http://127.0.0.1:8761/eureka/
12-
register-with-eureka: false
13-
fetch-registry: false
10+
register-with-eureka: false # 不注册到 Eureka 中
11+
fetch-registry: false # 不从 Eureka 拉取注册信息
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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-35-admin-03-adminserver</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring Boot Admin Server 的自动化配置 -->
17+
<!--
18+
包含 1. spring-boot-admin-server :Server 端
19+
2. spring-boot-admin-server-ui :UI 界面
20+
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
21+
-->
22+
<dependency>
23+
<groupId>de.codecentric</groupId>
24+
<artifactId>spring-boot-admin-starter-server</artifactId>
25+
<version>2.2.0</version>
26+
</dependency>
27+
28+
<!-- 实现对 Spring Security 的自动化配置 -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-security</artifactId>
32+
</dependency>
33+
</dependencies>
34+
35+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springboot.lab35.adminserver;
2+
3+
import de.codecentric.boot.admin.server.config.EnableAdminServer;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@EnableAdminServer
9+
public class AdminServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(AdminServerApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cn.iocoder.springboot.lab35.adminserver.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
6+
import org.springframework.security.config.web.server.ServerHttpSecurity;
7+
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
8+
import org.springframework.security.core.userdetails.User;
9+
import org.springframework.security.core.userdetails.UserDetails;
10+
import org.springframework.security.web.server.SecurityWebFilterChain;
11+
12+
@Configuration
13+
@EnableWebFluxSecurity // 开启 Security 对 WebFlux 的安全功能
14+
public class SecurityConfig {
15+
16+
@Bean
17+
public MapReactiveUserDetailsService userDetailsService() {
18+
// 创建一个用户
19+
UserDetails user = User.withDefaultPasswordEncoder()
20+
.username("user")
21+
.password("user")
22+
.roles("USER")
23+
.build();
24+
25+
// 如果胖友有更多用户的诉求,这里可以继续创建
26+
27+
// 创建 MapReactiveUserDetailsService
28+
return new MapReactiveUserDetailsService(user);
29+
}
30+
31+
@Bean
32+
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
33+
http.authorizeExchange(exchanges -> // 设置权限配置
34+
exchanges
35+
.pathMatchers("/assets/**").permitAll() // 静态资源,允许匿名访问
36+
.pathMatchers("/login").permitAll() // 登陆接口,允许匿名访问
37+
.anyExchange().authenticated() //
38+
)
39+
.formLogin().loginPage("/login") // 登陆页面
40+
.and().logout().logoutUrl("/logout") // 登出界面
41+
.and().httpBasic() // HTTP Basic 认证方式
42+
.and().csrf().disable(); // csrf 禁用
43+
return http.build();
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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-35-admin-03-demo-application</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+
28+
<!-- 实现对 Spring Boot Admin Client 的自动化配置 -->
29+
<dependency>
30+
<groupId>de.codecentric</groupId>
31+
<artifactId>spring-boot-admin-starter-client</artifactId>
32+
<version>2.2.0</version>
33+
</dependency>
34+
35+
<!-- 实现对 Spring Security 的自动化配置 -->
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-security</artifactId>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab35.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
management:
2+
endpoints:
3+
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
4+
web:
5+
exposure:
6+
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
7+
8+
spring:
9+
application:
10+
name: demo-application # 应用名
11+
12+
# Spring Security 配置项,对应 SecurityProperties 配置类
13+
security:
14+
# 配置默认的 InMemoryUserDetailsManager 的用户账号与密码。
15+
user:
16+
name: test # 账号
17+
password: test # 密码
18+
19+
boot:
20+
admin:
21+
client:
22+
url: http://127.0.0.1:8080 # Spring Boot Admin Server 地址
23+
username: user # Spring Boot Admin Server 的认证账号
24+
password: user # Spring Boot Admin Server 的认证密码
25+
instance:
26+
metadata:
27+
user.name: ${spring.security.user.name} # Actuator 端点的认证账号
28+
user.password: ${spring.security.user.password} # Actuator 端点的认证密码
29+
30+
server:
31+
port: 18080 # 设置自定义 Server 端口,避免和 Spring Boot Admin Server 端口冲突。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
management:
2+
endpoints:
3+
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
4+
web:
5+
exposure:
6+
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
7+
8+
spring:
9+
application:
10+
name: demo-application # 应用名
11+
12+
# Spring Security 配置项,对应 SecurityProperties 配置类
13+
security:
14+
# 配置默认的 InMemoryUserDetailsManager 的用户账号与密码。
15+
user:
16+
name: test # 账号
17+
password: test # 密码
18+
19+
boot:
20+
admin:
21+
client:
22+
url: http://127.0.0.1:8080 # Spring Boot Admin Server 地址
23+
username: user # Spring Boot Admin Server 的认证账号
24+
password: user # Spring Boot Admin Server 的认证密码
25+
instance:
26+
metadata:
27+
user.name: ${spring.security.user.name} # Actuator 端点的认证账号
28+
user.password: ${spring.security.user.password} # Actuator 端点的认证密码
29+
30+
server:
31+
port: 18080 # 设置自定义 Server 端口,避免和 Spring Boot Admin Server 端口冲突。
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-35-admin-04-adminserver</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring Boot Admin Server 的自动化配置 -->
17+
<!--
18+
包含 1. spring-boot-admin-server :Server 端
19+
2. spring-boot-admin-server-ui :UI 界面
20+
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
21+
-->
22+
<dependency>
23+
<groupId>de.codecentric</groupId>
24+
<artifactId>spring-boot-admin-starter-server</artifactId>
25+
<version>2.2.0</version>
26+
</dependency>
27+
28+
<!-- 实现对 Java Mail 的自动化配置 -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-mail</artifactId>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springboot.lab35.adminserver;
2+
3+
import de.codecentric.boot.admin.server.config.EnableAdminServer;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@EnableAdminServer
9+
public class AdminServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(AdminServerApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
mail: # 配置发送告警的邮箱
3+
host: smtp.126.com
4+
username: wwbmlhh@126.com
5+
password: '******'
6+
default-encoding: UTF-8
7+
8+
boot:
9+
admin:
10+
notify:
11+
mail:
12+
from: ${spring.mail.username} # 告警发件人
13+
to: 7685413@qq.com # 告警收件人
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
mail:
3+
host: smtp.126.com
4+
username: wwbmlhh@126.com
5+
password: wwb3743
6+
default-encoding: UTF-8
7+
8+
boot:
9+
admin:
10+
notify:
11+
mail:
12+
from: wwbmlhh@126.com
13+
to: 7685413@qq.com

0 commit comments

Comments
 (0)