轉載請標明出處:
https://www.fangzhipeng.com
本文出自方志朋的博客
Spring Boot Admin簡介
Spring Boot Admin是一個開源社區項目,用於管理和監控SpringBoot應用程序。 應用程序作為Spring Boot Admin Client向為Spring Boot Admin Server注冊(通過HTTP)或使用SpringCloud注冊中心(例如Eureka,Consul)發現。 UI是的AngularJs應用程序,展示Spring Boot Admin Client的Actuator端點上的一些監控。常見的功能或者監控如下:
- 顯示健康狀況
- 顯示詳細信息,例如
- JVM和內存指標
- micrometer.io指標
- 數據源指標
- 緩存指標
- 顯示構建信息編號
- 關注並下載日志文件
- 查看jvm系統和環境屬性
- 查看Spring Boot配置屬性
- 支持Spring Cloud的postable / env-和/ refresh-endpoint
- 輕松的日志級管理
- 與JMX-beans交互
- 查看線程轉儲
- 查看http跟蹤
- 查看auditevents
- 查看http-endpoints
- 查看計划任務
- 查看和刪除活動會話(使用spring-session)
- 查看Flyway / Liquibase數據庫遷移
- 下載heapdump
- 狀態變更通知(通過電子郵件,Slack,Hipchat,......)
- 狀態更改的事件日志(非持久性)
快速開始
創建Spring Boot Admin Server
本文的所有工程的Spring Boot版本為2.1.0 、Spring Cloud版本為Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依賴(完整的依賴見源碼):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
在工程admin-server引入admin-server的起來依賴和web的起步依賴,代碼如下:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后在工程的啟動類AdminServerApplication加上@EnableAdminServer注解,開啟AdminServer的功能,代碼如下:
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
在工程的配置文件application.yml中配置程序名和程序的端口,代碼如下:
spring:
application:
name: admin-server
server:
port: 8769
這樣Admin Server就創建好了。
創建Spring Boot Admin Client
在admin-client工程的pom文件引入admin-client的起步依賴和web的起步依賴,代碼如下:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在工程的配置文件application.yml中配置應用名和端口信息,以及向admin-server注冊的地址為http://localhost:8769,最后暴露自己的actuator的所有端口信息,具體配置如下:
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
在工程的啟動文件如下:
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
一次啟動兩個工程,在瀏覽器上輸入localhost:8769 ,瀏覽器顯示的界面如下:
查看wallboard:
點擊wallboard,可以查看admin-client具體的信息,比如內存狀態信息:
也可以查看spring bean的情況:
更多監控信息,自己體驗。
Spring boot Admin結合SC注冊中心使用
同上一個案例一樣,本案例也是使用的是Spring Boot版本為2.1.0 、Spring Cloud版本為Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依賴(完整的依賴見源碼),此處省略。
搭建注冊中心
注冊中心使用Eureka、使用Consul也是可以的,在eureka-server工程中的pom文件中引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置eureka-server的端口信息,以及defaultZone和防止自注冊。最后系統暴露eureka-server的actuator的所有端口。
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
在工程的啟動文件EurekaServerApplication加上@EnableEurekaServer注解開啟Eureka Server.
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
eureka-server搭建完畢。
搭建admin-server
在admin-server工程的pom文件引入admin-server的起步依賴、web的起步依賴、eureka-client的起步依賴,如下:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
然后配置admin-server,應用名、端口信息。並向注冊中心注冊,注冊地址為http://localhost:8761,最后將actuator的所有端口暴露出來,配置如下:
spring:
application:
name: admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
在工程的啟動類AdminServerApplication加上@EnableAdminServer注解,開啟admin server的功能,加上@EnableDiscoveryClient注解開啟eurke client的功能。
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
搭建admin-client
在admin-client的pom文件引入以下的依賴,由於2.1.0采用webflux,引入webflux的起步依賴,引入eureka-client的起步依賴,並引用actuator的起步依賴如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在工程的配置文件配置應用名、端口、向注冊中心注冊的地址,以及暴露actuator的所有端口。
spring:
application:
name: admin-client
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
server:
port: 8762
在啟動類加上@EnableDiscoveryClie注解,開啟DiscoveryClient的功能。
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
}
一次啟動三個工程,在瀏覽器上訪問localhost:8769,瀏覽器會顯示和上一小節一樣的界面。
集成spring security
在2.1.0版本中去掉了hystrix dashboard,登錄界面默認集成到了spring security模塊,只要加上spring security就集成了登錄模塊。
只需要改變下admin-server工程,需要在admin-server工程的pom文件引入以下的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在admin-server工的配置文件application.yml中配置spring security的用戶名和密碼,這時需要在服務注冊時帶上metadata-map的信息,如下:
spring:
security:
user:
name: "admin"
password: "admin"
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
寫一個配置類SecuritySecureConfig繼承WebSecurityConfigurerAdapter,配置如下:
@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" );
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().disable();
// @formatter:on
}
}
重啟啟動工程,在瀏覽器上訪問:http://localhost:8769/,會被重定向到登錄界面,登錄的用戶名和密碼為配置文件中配置的,分別為admin和admin,界面顯示如下:
集成郵箱報警功能
在spring boot admin中,也可以集成郵箱報警功能,比如服務不健康了、下線了,都可以給指定郵箱發送郵件。集成非常簡單,只需要改造下admin-server即可:
在admin-server工程Pom文件,加上mail的起步依賴,代碼如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
在配置文件application.yml文件中,需要配置郵件相關的配置,如下:
spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: 124746406@qq.com
做完以上配置后,當我們已注冊的客戶端的狀態從 UP 變為 OFFLINE 或其他狀態,服務端就會自動將電子郵件發送到上面配置的地址。
源碼下載
快速開始: https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin
和spring cloud結合:https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin-cloud
參考資料
http://codecentric.github.io/spring-boot-admin/2.1.0/
https://github.com/codecentric/spring-boot-admin
更多閱讀
掃一掃,支持下作者吧
(轉載本站文章請注明作者和出處 方志朋的博客)