mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 admin 示例
This commit is contained in:
@@ -65,8 +65,8 @@
|
||||
## 监控管理
|
||||
|
||||
* [《芋道 Spring Boot 监控端点 Actuator 入门》](http://www.iocoder.cn/Spring-Boot/Actuator/?github) 对应 [lab-34](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-34) 。
|
||||
* [《芋道 Spring Boot 监控系统 Admin 入门》](http://www.iocoder.cn/Spring-Boot/Admin/?github) 对应 [lab-35](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-35) 。
|
||||
* [《芋道 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) 。
|
||||
* [《芋道 Spring Boot 监控工具 Admin 入门》](http://www.iocoder.cn/Spring-Boot/Admin/?github) 对应 [lab-35](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-35) 。
|
||||
* [《芋道 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) 。
|
||||
|
||||
## 日志管理
|
||||
|
||||
@@ -77,6 +77,7 @@
|
||||
|
||||
* [《芋道 Spring Boot 链路追踪 SkyWalking 入门》](http://www.iocoder.cn/Spring-Boot/SkyWalking/?github) 对应 [lab-37](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-37) 。
|
||||
* [《芋道 Spring Boot 链路追踪 Zipkin 入门》](http://www.iocoder.cn/Spring-Boot/Zipkin/?github) 对应 [lab-38](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-38) 。
|
||||
* [《芋道 Spring Boot 链路追踪 Pinpoint 入门》](http://www.iocoder.cn/Spring-Boot/Pinpoint/?github) 对应 [lab-39](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-38) 。
|
||||
|
||||
## 性能测试
|
||||
|
||||
|
||||
@@ -2,4 +2,4 @@ eureka:
|
||||
client:
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka
|
||||
register-with-eureka: false
|
||||
register-with-eureka: false # 不注册到 Eureka 中
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
public class Demo01Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("server.port", "18081");
|
||||
System.setProperty("server.port", "18081"); // 端口 18081
|
||||
SpringApplication.run(Demo01Application.class, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
public class Demo02Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("server.port", "18082");
|
||||
System.setProperty("server.port", "18082"); // 端口 18082
|
||||
SpringApplication.run(Demo02Application.class, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<artifactId>lab-35-admin-02-eurekaserver</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Eureka Server 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
spring:
|
||||
application:
|
||||
name: eureka-server
|
||||
name: eureka-server # 应用名
|
||||
|
||||
server:
|
||||
port: 8761
|
||||
port: 8761 # 自定义服务器端口,避免冲突
|
||||
|
||||
eureka:
|
||||
client:
|
||||
# service-url:
|
||||
# defaultZone: http://127.0.0.1:8761/eureka/
|
||||
register-with-eureka: false
|
||||
fetch-registry: false
|
||||
register-with-eureka: false # 不注册到 Eureka 中
|
||||
fetch-registry: false # 不从 Eureka 拉取注册信息
|
||||
|
||||
35
lab-35/lab-35-admin-03-adminserver/pom.xml
Normal file
35
lab-35/lab-35-admin-03-adminserver/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-35-admin-03-adminserver</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring Boot Admin Server 的自动化配置 -->
|
||||
<!--
|
||||
包含 1. spring-boot-admin-server :Server 端
|
||||
2. spring-boot-admin-server-ui :UI 界面
|
||||
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-server</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Spring Security 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springboot.lab35.adminserver;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAdminServer
|
||||
public class AdminServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AdminServerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.iocoder.springboot.lab35.adminserver.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebFluxSecurity // 开启 Security 对 WebFlux 的安全功能
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public MapReactiveUserDetailsService userDetailsService() {
|
||||
// 创建一个用户
|
||||
UserDetails user = User.withDefaultPasswordEncoder()
|
||||
.username("user")
|
||||
.password("user")
|
||||
.roles("USER")
|
||||
.build();
|
||||
|
||||
// 如果胖友有更多用户的诉求,这里可以继续创建
|
||||
|
||||
// 创建 MapReactiveUserDetailsService
|
||||
return new MapReactiveUserDetailsService(user);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
http.authorizeExchange(exchanges -> // 设置权限配置
|
||||
exchanges
|
||||
.pathMatchers("/assets/**").permitAll() // 静态资源,允许匿名访问
|
||||
.pathMatchers("/login").permitAll() // 登陆接口,允许匿名访问
|
||||
.anyExchange().authenticated() //
|
||||
)
|
||||
.formLogin().loginPage("/login") // 登陆页面
|
||||
.and().logout().logoutUrl("/logout") // 登出界面
|
||||
.and().httpBasic() // HTTP Basic 认证方式
|
||||
.and().csrf().disable(); // csrf 禁用
|
||||
return http.build();
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
43
lab-35/lab-35-admin-03-demo-application/pom.xml
Normal file
43
lab-35/lab-35-admin-03-demo-application/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-35-admin-03-demo-application</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring MVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Spring Boot Admin Client 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-client</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Spring Security 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab35.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
management:
|
||||
endpoints:
|
||||
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: demo-application # 应用名
|
||||
|
||||
# Spring Security 配置项,对应 SecurityProperties 配置类
|
||||
security:
|
||||
# 配置默认的 InMemoryUserDetailsManager 的用户账号与密码。
|
||||
user:
|
||||
name: test # 账号
|
||||
password: test # 密码
|
||||
|
||||
boot:
|
||||
admin:
|
||||
client:
|
||||
url: http://127.0.0.1:8080 # Spring Boot Admin Server 地址
|
||||
username: user # Spring Boot Admin Server 的认证账号
|
||||
password: user # Spring Boot Admin Server 的认证密码
|
||||
instance:
|
||||
metadata:
|
||||
user.name: ${spring.security.user.name} # Actuator 端点的认证账号
|
||||
user.password: ${spring.security.user.password} # Actuator 端点的认证密码
|
||||
|
||||
server:
|
||||
port: 18080 # 设置自定义 Server 端口,避免和 Spring Boot Admin Server 端口冲突。
|
||||
Binary file not shown.
@@ -0,0 +1,31 @@
|
||||
management:
|
||||
endpoints:
|
||||
# Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: demo-application # 应用名
|
||||
|
||||
# Spring Security 配置项,对应 SecurityProperties 配置类
|
||||
security:
|
||||
# 配置默认的 InMemoryUserDetailsManager 的用户账号与密码。
|
||||
user:
|
||||
name: test # 账号
|
||||
password: test # 密码
|
||||
|
||||
boot:
|
||||
admin:
|
||||
client:
|
||||
url: http://127.0.0.1:8080 # Spring Boot Admin Server 地址
|
||||
username: user # Spring Boot Admin Server 的认证账号
|
||||
password: user # Spring Boot Admin Server 的认证密码
|
||||
instance:
|
||||
metadata:
|
||||
user.name: ${spring.security.user.name} # Actuator 端点的认证账号
|
||||
user.password: ${spring.security.user.password} # Actuator 端点的认证密码
|
||||
|
||||
server:
|
||||
port: 18080 # 设置自定义 Server 端口,避免和 Spring Boot Admin Server 端口冲突。
|
||||
36
lab-35/lab-35-admin-04-adminserver/pom.xml
Normal file
36
lab-35/lab-35-admin-04-adminserver/pom.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-35-admin-04-adminserver</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring Boot Admin Server 的自动化配置 -->
|
||||
<!--
|
||||
包含 1. spring-boot-admin-server :Server 端
|
||||
2. spring-boot-admin-server-ui :UI 界面
|
||||
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-server</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Java Mail 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springboot.lab35.adminserver;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAdminServer
|
||||
public class AdminServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AdminServerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
mail: # 配置发送告警的邮箱
|
||||
host: smtp.126.com
|
||||
username: wwbmlhh@126.com
|
||||
password: '******'
|
||||
default-encoding: UTF-8
|
||||
|
||||
boot:
|
||||
admin:
|
||||
notify:
|
||||
mail:
|
||||
from: ${spring.mail.username} # 告警发件人
|
||||
to: 7685413@qq.com # 告警收件人
|
||||
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
spring:
|
||||
mail:
|
||||
host: smtp.126.com
|
||||
username: wwbmlhh@126.com
|
||||
password: wwb3743
|
||||
default-encoding: UTF-8
|
||||
|
||||
boot:
|
||||
admin:
|
||||
notify:
|
||||
mail:
|
||||
from: wwbmlhh@126.com
|
||||
to: 7685413@qq.com
|
||||
29
lab-35/lab-35-admin-05-adminserver/pom.xml
Normal file
29
lab-35/lab-35-admin-05-adminserver/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-35-admin-05-adminserver</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring Boot Admin Server 的自动化配置 -->
|
||||
<!--
|
||||
包含 1. spring-boot-admin-server :Server 端
|
||||
2. spring-boot-admin-server-ui :UI 界面
|
||||
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-server</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springboot.lab35.adminserver;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAdminServer
|
||||
public class AdminServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AdminServerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cn.iocoder.springboot.lab35.adminserver.notify;
|
||||
|
||||
import de.codecentric.boot.admin.server.domain.entities.Instance;
|
||||
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
|
||||
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
|
||||
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
|
||||
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class LoggerNotifier extends AbstractEventNotifier {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public LoggerNotifier(InstanceRepository repository) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
|
||||
return Mono.fromRunnable(() -> {
|
||||
if (event instanceof InstanceStatusChangedEvent) {
|
||||
logger.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(),
|
||||
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
|
||||
} else {
|
||||
logger.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(), event.getType());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -18,6 +18,12 @@
|
||||
<module>lab-35-admin-02-adminserver</module>
|
||||
<module>lab-35-admin-02-demo-application</module>
|
||||
<module>lab-35-admin-02-eurekaserver</module>
|
||||
|
||||
<module>lab-35-admin-03-adminserver</module>
|
||||
<module>lab-35-admin-03-demo-application</module>
|
||||
|
||||
<module>lab-35-admin-04-adminserver</module>
|
||||
<module>lab-35-admin-05-adminserver</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user