原文:https://blog.csdn.net/forezp/article/details/86105850git
Spring Boot Admin簡介github
Spring Boot Admin是一個開源社區項目,用於管理和監控SpringBoot應用程序。 應用程序做爲Spring Boot Admin Client向爲Spring Boot Admin Server註冊(經過HTTP)或使用SpringCloud註冊中心(例如Eureka,Consul)發現。 UI是的AngularJs應用程序,展現Spring Boot Admin Client的Actuator端點上的一些監控。常見的功能或者監控以下:web
顯示健康情況spring
顯示詳細信息,例如數據庫
JVM和內存指標瀏覽器
micrometer.io指標緩存
數據源指標session
緩存指標app
顯示構建信息編號jvm
關注並下載日誌文件
查看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就建立好了。
在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工程的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的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
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>12345678910111213141516171819202122232425在工程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>123456789而後在工程的啓動類AdminServerApplication加上@EnableAdminServer註解,開啓AdminServer的功能,代碼以下:@SpringBootApplication@EnableAdminServerpublic class AdminServerApplication { public static void main(String[] args) { SpringApplication.run( AdminServerApplication.class, args ); }}1234567891011在工程的配置文件application.yml中配置程序名和程序的端口,代碼以下:spring: application: name: admin-serverserver: port: 876912345這樣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>12345678910111213在工程的配置文件application.yml中配置應用名和端口信息,以及向admin-server註冊的地址爲http://localhost:8769,最後暴露本身的actuator的全部端口信息,具體配置以下:spring: application: name: admin-client boot: admin: client: url: http://localhost:8769server: port: 8768management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS12345678910111213141516171819在工程的啓動文件以下:@SpringBootApplicationpublic class AdminClientApplication { public static void main(String[] args) { SpringApplication.run( AdminClientApplication.class, args ); }1234567一次啓動兩個工程,在瀏覽器上輸入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>12345配置eureka-server的端口信息,以及defaultZone和防止自注冊。最後系統暴露eureka-server的actuator的全部端口。spring: application: name: eureka-serverserver: port: 8761eureka: client: service-url: defaultZone: http://localhost:8761/eureka register-with-eureka: false fetch-registry: falsemanagement: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS12345678910111213141516171819在工程的啓動文件EurekaServerApplication加上@EnableEurekaServer註解開啓Eureka Server.@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run( EurekaServerApplication.class, args ); }}12345678910eureka-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>123456789101112131415而後配置admin-server,應用名、端口信息。並向註冊中心註冊,註冊地址爲http://localhost:8761,最後將actuator的全部端口暴露出來,配置以下:spring: application: name: admin-serverserver: port: 8769eureka: client: registryFetchIntervalSeconds: 5 service-url: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/healthmanagement: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS1234567891011121314151617181920212223在工程的啓動類AdminServerApplication加上@EnableAdminServer註解,開啓admin server的功能,加上@EnableDiscoveryClient註解開啓eurke client的功能。@SpringBootApplication@EnableAdminServer@EnableDiscoveryClientpublic class AdminServerApplication { public static void main(String[] args) { SpringApplication.run( AdminServerApplication.class, args ); }}123456789101112搭建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>123456789101112131415在工程的配置文件配置應用名、端口、向註冊中心註冊的地址,以及暴露actuator的全部端口。spring: application: name: admin-clienteureka: 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: ALWAYSserver: port: 8762123456789101112131415161718192021222324在啓動類加上@EnableDiscoveryClie註解,開啓DiscoveryClient的功能。@SpringBootApplication@EnableDiscoveryClientpublic class AdminClientApplication { public static void main(String[] args) { SpringApplication.run( AdminClientApplication.class, args ); }}12345678一次啓動三個工程,在瀏覽器上訪問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>1234在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}123456789101112寫一個配置類SecuritySecureConfig繼承WebSecurityConfigurerAdapter,配置以下:@Configurationpublic 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 }}1234567891011121314151617181920212223242526272829重啓啓動工程,在瀏覽器上訪問: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>12345在配置文件application.yml文件中,須要配置郵件相關的配置,以下:spring.mail.host: smtp.163.comspring.mail.username: miles02spring.mail.password:spring.boot.admin.notify.mail.to: 124746406@qq.com12345作完以上配置後,當咱們已註冊的客戶端的狀態從 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--------------------- 版權聲明:本文爲CSDN博主「方誌朋」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處連接及本聲明。原文連接:https://blog.csdn.net/forezp/article/details/86105850