Spring Boot - 管理客戶端



要透過 Spring Boot Admin Server 監控和管理您的微服務應用程式,您應該新增 Spring Boot Admin 啟動器客戶端依賴項,並將 Admin Server URI 指向應用程式屬性檔案。

注意 − 要監控應用程式,您應該為您的微服務應用程式啟用 Spring Boot Actuator 端點。

要構建 Spring Boot Admin Server,我們需要在您的構建配置檔案中新增以下依賴項。

Maven 使用者可以在您的 pom.xml 檔案中新增以下依賴項 −

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

Gradle 使用者可以在您的 build.gradle 檔案中新增以下依賴項 −

compile group: 'de.codecentric', name: 'spring-boot-admin-starter-client', version: '3.3.3'
compile('org.springframework.boot:spring-boot-starter-actuator')

現在,將 Spring Boot Admin Server URL 新增到您的應用程式屬性檔案。

對於屬性檔案使用者,請在 application.properties 檔案中新增以下屬性。

spring.boot.admin.url = https://:9090/

對於 YAML 使用者,請在 application.yml 檔案中新增以下屬性。

spring:
   boot:
      admin:
         url: https://:9000/

建立 Spring Admin 客戶端

首先,從 Spring Initializer 頁面下載 Spring Boot 專案,並選擇 Spring Admin Client 和 Spring Boot Actuator。觀察下面的螢幕截圖 −

Creating Spring Admin Client

以下是 Spring Boot 主類。

AdminclientApplication.java

package com.tutorialspoint.adminclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AdminclientApplication {
   public static void main(String[] args) {
      SpringApplication.run(AdminclientApplication.class, args);
   }
}

現在,在 application.properties 檔案中定義 admin server url,如下所示 −

spring.boot.admin.url = https://:9090/
spring.application.name = adminclient

對於 YAML 使用者,請使用以下屬性在 application.yml 檔案中定義埠號和應用程式名稱。

spring:
   application:
      name: adminserver
   boot:
      admin:
         url: https://:9090/	  

構建配置檔案如下所示。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.3.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>adminclient</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>adminserver</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
      <spring-boot-admin.version>3.3.3</spring-boot-admin.version>
   </properties>
   <dependencies>
      <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>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <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>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

對於 Gradle 使用者 – build.gradle 檔案

buildscript {
   ext {
      springBootVersion = '3.3.3'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21
repositories {   
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter')
   compile('org.springframework.boot:spring-boot-starter-actuator')
   compile group: 'de.codecentric', name: 'spring-boot-admin-client', version: '3.3.3'
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

編譯和執行

您可以建立一個可執行的 JAR 檔案,並使用以下 Maven 或 Gradle 命令執行 Spring Boot 應用程式 −

對於 Maven,請使用此處顯示的命令 −

mvn clean install

“BUILD SUCCESS”之後,您可以在 target 目錄下找到 JAR 檔案。

對於 Gradle,請使用此處顯示的命令 −

gradle clean build

“BUILD SUCCESSFUL”之後,您可以在 build/libs 目錄下找到 JAR 檔案。

現在,使用下面給出的命令執行 JAR 檔案 −

java –jar <JARFILE> 

現在,應用程式已在 Tomcat 埠 9090 上啟動,如下所示 −


現在,從您的 Web 瀏覽器訪問以下 URL,檢視您的 Spring Boot 應用程式是否已在 Spring Boot Admin Server 上註冊。

https://:9090/

Web Browser Admin Server

現在,單擊詳細資訊按鈕,然後在 Admin Server UI 中檢視執行器端點。

Actuator Endpoints in Admin Server UI
廣告