在上一篇中,通過restful api的方式查看信息過於繁瑣,也不直觀,效率低下。當服務過多的時候看起來就過於麻煩,每個服務都需要調用不同的接口來查看監控信息。
SBA
SBA全稱spring boot admin 是一個管理和監控spring boot 應用程序的開源項目,分為admin-server與admin-client兩個組件,admin-server通過采集actuator端點數據,顯示在spring -boot-admin-ui上,已知的端點幾乎都有進行采集,通過spring-boot-admin可以動態切換日志級別、導出日志、導出heapdump、監控各項指標 等等
spring boot admin在對單一服務監控的同時也提供了集群監控方案,支持通過eureka、consul、zookeeper等注冊中心的方式實現多服務監控與管理
導入依賴
在pom.xml中添加對spring-boot-admin的相關依賴
<!-- 服務端:帶UI界面 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.0</version> </dependency> <!-- 客戶端包 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.0.0</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-actuator</artifactId> </dependency> <!-- 在管理界面中與 JMX-beans 進行交互所需要被依賴的 JAR --> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
如果想訪問info的信息需要如下配置
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
屬性配置
在application.yml中配置actuator的相關配置,其中info開頭的屬性,就是訪問info端點中顯示的相關內容,值得注意的十spring boot2.x中,默認只開放了info、health兩個端點,剩余的需要自己通過配置management.endpoints.web.exposure.include屬性來加載,這個management.endpoints.web.base-path屬性比較重要,因為spring boot2.x后每個端點默認的路徑是/actuator/endpointId這樣依賴spring boot admin是無法正常采集的
application.yml
spring: profiles: active: prod #指定讀取哪個文件 boot: admin: client: url: http://localhost:8088 instance: prefer-ip: true info: head: head body: body management: endpoints: web: exposure: #加載所有的端點,默認只加載了info、health include: '*' #不配置SBA 掃描不到 base-path: / endpoint: health: show-details: always #可以關閉指定的端點 shutdown: enabled: false
application-dev.yml 空的
application-prod.yml
server: port: 8088 servlet: context-path: / spring: boot: admin: client: username: david password: 123456 instance: metadata: user: name: david password: 123456 security: user: name: david password: 123456
在啟動方法中增加@EnableAdminServer注解 代表是Server端
@EnableAdminServer @SpringBootApplication public class BootApplication{ public static void main(String[] args) { SpringApplication.run(BootApplication.class,args); } @Profile("dev") @Configuration public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception{ http.authorizeRequests().anyRequest().permitAll().and().csrf().disable(); } } @Profile("prod") @Configuration public static 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"); 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(); } } }
測試
啟動項目 訪問 http://localhost:8088/login