Spring Boot Admin 1.x其簡陋的頁面讓人不忍直視澎羞,但更新到2.x系列后较店,像脫胎換骨一般好用
這篇博客記錄我個人在使用Spring Boot Admin過程中遇到過的坑颂暇,每個坑位都會附上詳細的填坑辦法
環(huán)境參數(shù):
Spring Boot 2.x
Spring Boot Admin 2.x
JDK1.8+
CentOS
服務(wù)直接注冊失敗
常見的注冊失敗問題可以分為以下兩種
Spring Boot Admin服務(wù)端與客戶端不在同一臺服務(wù)器上
提示安全校驗不通過
第一種問題的解決辦法:
必須在客戶端配置boot.admin.client.instance.service-url屬性,讓Spring Boot Admin服務(wù)端可以通過網(wǎng)絡(luò)獲取客戶端的數(shù)據(jù)(否則默認會通過主機名去獲确龉亍)
boot:
admin:
client:
url: ${your spring boot admin url}
username: ${your spring boot admin username}
password: ${your spring boot admin password}
instance:
prefer-ip: true
service-url: ${your spring boot client url}
第二種問題的解決辦法:
首先缔逛,安全檢驗問題,其實就是現(xiàn)在服務(wù)端配置賬號密碼冰悠,然后客戶端在注冊的時候提供賬號密碼進行登錄來完成校驗
這個過程的實現(xiàn)堡妒,作為Spring全家桶項目,推薦使用Spring Security來解決溉卓,所以如果出現(xiàn)校驗失敗皮迟,那多半是Spring Security的配置出現(xiàn)問題
接下來介紹如何分別配置服務(wù)端與客戶端來處理這個問題
服務(wù)端配置
通過maven加載Spring Security依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
設(shè)置服務(wù)端的用戶名和密碼(客戶端來注冊時使用此賬號密碼進行登錄)
spring:
security:
user:
name: liumapp
password: superliumapp
編寫Spring Security配置類
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
/**
* file SecuritySecureConfig.java
* author liumapp
* github https://github.com/liumapp
* email liumapp.com@gmail.com
* homepage http://www.liumapp.com
* date 2018/11/29
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
// @formatter:on
}
}
上面這段代碼,需要大家注意的就一個AdminServerProperties類桑寨,通過瀏覽它的部分源代碼:
@ConfigurationProperties("spring.boot.admin")
public class AdminServerProperties {
/**
* The context-path prefixes the path where the Admin Servers statics assets and api should be
* served. Relative to the Dispatcher-Servlet.
*/
private String contextPath = "";
/**
* The metadata keys which should be sanitized when serializing to json
*/
private String[] metadataKeysToSanitize = new String[]{".*password$", ".*secret$", ".*key$", ".*$token$", ".*credentials.*", ".*vcap_services$"};
/**
* For Spring Boot 2.x applications the endpoints should be discovered automatically using the actuator links.
* For Spring Boot 1.x applications SBA probes for the specified endpoints using an OPTIONS request.
* If the path differs from the id you can specify this as id:path (e.g. health:ping).
*/
private String[] probedEndpoints = {"health", "env", "metrics", "httptrace:trace", "httptrace", "threaddump:dump", "threaddump", "jolokia", "info", "logfile", "refresh", "flyway", "liquibase", "heapdump", "loggers", "auditevents", "mappings", "scheduledtasks", "configprops", "caches", "beans"};
//以下省略...
}
可以發(fā)現(xiàn)AdminServerProperties定義了Spring Boot Admin的配置屬性伏尼,登錄自然也是其中之一,所以我們在編寫Spring Security配置類的時候尉尾,務(wù)必要引入AdminServerProperties
到這里爆阶,Spring Boot Admin服務(wù)端對于Spring Security的配置便結(jié)束了,接下來讓我們開始客戶端的Security配置
客戶端配置
首先對于客戶端沙咏,我們除了Spring Boot Admin Client依賴外辨图,還需要額外引入 Spring Security依賴:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在此基礎(chǔ)上通過編寫客戶端application.yml配置文件來設(shè)置賬號密碼
spring:
boot:
admin:
client:
url: ${your sba server url}
username: ${your sba username}
password: ${your sba password}
instance:
service-base-url: ${your client url}
接下來對Client端的Spring Security做配置,允許Server端讀取actuator暴露的數(shù)據(jù)
添加一個配置類:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
到此肢藐,因為安全驗證而不能注冊成功的問題便可以解決
注冊成功但無法顯示日志
這個問題產(chǎn)生原因有兩種
客戶端日志沒有以文件形式存儲下來
客戶端容器化部署后故河,日志文件沒有映射到宿主機磁盤上
針對第一種情況,解決辦法比較簡單吆豹,將系統(tǒng)產(chǎn)生的日志以文件形式保存即可:
logging:
file: ./log/client.log
pattern:
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
第二種情況較為復(fù)雜鱼的,首先要分清除是用什么工具來部署容器的,但一般而言直接通過文件映射即可
這里以docker為例痘煤,在docker內(nèi)通過設(shè)置volumes來映射日志文件
volumes:
- ./log:/client/log/
注冊成功但信息顯示不全
偶爾也會遇到這種情況:Spring Boot Admin客戶端注冊服務(wù)端是成功的凑阶,但是統(tǒng)計頁面顯示的數(shù)據(jù)過少(可能只有日志這一欄)
造成這種問題的原因在于:我們沒有開放客戶端的actuator接口地址給服務(wù)端訪問
那么解決辦法也很簡單,允許服務(wù)端訪問actuator即可
首先我們需要確保項目有actuator依賴(一般來說速勇,spring-boot-admin-starter-client本身就包含這個依賴晌砾,所以不需要額外引入):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然后打開actuator的端口,在client端的配置文件中增加以下內(nèi)容:
management:
endpoints:
web:
exposure:
include: "*"
同時考慮到client與server域名存在不一樣的情況烦磁,順便把跨域也解決掉养匈,增加跨域配置類:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author liumapp
* @file CorsConfig.java
* @email liumapp.com@gmail.com
* @homepage http://www.liumapp.com
* @date 2018/8/11
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("*");
}
}
問題即可解決