為springboot項目添加springboot-admin監控


我們知道spring-boot-actuator暴露了大量統計和監控信息的端點,spring-boot-admin
就是為此提供的監控項目。

先來看看大概會提供什么樣的功能

從圖中可以看出,主要內容都是由spring boot actuator來提供的。更多關於actuator的信息參見Springboot actuator

接下來,hello world時間。

server端

server端是一個單獨的springboot項目,主要負責收集和展示監控指標,提供了ui page。

項目地址: https://github.com/Ryan-Miao/springboot-admin-demo

新建一個springboot項目,可以使用idea自帶的spring initializr. 主要添加以下兩個依賴,

<springboot.version>1.5.13.RELEASE</springboot.version>
<spring-boot-admin.version>1.5.7</spring-boot-admin.version>
    
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-dependencies</artifactId>
        <version>${spring-boot-admin.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
    <dependencies>
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
      </dependency>
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-server-ui</artifactId>
      </dependency>
    </dependencies>

在啟動類上添加@EnableAdminServer

在配置文件中設置服務地址,這里假設為8081

server.port=8081

啟動。

client 端

client端是指我們正常的app,我們的spring boot項目就是。在原有的基礎上添加依賴

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-starter-client</artifactId>
    </dependency>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${springboot.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>build-info</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

之后配置相關信息:

client端添加admin的url,這里暫時忽略spring security

spring:
  boot:
    admin:
      url: http://localhost:8081
management:
  security:
    enabled: false

然后,啟動我們的app。訪問localhost:8081就可以看到文章開始圖片里的信息了。

更多文檔: https://codecentric.github.io/spring-boot-admin/1.5.0/#_what_is_spring_boot_admin


免責聲明!

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



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