Springboot Admin是一個管理和監(jiān)控Springboot項(xiàng)目的組件,分為服務(wù)端和客戶端,兩端通過http進(jìn)行通信。由于其輕量級的特性,所以特別適合中小項(xiàng)目使用。

簡介
Springboot Admin是一個管理和監(jiān)控Springboot項(xiàng)目的組件,分為服務(wù)端和客戶端,兩端通過http進(jìn)行通信。由于其輕量級的特性,所以特別適合中小項(xiàng)目使用。
其效果圖如下:

服務(wù)端配置
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、配置相關(guān)屬性。
server:
port: 8080
servlet:
context-path: /server
spring:
security:
user:
#admin Server端登錄時用的賬戶密碼
name: server123
password: 123456
boot:
admin:
instance-auth:
#啟用header驗(yàn)證
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);
}
}
經(jīng)過以上3步,服務(wù)端就可以啟動了。
訪問??http://localhost:8080/server/,就可以看到以下登錄界面。??

使用在yml文件中配置的賬戶密碼就可以登錄了。

客戶端配置
1、在我們要監(jiān)控的客戶端中加入以下依賴。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.1</version>
</dependency>
2、暴露監(jiān)控接口以及配置Server地址。
客戶端在啟動后會向配置的Server發(fā)起注冊申請,此時為了安全性還需要Server端的賬戶密碼進(jìn)行校驗(yàn)。
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、對暴露的接口進(jìn)行權(quán)限校驗(yàn)。
由于我們將監(jiān)控接口進(jìn)行了暴露,所以必須對相關(guān)的接口進(jìn)行權(quán)限校驗(yàn),否則就有可能泄露相關(guān)信息。
對接口進(jìn)行權(quán)限過濾有很多種選擇,比如設(shè)置IP訪問的白名單,只允許admin Server所在的服務(wù)器訪問,也可以配置相關(guān)的token等等。
下面我們以一個簡單的接口過濾器實(shí)現(xiàn)對/actuator/**相關(guān)接口的權(quán)限校驗(yàn)。
@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("權(quán)限不足");
return;
}
//其他接口直接放行
filterChain.doFilter(request, servletResponse);
}
}
在這個filter中,對actuator相關(guān)的接口進(jìn)行了header參數(shù)的校驗(yàn),只有通過校驗(yàn)才可以訪問暴露出的actuator接口。
當(dāng)然,如果我們使用了SpringSecurity或者SaToken這樣的第三方權(quán)限框架,也可以去重寫相關(guān)的配置完成權(quán)限的判斷,原理都是一樣的。
下面我們看一下最終的監(jiān)控效果:




最后
除了通過普通http請求方式獲取監(jiān)控信息以外,Springboot admin還支持通過注冊中心的方式獲取相關(guān)信息,在其官方文檔大家也可以看到相關(guān)的配置。