SpringBootAdmin:轻量级的SpringBoot监控组件,用过的都说好

开发 架构
Springboot Admin是一个管理和监控Springboot项目的组件,分为服务端和客户端,两端通过http进行通信。由于其轻量级的特性,所以特别适合中小项目使用。

简介

Springboot Admin是一个管理和监控Springboot项目的组件,分为服务端和客户端,两端通过http进行通信。由于其轻量级的特性,所以特别适合中小项目使用。

其效果图如下:

服务端配置

1、引入Springboot admin和Spring Security依赖。

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、配置相关属性。

server:
port: 8080
servlet:
context-path: /server
spring:
security:
user:
#admin Server端登录时用的账户密码
name: server123
password: 123456
boot:
admin:
instance-auth:
#启用header验证
enabled: true
#Server访问client接口时会使用下面的配置生成authorization
default-user-name: "name_shishan"
default-password: "pwd_shishan"

3、配置@EnableAdminServer注解。

@SpringBootApplication
@Configuration
@EnableAdminServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}

经过以上3步,服务端就可以启动了。

访问​​http://localhost:8080/server/,就可以看到以下登录界面。​

使用在yml文件中配置的账户密码就可以登录了。

客户端配置

1、在我们要监控的客户端中加入以下依赖。

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.1</version>
</dependency>

2、暴露监控接口以及配置Server地址。

客户端在启动后会向配置的Server发起注册申请,此时为了安全性还需要Server端的账户密码进行校验。

spring:
boot:
admin:
client:
#admin注册地址
url: http://localhost:8080/server
#配置admin的账户
username: server123
password: 123456
admin:
header:
auth:
name: "name_shishan"
password: "pwd_shishan"
#暴露出端口
management:
endpoints:
web:
exposure:
include: "*"

3、对暴露的接口进行权限校验。

由于我们将监控接口进行了暴露,所以必须对相关的接口进行权限校验,否则就有可能泄露相关信息。

对接口进行权限过滤有很多种选择,比如设置IP访问的白名单,只允许admin Server所在的服务器访问,也可以配置相关的token等等。

下面我们以一个简单的接口过滤器实现对/actuator/**相关接口的权限校验。

@Component
public class PathFilter implements Filter {

@Value("${admin.header.auth.name}")
private String username;
@Value("${admin.header.auth.password}")
private String password;

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
AntPathMatcher antPathMatcher = new AntPathMatcher();
if (antPathMatcher.match("/actuator/**", request.getServletPath())) {
String authorization = request.getHeader("authorization");
if (StringUtils.hasText(authorization)) {
String token = Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
if (authorization.equals("Basic " + token)) {
//token匹配才放行
filterChain.doFilter(request, servletResponse);
return;
}
}
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().print("权限不足");
return;
}
//其他接口直接放行
filterChain.doFilter(request, servletResponse);
}
}

在这个filter中,对actuator相关的接口进行了header参数的校验,只有通过校验才可以访问暴露出的actuator接口。

当然,如果我们使用了SpringSecurity或者SaToken这样的第三方权限框架,也可以去重写相关的配置完成权限的判断,原理都是一样的。

下面我们看一下最终的监控效果:

最后

除了通过普通http请求方式获取监控信息以外,Springboot admin还支持通过注册中心的方式获取相关信息,在其官方文档大家也可以看到相关的配置。

责任编辑:姜华 来源: 今日头条
相关推荐

2009-07-14 18:05:28

轻量级Swing组件

2009-07-17 14:38:51

轻量级Swing组件

2023-02-24 16:33:37

VS Code插件

2023-09-27 00:12:23

2022-12-29 09:49:06

轻量级架构决策

2023-06-27 16:42:18

Tinygrad深度学习工具

2021-04-14 13:32:50

Redis轻量级分布式

2022-07-15 16:39:19

PythonWhoosh工具

2022-03-23 08:01:04

Python语言代码

2014-06-11 20:46:51

Monitorix监控系统

2020-06-19 15:38:08

分析工具GoatCounter开发

2009-03-09 18:08:26

LinuxEvilvte终端模拟器

2009-03-09 21:06:35

LinuxEvilvte终端模拟器

2021-12-13 16:43:04

鸿蒙HarmonyOS应用

2024-01-07 19:17:00

ArchcraftLinux发行版

2020-09-01 07:31:43

服务器监控监控工具服务器

2023-12-18 10:24:59

2016-10-14 16:35:39

2020-03-02 19:08:21

JVMJDKJRE

2009-01-19 09:28:42

JSONJavaScriptJSON结构
点赞
收藏

51CTO技术栈公众号