SpringBoot Admin應用監控搭建


簡介

Spring Boot Admin 用於監控基於 Spring Boot 的應用,它是在 Spring Boot Actuator 的基礎上提供簡潔的可視化 WEB UI。

參考手冊地址:http://codecentric.github.io/spring-boot-admin/2.1.1/

Spring Boot Admin 是由服務端Server和客戶端Client組成

  • 服務端
    • 1.創建一個springboot工程並添加以下依賴
       1 <dependencies>
       2      <dependency>
       3          <groupId>org.springframework.boot</groupId>
       4          <artifactId>spring-boot-starter-web</artifactId>
       5      </dependency>
       6 
       7      <dependency>
       8          <groupId>de.codecentric</groupId>
       9          <artifactId>spring-boot-admin-server</artifactId>
      10          <version>2.1.1</version>
      11      </dependency>
      12 
      13      <dependency>
      14          <groupId>de.codecentric</groupId>
      15          <artifactId>spring-boot-admin-server-ui</artifactId>
      16          <version>2.1.1</version>
      17      </dependency>
      18 
      19      <dependency>
      20          <groupId>org.springframework.boot</groupId>
      21          <artifactId>spring-boot-starter-security</artifactId>
      22       </dependency>
      23 </dependencies>
    • 2.重寫權限控制類(非必要)
       1 @Configuration
       2 public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
       3     private final String adminContextPath;
       4 
       5     public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
       6         this.adminContextPath = adminServerProperties.getContextPath();
       7     }
       8 
       9     @Override
      10     protected void configure(HttpSecurity http) throws Exception {
      11         SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
      12         successHandler.setTargetUrlParameter("redirectTo");
      13         successHandler.setDefaultTargetUrl(adminContextPath + "/monitor");
      14 
      15         http.authorizeRequests()
      16                 .antMatchers(adminContextPath + "/assets/**").permitAll()
      17                 .antMatchers(adminContextPath + "/login").permitAll()
      18                 .anyRequest().authenticated()
      19                 .and()
      20                 .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
      21                 .logout().logoutUrl(adminContextPath + "/logout").and()
      22                 .httpBasic().and()
      23                 .csrf()
      24                 .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
      25                 .ignoringAntMatchers(
      26                         adminContextPath + "/instances",
      27                         adminContextPath + "/actuator/**"
      28                 );
      29     }
      30 }
       1 @EnableWebSecurity
       2 public class WebSecurityConfig implements WebMvcConfigurer {
       3 
       4     @Bean
       5     public UserDetailsService userDetailsService() throws Exception {
       6         InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
       7         manager.createUser(User.withDefaultPasswordEncoder().username("root").password("root").roles("administrator").build());
       8         return manager;
       9     }
      10 }
    • 3.啟動類添加注解@EnableAdminServer
      1 @SpringBootApplication
      2 @EnableAdminServer
      3 public class AdminServerApplication{
      4 
      5     public static void main(String[] args) {
      6         SpringApplication.run(AdminServerApplication.class, args);
      7     }
      8 }
    • 4.配置application.yml
      1 server:
      2   port: 8081
      3  
      4 spring:
      5   boot:
      6     admin:
      7       context-path: monitor
    • 5.瀏覽器訪問該項目
  • 客戶端
    • 1.在需要被監控的應用里面添加如下依賴(注意版本號要與server端相同)
      1 <dependency>
      2       <groupId>de.codecentric</groupId>
      3       <artifactId>spring-boot-admin-starter-client</artifactId>
      4       <version>2.1.1</version>
      5 </dependency>
    • 2.編輯配置文件(詳細屬性配置參考手冊)
      1 spring.boot.admin.client.url = http://localhost:8081/admin-server/monitor
      2 spring.boot.admin.client.username = root
      3 spring.boot.admin.client.password = root
      4 spring.boot.admin.client.instance.service-base-url=http://localhost:8080
      5 spring.boot.admin.client.instance.name = dida
      6 
      7 management.endpoints.web.exposure.include = *
      8 management.endpoint.health.show-details = ALWAYS
  • 刷新服務端

問題記錄

   按照上面的操作,我把公司的一個項目配置成了客戶端。在我的win10系統電腦上面運行這兩個程序是沒有問題的。但是當實際部署在linux服務器上面的時候就遇到了問題:我把springbootAdmin的server端部署到了linux服務器(服務器1),把要監控的項目部署到了另一台Linux服務器(服務器2),這兩台服務器處於同一個局域網內。運行這兩個項目,發現程序server端能夠發現這個我的client項目,但是client項目一直處於斷開狀態。

 錯誤日志

WARN  de.codecentric.boot.admin.client.registration.ApplicationRegistrator.register Line:115 - Failed to register application as Application
(name=dida, managementUrl=http://www.xxx.com:8081/dida/actuator, healthUrl=http://www.xxx.com:8081/dida/actuator/health,serviceUrl=http://www.xxx.com:8081/dida)
at spring-boot-admin ([http://admin.xxx.com:8080/monitor/instances]): 404 null. Further attempts are logged on DEBUG level

經過多次嘗試,發現spring.boot.admin.client.url 該項目配置應該配置ip地址 http://172.18.0.188:8080/monitor,而不是域名。

INFO  de.codecentric.boot.admin.client.registration.ApplicationRegistrator.register Line:98 - Application registered itself as 2449779efa06

日志顯示注冊成功。

待補充 ... ...

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM