<div class="show-content-free"> <blockquote> <h3>Spring Boot Admin 用於監控基於 Spring Boot 的應用,它是在 Spring Boot Actuator 的基礎上提供簡潔的可視化 WEB UI。</h3> </blockquote> <h2>1. 什麼是Spring Boot Admin?</h2> <hr> <p>Spring Boot Admin(<strong>下文簡稱SBA</strong>)是一個社區開源項目,用於管理和監控你的Spring Boot應用。應用經過SBA Client註冊到SBA Server中,可經過HTTP請求或者Spring Cloud發現(例如Eureka、Consul),UI展現經過Vue在Spring Boot Actuator端點上獲取應用監控數據進行管理。</p> <h2>2. 開始使用</h2> <hr> <h3>2,1 配置好你的SBA Server服務端程序</h3> <h4>2.1.1 添加spring-boot-admin-starter-server的Maven依賴到你的pom.xml中:</h4> <pre class="hljs xml"><code class="xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>de.codecentric<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-admin-starter-server<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.0.2<span class="hljs-tag"></<span class="hljs-name">version</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>de.codecentric<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-admin-server-ui<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.0.2<span class="hljs-tag"></<span class="hljs-name">version</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-security<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-web<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> </code></pre> <h4>2.1.2 打開你的Spring Boot啓動文件中添加@EnableAdminServer 註解用於激活SBA Server配置:</h4> <pre class="hljs java"><code class="java"><span class="hljs-keyword">import</span> de.codecentric.boot.admin.server.config.EnableAdminServer; <span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication; <span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;java
<span class="hljs-meta">@SpringBootApplication</span> <span class="hljs-meta">@EnableAdminServer</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span> </span>{git
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{ SpringApplication.run(Application.class, args); }
} </code></pre>github
<p><strong>注意若是你須要將應用以war形式部署,請將啓動文件作以下改變:</strong></p> <pre class="hljs java"><code class="java"><span class="hljs-keyword">import</span> de.codecentric.boot.admin.server.config.EnableAdminServer; <span class="hljs-keyword">import</span> org.springframework.boot.SpringApplication; <span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication; <span class="hljs-keyword">import</span> org.springframework.boot.builder.SpringApplicationBuilder; <span class="hljs-keyword">import</span> org.springframework.boot.web.servlet.support.SpringBootServletInitializer;web
<span class="hljs-meta">@SpringBootApplication</span> <span class="hljs-meta">@EnableAdminServer</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Application</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">SpringBootServletInitializer</span> </span>{spring
<span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> SpringApplicationBuilder <span class="hljs-title">configure</span><span class="hljs-params">(SpringApplicationBuilder builder)</span> </span>{ <span class="hljs-keyword">return</span> builder.sources(Application.class); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{ SpringApplication.run(Application.class, args); }
} </code></pre>瀏覽器
<h4>2.1.3 在你的SBA Server中的 application.yml 中進行相關配置:</h4> <pre class="hljs bash"><code class="bash">spring: <span class="hljs-comment"># 配置SBA Client鏈接的安全帳號密碼</span> security: user: name: admin password: admin boot: admin: ui: <span class="hljs-comment"># 修改網頁顯示的tab標題</span> title: <span class="hljs-string">"應用監控管理"</span> <span class="hljs-comment"># 修改網頁的brand的圖標和標題</span> brand: <span class="hljs-string">"<img src='assets/img/icon-spring-boot-admin.svg'><span>應用監控管理</span>"</span> server: port: 7070 </code></pre> <h4>2.1.4 進行Spring Security相關配置:</h4> <pre class="hljs java"><code class="java"><span class="hljs-keyword">import</span> de.codecentric.boot.admin.server.config.AdminServerProperties; <span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.builders.HttpSecurity; <span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; <span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; <span class="hljs-keyword">import</span> org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; <span class="hljs-keyword">import</span> org.springframework.security.web.csrf.CookieCsrfTokenRepository;安全
<span class="hljs-meta">@EnableWebSecurity</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SecurityConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WebSecurityConfigurerAdapter</span> </span>{ <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String adminContextPath;bash
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SecurityConfig</span><span class="hljs-params">(AdminServerProperties adminServerProperties)</span> </span>{ <span class="hljs-keyword">this</span>.adminContextPath = adminServerProperties.getContextPath(); } <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">configure</span><span class="hljs-params">(HttpSecurity http)</span> <span class="hljs-keyword">throws</span> Exception </span>{ SavedRequestAwareAuthenticationSuccessHandler successHandler = <span class="hljs-keyword">new</span> SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter(<span class="hljs-string">"redirectTo"</span>); successHandler.setDefaultTargetUrl(adminContextPath + <span class="hljs-string">"/"</span>); http.authorizeRequests() .antMatchers( adminContextPath + <span class="hljs-string">"/assets/**"</span>, adminContextPath + <span class="hljs-string">"/login"</span> ).permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminContextPath + <span class="hljs-string">"/login"</span>).successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + <span class="hljs-string">"/logout"</span>).and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( <span class="hljs-string">"/instances"</span>, <span class="hljs-string">"/actuator/**"</span>, adminContextPath + <span class="hljs-string">"/logout"</span> ); }
} </code></pre>網絡
<h3>2.2 註冊你的客戶端應用</h3> <blockquote> <p>爲了註冊你的應用在SBA Server中,你能夠使用SBA Client 或者使用Spring Cloud Discovery(例如:Eureka, Consul, ...)。</p> </blockquote> <h4>2.2.1 配置你的Spring Boot Admin客戶端</h4> <p>每個想註冊的應用都必須包含SBA Client,同時爲了確保Actuator端點的安全,建議添加 <strong>spring-boot-starter-security</strong> 依賴用於保護端點安全訪問。<br> 添加 <strong>spring-boot-admin-starter-client</strong> 的Maven依賴到你的pom.xml中:</p> <pre class="hljs xml"><code class="xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>de.codecentric<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-admin-starter-client<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.0.2<span class="hljs-tag"></<span class="hljs-name">version</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> <span class="hljs-comment"><!-- 可選擇添加下面依賴 --></span> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-security<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"></<span class="hljs-name">dependency</span>></span> </code></pre> <h4>2.2.2. 在你的SBA Client中的 <strong>application.yml</strong> 中配置你的SBA Server的服務地址:</h4> <pre class="hljs bash"><code class="bash">spring: boot: admin: client: <span class="hljs-comment"># 這個URL地址是SBA Server的服務地址,你須要將你的應用註冊到該地址上</span> url: http://localhost:7070 <span class="hljs-comment"># 配置鏈接到監測管理平臺的Security安全密碼</span> username: admin password: admin instance: metadata: <span class="hljs-comment"># 配置發送到SBA Server的SBA Client的端點安全密碼</span> user.name: <span class="hljs-variable">${spring.security.user.name}</span> user.password: <span class="hljs-variable">${spring.security.user.password}</span> security: user: name: root password: root <span class="hljs-comment"># 官方文檔中有提到, SpringBoot 的 Logging 配置的級別有7個:TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF</span> logging: <span class="hljs-comment"># 設置日誌保存的路徑,path和file只須要設置一個,指定path則日誌名稱固定爲spring.log</span> path: /java-log <span class="hljs-comment"># 打印日誌的級別</span> level: root: info <span class="hljs-comment"># 在默認狀況下大多數Actuator的端點並無徹底公開,這裏我將全部端點都進行公開進行管理</span> management.endpoints.web.exposure.include: <span class="hljs-string">"*"</span> app
</code></pre>
<h4>2.2.3. 若是你添加了 <strong>spring-boot-starter-security</strong> 依賴,你須要進行以下配置使SBA Server服務端可以訪問SBA Client客戶端的Actuator端點:</h4> <pre class="hljs java"><code class="java"><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration; <span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.builders.HttpSecurity; <span class="hljs-keyword">import</span> org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
<span class="hljs-meta">@Configuration</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SecurityConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WebSecurityConfigurerAdapter</span> </span>{
<span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">configure</span><span class="hljs-params">(HttpSecurity http)</span> <span class="hljs-keyword">throws</span> Exception </span>{ http.httpBasic().and() .authorizeRequests().antMatchers(<span class="hljs-string">"/actuator/**"</span>).authenticated() .anyRequest().permitAll(); }
} </code></pre>
<h3>3.至此相關配置已經完成,如今讓咱們啓動SBA Server服務端程序,打開瀏覽器輸入<a href="http://localhost:7070" target="_blank" rel="nofollow">http://localhost:7070</a> 訪問它。</h3>
<div class="image-container" style="max-width: 700px; max-height: 345px; background-color: transparent;"> <div class="image-view" data-width="1920" data-height="949"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-e8258ce3badab3be.png" data-original-width="1920" data-original-height="949" data-original-format="image/png" data-original-filesize="52194" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150321306-288700741.png"></div> </div><br><br><br> <p>咱們能夠看到SBA Server跳轉到了登陸頁面提示咱們登陸,這時候咱們輸入咱們在application.yml中配置的帳號密碼進行登陸:</p> <br> <div class="image-container" style="max-width: 431px; max-height: 397px; background-color: transparent;"> <div class="image-view" data-width="431" data-height="397"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-83269364e36c29d0.png" data-original-width="431" data-original-height="397" data-original-format="image/png" data-original-filesize="17275" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150411912-1666681706.png"></div> </div><br><br> <p>輸入用戶名和密碼後點擊Login後跳轉到咱們的應用監控管理首頁:</p> <br> <div class="image-container" style="max-width: 700px; max-height: 345px; background-color: transparent;"> <div class="image-view" data-width="1920" data-height="949"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-72bd1b0d05d4b874.png" data-original-width="1920" data-original-height="949" data-original-format="image/png" data-original-filesize="50527" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150454196-1804564636.png"></div> </div><br><br> 這時咱們看到是沒有應用註冊進來的,頁面是數據是空白的,整個頁面簡潔乾淨,接下去讓咱們啓動一個SBA Client客戶端程序註冊進來<br> <div class="image-container" style="max-width: 700px; max-height: 345px; background-color: transparent;"> <div class="image-view" data-width="1920" data-height="949"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-575f1b73a9f994b6.png" data-original-width="1920" data-original-height="949" data-original-format="image/png" data-original-filesize="52659" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150536704-1308118067.png"></div> </div><br><br> 啓動一個SBA Client後,能夠看到頁面上出現了一個客戶端程序註冊進來了<br> <div class="image-container" style="max-width: 700px; max-height: 345px; background-color: transparent;"> <div class="image-view" data-width="1920" data-height="949"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-d84eb022f3069fe7.png" data-original-width="1920" data-original-height="949" data-original-format="image/png" data-original-filesize="83527" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150609688-626626406.png"></div> </div> <p>下圖是你的應用的基本信息查看,包括線程使用圖表,堆內存使用圖表,非堆內存使用圖表等等</p><br><br> <div class="image-view" data-width="1920" data-height="1080"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-3c2c62382daf9be1.png" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="104437" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150642488-341401263.png"></div> <br> <p>下圖是你的應用的日誌打印</p> <br> <div class="image-view" data-width="1920" data-height="1080"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-3aac83ee693a9351.png" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="202490" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150713228-1347024027.png"></div> <br> <p>下圖是你的應用的網絡URL請求狀況監控</p> <br><br> <div class="image-container" style="max-width: 700px; max-height: 393px; background-color: transparent;"> <div class="image-view" data-width="1920" data-height="1080"><img data-original-src="//upload-images.jianshu.io/upload_images/7089499-7b0fa09063c0d95a.png" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="152871" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201906/1112483-20190610150744789-193538763.png"></div> </div> <br><br><br> <h3>這裏就不過多演示了,能夠看出這個經過這個監控平臺咱們能夠方便快捷的管理監控咱們的應用,不再用打開Linux命令行查看咱們項目運行的狀況,怎麼樣功能是否是很強大。</h3> <p>最後貼上如下官方文檔地址<br> <a href="http://codecentric.github.io/spring-boot-admin/2.0.2/#_what_is_spring_boot_admin" target="_blank" rel="nofollow">spring-boot-admin 官方文檔地址</a><br> <a href="https://github.com/codecentric/spring-boot-admin" target="_blank" rel="nofollow">spring-boot-admin GitHub地址</a></p>
</div>