Spring Boot Admin 快速入門詳解
1、介紹
Spring Boot Admin 是開源社區(qū)孵化的項目,用于對 Spring Boot 應用的管理和監(jiān)控。Spring Boot Admin 分為服務端(spring-boot-admin-server)和客戶端(spring-boot-admin-client),服務端和客戶端之間采用 http 通訊方式實現(xiàn)數(shù)據(jù)交互;單體項目中需要整合 spring-boot-admin-client 才能讓應用被監(jiān)控。在 SpringCloud 項目中,spring-boot-admin-server 是直接從注冊中心抓取應用信息,不需要每個微服務應用整合 spring-boot-admin-client 就可以實現(xiàn)應用的管理和監(jiān)控。
2、服務端搭建
2.1 引入依賴
注意:版本要和 Spring Boot 版本對應,例如我的 Spring Boot 是 2.3.7.RELEASE,那么 Spring Boot Admin 對應的版本就是 2.3.x。
<!-- Spring Boot Admin 服務端依賴 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
2.2 添加注解
給啟動類添加一個注解:@EnableAdminServer
@EnableAdminServer
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
2.3 進行測試
訪問項目的端口號即可!
例如我配置的端口號是 9000,那么直接訪問 http://localhost:9000/ 即可!
2.4 測試結果

3、客戶端搭建
3.1 引入依賴
注意:版本要和 Spring Boot 版本對應,例如我的 Spring Boot 是 2.3.7.RELEASE,那么 Spring Boot Admin 對應的版本就是 2.3.x。
<!-- Spring Boot Admin 客戶端依賴 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
3.2 編寫配置
編寫 application.yml 文件:
spring:
application:
name: Client
boot:
admin:
client:
# 配置 Admin Server(服務端的名字)
url: http://localhost:9000
server:
port: 9001
# 開放端點用于 SpringBoot Admin 的監(jiān)控
management:
endpoints:
web:
exposure:
include: '*'
logging:
file:
# 配置生成日志文件名稱
name: admin-client.log
3.3 進行測試
啟動項目,然后訪問服務端的 Web 管理界面:


4、安全性
這個 Spring Boot Admin 的管理后臺不用賬號密碼就能直接訪問,一點都不安全,因此要給它加上登錄的功能。
參考 Spring Boot Admin 的官方文檔,我們可以在 Admin-Server 端添加 Spring Security 相關依賴及就可以實現(xiàn)需要登錄后才能訪問網(wǎng)頁管理面板。
4.1 添加依賴
在服務端添加 Spring Security 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
4.2 編寫配置
編寫 application.yml 文件,編寫用戶名密碼:
server:
port: 9000
spring:
application:
name: Server
security:
user:
name: admin
password: admin
4.3 編寫配置類
編寫 Spring Security 的配置類:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
// 1. 配置所有靜態(tài)資源和登錄頁可以公開訪問(匿名訪問)
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
// 2. 配置登錄和登出路徑
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
// 3. 開啟 http basic 支持,客戶端注冊時需要使用
.httpBasic().and()
.csrf()
// 4. 開啟基于 Cookie 的 CSRF 保護
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
// 5. 忽略這些路徑的 CSRF 保護以便客戶端注冊
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
4.4 修改客戶端配置
修改客戶端的 application.yml 配置文件,添加用戶名密碼:
這里不添加用戶名和密碼的話,是連不上服務端的:
spring:
application:
name: Client
boot:
admin:
client:
# 配置 Admin Server(服務端的名字)
url: http://localhost:9000
# 配置用戶名
username: admin
# 配置密碼
password: admin
4.5 進行測試
重啟客戶端和服務端項目
訪問效果為:


總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
解決SpringBoot返回結果如果為null或空值不顯示處理問題
這篇文章主要介紹了解決SpringBoot返回結果如果為null或空值不顯示處理問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件
這篇文章主要為大家詳細介紹了基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Java Swing JSlider滑塊的實現(xiàn)示例
這篇文章主要介紹了Java Swing JSlider滑塊的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12

