文章目錄
項目使用的Spring Cloud爲Hoxton版本,Spring Boot爲2.2.2.RELEASE版本
Spring Cloud入門系列彙總
摘要
Spring Boot Admin 可以對SpringBoot應用的各項指標進行監控,可以作爲微服務架構中的監控中心來使用,本文將對其用法進行詳細介紹。
Spring Boot Admin 簡介
SpringBoot應用可以通過Actuator來暴露應用運行過程中的各項指標,Spring Boot Admin通過這些指標來監控SpringBoot應用,然後通過圖形化界面呈現出來。Spring Boot Admin不僅可以監控單體應用,還可以和Spring Cloud的註冊中心相結合來監控微服務應用。
Spring Boot Admin 可以提供應用的以下監控信息:
- 監控應用運行過程中的概覽信息;
- 度量指標信息,比如JVM、Tomcat及進程信息;
- 環境變量信息,比如系統屬性、系統環境變量以及應用配置信息;
- 查看所有創建的Bean信息;
- 查看應用中的所有配置信息;
- 查看應用運行日誌信息;
- 查看JVM信息;
- 查看可以訪問的Web端點;
- 查看HTTP跟蹤信息。
創建admin-server模塊
這裏我們創建一個admin-server模塊來作爲監控中心演示其功能。
在pom.xml中添加相關依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
在application.yml中進行配置:
server:
port: 9301
spring:
application:
name: admin-server
在啓動類上添加@EnableAdminServer來啓用admin-server功能:
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
創建admin-client模塊
這裏我們創建一個admin-client模塊作爲客戶端註冊到admin-server。
在pom.xml中添加相關依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
在application.yml中進行配置:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
# 配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
# 添加開啓admin的日誌監控
logging:
file: admin-client.log
啓動admin-server和admin-client服務。
監控信息演示
訪問如下地址打開Spring Boot Admin的主頁:http://localhost:9301
點擊wallboard按鈕,選擇admin-client查看監控信息;
監控信息概覽;
度量指標信息,比如JVM、Tomcat及進程信息;
環境變量信息,比如系統屬性、系統環境變量以及應用配置信息;
查看所有創建的Bean信息;
查看應用中的所有配置信息;
查看日誌信息,admin-client模塊的applicaiton.yml需要添加以下配置才能開啓;
logging:
#添加開啓admin的日誌監控
file: admin-client.log
查看JVM信息;
查看可以訪問的Web端點;
結合註冊中心使用
Spring Boot Admin結合Spring Cloud 註冊中心使用,只需將admin-server和註冊中心整合即可,admin-server 會自動從註冊中心獲取服務列表,然後挨個獲取監控信息。這裏以Eureka註冊中心爲例來介紹下該功能。
修改admin-server
在pom.xml中添加相關依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在application-eureka.yml中進行配置,只需添加註冊中心配置即可:
server:
port: 9301
spring:
application:
name: admin-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
在啓動類上添加@EnableDiscoveryClient來啓用服務註冊功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
修改admin-client
在pom.xml中添加相關依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在application-eureka.yml中進行配置,刪除原來的admin-server地址配置,添加註冊中心配置即可:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
# 配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
# 添加開啓admin的日誌監控
logging:
file: admin-client.log
在啓動類上添加@EnableDiscoveryClient來啓用服務註冊功能:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
功能演示
啓動eureka-server,使用application-eureka.yml配置啓動admin-server,admin-client;
查看註冊中心發現服務均已註冊:http://localhost:8001/
查看Spring Boot Admin 主頁發現可以看到服務信息:http://localhost:9301
添加登錄認證
我們可以通過給admin-server添加Spring Security支持來獲得登錄認證功能。
創建admin-security-server模塊
在pom.xml中添加相關依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在application.yml中進行配置,配置登錄用戶名和密碼,忽略admin-security-server的監控信息:
server:
port: 9301
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: admin-security-server
security:
user:
# 配置登錄用戶名和密碼
name: root
password: 123456
boot:
admin:
discovery:
# 不顯示admin-security-server的監控信息
ignored-services: ${spring.application.name}
對SpringSecurity進行配置,以便admin-client可以註冊:
@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 {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
//1.配置所有靜態資源和登錄頁可以公開訪問
.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支持,admin-client註冊時需要使用
.httpBasic().and()
.csrf()
//4.開啓基於cookie的csrf保護
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//5.忽略這些路徑的csrf保護以便admin-client註冊
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
修改啓動類,開啓AdminServer及註冊發現功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminSecurityServerApplication.class, args);
}
}
啓動eureka-server,admin-security-server,訪問Spring Boot Admin 主頁發現需要登錄才能訪問:http://localhost:9301
使用到的模塊
springcloud-learning
├── eureka-server -- eureka註冊中心
├── admin-server -- admin監控中心服務
├── admin-client -- admin監控中心監控的應用服務
└── admin-security-server -- 帶登錄認證的admin監控中心服務