生活随笔
收集整理的這篇文章主要介紹了
Spring Boot Admin:微服务应用监控
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要
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模塊來作為監控中心演示其功能。
<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>
spring:application:name: admin
-server
server:port: 9301
- 在啟動類上添加@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。
<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>
spring:application:name: admin
-client
boot:admin:client:url: http
://localhost
:9301
server:port: 9305
management:endpoints:web:exposure:include: '*'endpoint:health:show-details: always
logging:file: admin
-client.log
監控信息演示
- 訪問如下地址打開Spring Boot Admin的主頁:http://localhost:9301
- 點擊wallboard按鈕,選擇admin-client查看監控信息;
- 監控信息概覽;
- 度量指標信息,比如JVM、Tomcat及進程信息;
- 環境變量信息,比如系統屬性、系統環境變量以及應用配置信息;
- 查看所有創建的Bean信息;
- 查看應用中的所有配置信息;
- 查看日志信息,需要添加以下配置才能開啟;
logging:file: admin
-client.log
-
查看JVM信息;
-
查看可以訪問的Web端點;
-
查看HTTP跟蹤信息;
結合注冊中心使用
Spring Boot Admin結合Spring Cloud 注冊中心使用,只需將admin-server和注冊中心整合即可,admin-server 會自動從注冊中心獲取服務列表,然后挨個獲取監控信息。這里以Eureka注冊中心為例來介紹下該功能。
修改admin-server
<dependency><groupId>org.springframework.cloud
</groupId><artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
- 在application-eureka.yml中進行配置,只需添加注冊中心配置即可:
spring:application:name: admin
-server
server:port: 9301
eureka:client:register-with-eureka: truefetch-registry: trueservice-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
<dependency><groupId>org.springframework.cloud
</groupId><artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
- 在application-eureka.yml中進行配置,刪除原來的admin-server地址配置,添加注冊中心配置即可:
spring:application:name: admin
-client
server:port: 9305
management:endpoints:web:exposure:include: '*'endpoint:health:show-details: always
logging:file: admin
-client.log
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http
://localhost
:8001/eureka/
- 在啟動類上添加@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模塊
<dependency><groupId>org.springframework.cloud
</groupId><artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<dependency><groupId>de.codecentric
</groupId><artifactId>spring-boot-admin-starter-server
</artifactId><version>2.1.5
</version>
</dependency>
<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>
- 在application.yml中進行配置,配置登錄用戶名和密碼,忽略admin-security-server的監控信息:
spring:application:name: admin
-security
-server
security: user:name: macro
password: 123456boot: admin:discovery:ignored-services: $
{spring.application.name
}
server:port: 9301
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http
://localhost
:8001/eureka/
- 對SpringSecurity進行配置,以便admin-client可以注冊:
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath
;public SecuritySecureConfig(AdminServerProperties adminServerProperties
) {this.adminContextPath
= adminServerProperties
.getContextPath();}@Overrideprotected void configure(HttpSecurity http
) throws Exception
{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()//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
├── admin
-server
├── admin
-client
└── admin
-security
-server
項目源碼地址
https://github.com/macrozheng/springcloud-learning
總結
以上是生活随笔為你收集整理的Spring Boot Admin:微服务应用监控的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。