再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

spring boot是當前熱門的java 微服務開發框架,更新速度也極其快速,每當我們差不多快掌握了某一個框架技術之後,新的版本又更新了,增加了很多特性,不得不跟著學,不禁有人吐槽:再更新我就學不動, 再更新我就瘋了,真的是活到老學到老。就spring boot admin 來說從1.5一下子更新到2.0版本了,界面就完全不一樣了。下面我們調戲一下她吧!

spring boot admin 簡介

spring boot admin 是一個監控中心組件,用於監控各個spring boot 服務的各個指標,比如cpu使用率,jvm內存,線程池,調用跟蹤,heapdump等等。各個spring boot 服務可以註冊到spring boot admin server端,就可以被監控中心採集並展示數據指標,供開發人員監控服務的運行狀況,並且可以在達到一定的報錯情況進行通知推送。各個監控的對象要接入spring boot admin server 有兩種方法,一種是各個接入的服務添加pom依賴和屬性配置,另外一種是通過spring boot admin server去註冊中心拿監控的服務,前者沒有註冊中心,下面我們著重講講第一種。

工具和環境:mavne3.5,jdk1.8,idea

第一步:使用spring initializr創建一個springboot admin 服務端的project,選擇spring boot 2.0.4的版本,並選擇依賴的組件,我們服務端只需要web,actuator,spring boot admin server3個組件即可。

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

創建好工程之後。在file->setting輸入mavne,找到maven設置項,設置下mavne的setting配置文件和本地倉庫的目錄,根據自己的情況設置並保存。同時也要確認下對應的jdk的選項。

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

在file->project structure裡面設置jdk

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

在pom裡面添加jolokia-core的依賴,

org.jolokia

jolokia-core

1.5.0

點擊idea右下角的enable-import按鈕,至此,就創建了一個spring boot admin server空項目。


第二步:在上一步建立的空項目中添加一些代碼和配置。我們在主類中添加@EnableAdminServer註解,表示該類是一個spring boot admin server服務。刪除static,template空目錄,同時編輯application.properties文件,設置端口號和程序名稱即可

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

啟動工程,打開,即可進入spring-admin-server的管理界面,說明我們已經建立好了一個簡單的spring boot admin監控服務了,這個時候,我們看到的是空的,沒有監控到任何程序。接下來我們需要把需要監控的項目添加進來,現在我們建立一個spring –boot admin的client,作為被監控的對象。

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控


第三步:我們還是和第一步一樣建立一個spring-boot-admin server的工程,這時我們的依賴組件選擇web,actuator,spring boot admin client。

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

其他同第一步一樣,然後編輯application.properties文件,設置端口號,應用名稱,監控中心的url。啟動spring boot的spring boot admin client程序。

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

這個時候我們看到了監控中心多了一個監控應用了,就是我們剛剛啟動的應用。但是還沒有達到我們的監控目的,因此我們需要添加一些配置。如圖:

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

同時添加幾個controller類,用於展示監控內容

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

請求這些controller接口後,再到後臺去查看監控信息,我們可以看到一些信息,

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

到此,整個spring boot admin2.0的監控就搭建成功了。


第四步:如果需要將spring boot admin server管理界面設置登錄界面,我們需要繼承WebSecurityConfigurerAdapter,重寫configure方法:

@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 { 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() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( "/instances", "/actuator/**" ); }}

我們還需要在server端的配置文件裡面添加一些配置項,再重啟server,進入頁面後就可以看到登錄界面了

# 這個是登錄界面的賬號密碼spring.security.user.name=adminspring.security.user.password=123456# These two are needed so that the client can register at the protected server apispring.boot.admin.client.username=adminspring.boot.admin.client.password=123456# These two are needed so that the server can access the protected client endpointsspring.boot.admin.client.instance.metadata.user.name=adminspring.boot.admin.client.instance.metadata.user.password=123456

同時clinet端也添加配置:

spring.security.user.name=adminspring.security.user.password=123456# These two are needed so that the client can register at the protected server apispring.boot.admin.client.username=adminspring.boot.admin.client.password=123456# These two are needed so that the server can access the protected client endpointsspring.boot.admin.client.instance.metadata.user.name=adminspring.boot.admin.client.instance.metadata.user.password=123456
再更新我就學不動了!30分鐘學會spring boot admin 2.0監控

我們還可以選擇性的監控對象,需要在每個client添加一個配置,如下表示監控除logfile,heapdump,auditevents以外所有的

management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=logfile,heapdump,auditeventsmanagement.endpoint.health.show-details=always

後續的其他細節配置,參考官方的說明文檔http://codecentric.github.io/spring-boot-admin/2.0.2/。

再更新我就老了,這句話其實是不對的,作為程序員,應該是活到老學到老,學無止境!

大家有什麼問題或者文章有什麼不對的地方歡迎批評吐槽,多多交流,多多評論,謝謝大家了!


分享到:


相關文章: