Spring Boot Admin(監(jiān)控工具)的使用
前面的文章我們講了Spring Boot的Actuator。但是Spring Boot Actuator只是提供了一個(gè)個(gè)的接口,需要我們自行集成到監(jiān)控程序中。今天我們將會(huì)講解一個(gè)優(yōu)秀的監(jiān)控工具Spring Boot Admin。 它采用圖形化的界面,讓我們的Spring Boot管理更加簡(jiǎn)單。
先上圖給大家看一下Spring Boot Admin的界面:
從界面上面我們可以看到Spring Boot Admin提供了眾多強(qiáng)大的監(jiān)控功能。那么開始我們的學(xué)習(xí)吧。
配置Admin Server
既然是管理程序,肯定有一個(gè)server,配置server很簡(jiǎn)單,我們添加這個(gè)依賴即可:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.2</version> </dependency>
同時(shí)我們需要在main程序中添加@EnableAdminServer來啟動(dòng)admin server。
@EnableAdminServer @SpringBootApplication public class SpringBootAdminServerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminServerApplication.class, args); } }
配置admin client
有了server,我們接下來配置需要監(jiān)控的client應(yīng)用程序,在本文中,我們自己監(jiān)控自己,添加client依賴如下:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.2</version> </dependency>
我們需要為client指定要注冊(cè)到的admin server:
spring.boot.admin.client.url=http://localhost:8080
因?yàn)镾pring Boot Admin依賴于 Spring Boot Actuator, 從Spring Boot2 之后,我們需要主動(dòng)開啟暴露的主鍵,如下:
management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
配置安全主鍵
通常來說,我們需要一個(gè)登陸界面,以防止未經(jīng)授權(quán)的人訪問。spring boot admin提供了一個(gè)UI供我們使用,同時(shí)我們添加Spring Security依賴:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-login</artifactId> <version>1.5.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
添加了Spring Security,我們需要自定義一些配置:
@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; public WebSecurityConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/"); http .authorizeRequests() .antMatchers(this.adminServer.getContextPath() + "/assets/**").permitAll() .antMatchers(this.adminServer.getContextPath() + "/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(this.adminServer.getContextPath() + "/login") .successHandler(successHandler) .and() .logout() .logoutUrl(this.adminServer.getContextPath() + "/logout") .and() .httpBasic() .and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances", HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.getContextPath() + "/instances/*", HttpMethod.DELETE.toString()), new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**")) .and() .rememberMe() .key(UUID.randomUUID().toString()) .tokenValiditySeconds(1209600); } }
接下來,我們?cè)谂渲梦募兄付ǚ?wù)器的用戶名和密碼:
spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin
作為一個(gè)客戶端,連接服務(wù)器的時(shí)候,我們也需要提供相應(yīng)的認(rèn)證信息如下:
spring.boot.admin.client.instance.metadata.user.name=admin spring.boot.admin.client.instance.metadata.user.password=admin spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin
好了,登錄頁(yè)面和權(quán)限認(rèn)證也完成了。
Hazelcast集群
Spring Boot Admin 支持Hazelcast的集群,我們先添加依賴如下:
<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> <version>3.12.2</version> </dependency>
然后添加Hazelcast的配置:
@Configuration public class HazelcastConfig { @Bean public Config hazelcast() { MapConfig eventStoreMap = new MapConfig("spring-boot-admin-event-store") .setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) .setEvictionPolicy(EvictionPolicy.NONE) .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100)); MapConfig sentNotificationsMap = new MapConfig("spring-boot-admin-application-store") .setInMemoryFormat(InMemoryFormat.OBJECT) .setBackupCount(1) .setEvictionPolicy(EvictionPolicy.LRU) .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100)); Config config = new Config(); config.addMapConfig(eventStoreMap); config.addMapConfig(sentNotificationsMap); config.setProperty("hazelcast.jmx", "true"); config.getNetworkConfig() .getJoin() .getMulticastConfig() .setEnabled(false); TcpIpConfig tcpIpConfig = config.getNetworkConfig() .getJoin() .getTcpIpConfig(); tcpIpConfig.setEnabled(true); tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1")); return config; } }
本文的例子可以參考 https://github.com/ddean2009/learn-springboot2/tree/master/springboot-admin
總結(jié)
以上所述是小編給大家介紹的Spring Boot Admin(監(jiān)控工具)的使用,希望對(duì)大家有所幫助!
相關(guān)文章
使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法
這篇文章主要介紹了使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法,需要的朋友可以參考下2017-08-08關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問題
這篇文章主要介紹了關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08從繁瑣到簡(jiǎn)潔的Jenkins?Pipeline腳本優(yōu)化實(shí)踐
這篇文章主要為大家介紹了從繁瑣到簡(jiǎn)潔的Jenkins?Pipeline腳本優(yōu)化實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Mybatis-Plus實(shí)體類注解方法與mapper層和service層的CRUD方法
CRUD是指在做計(jì)算處理時(shí)的增加(Create)、讀取查詢(Retrieve)、更新(Update)和刪除(Delete)幾個(gè)單詞的首字母簡(jiǎn)寫。主要被用在描述軟件系統(tǒng)中DataBase或者持久層的基本操作功能,下面讓我們一起看看吧2022-03-03Spring加載配置和讀取多個(gè)Properties文件的講解
今天小編就為大家分享一篇關(guān)于Spring加載配置和讀取多個(gè)Properties文件的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03springboot應(yīng)用訪問zookeeper的流程
這篇文章主要介紹了springboot應(yīng)用訪問zookeeper的流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01詳解java調(diào)用存儲(chǔ)過程并封裝成map
這篇文章主要介紹了詳解java調(diào)用存儲(chǔ)過程并封裝成map的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09