Spring Boot 管理(Admin)客户端

  • Spring Boot 管理(Admin)客户端

    为了通过Spring Boot Admin Server监视和管理您的微服务应用程序,您应该添加Spring Boot Admin入门客户端依赖项,并在应用程序属性文件中指出Admin Server URI。
    –为了监视应用程序,应该为微服务应用程序启用Spring Boot Actuator端点。
    首先,在构建配置文件中添加以下Spring Boot Admin Starter Client依赖项和Spring Boot Starter Actuator依赖项。
    
    <dependency>
       <groupId>de.codecentric</groupId>
       <artifactId>spring-boot-admin-starter-client</artifactId>
       <version>2.2.2</version>
    </dependency>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    Gradle用户可以在build.gradle文件中添加以下依赖项。
    
    implementation 'de.codecentric:spring-boot-admin-starter-client:2.2.2'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    
    现在,将Spring Boot Admin Server URL添加到您的应用程序属性文件中。对于属性文件用户,在application.properties文件中添加以下属性。
    
    spring.application.name=admin-client  
    spring.boot.admin.server.url=http://localhost:9090 
    server.port=8081
    management.endpoints.web.exposure.include=*
    management.endpoint.health.show-details=ALWAYS
    
    对于YAML用户,在application.yml文件中添加以下属性。
    
    spring:
      application:
        name: admin-client
      boot:
        admin:
          server:
            url: http://localhost:9090
    server:
      port: 8081
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
    
    完整构建配置如下:
    生成配置文件如下。 对于Maven用户– 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>2.3.0.BUILD-SNAPSHOT</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.jc2182</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
               <groupId>de.codecentric</groupId>
               <artifactId>spring-boot-admin-client</artifactId>
               <version>2.2.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
     
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </pluginRepository>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    
    Gradle – build.gradle
    
    plugins {
        id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
        id 'java'
    }
    
    group = 'com.jc2182'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
        maven { url 'https://repo.spring.io/snapshot' }
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'de.codecentric:spring-boot-admin-client:2.2.2' 
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    
    test {
        useJUnitPlatform()
    }
    
    对于Maven,请使用以下命令
    
    mvn clean install
    
    在“BUILD SUCCESS”之后,您可以在target目录下找到JAR文件。
    对于Gradle,请使用以下命令-
    
    gradle clean build
    
    在“BUILD SUCCESSFUL”之后,您可以在build/libs目录下找到JAR文件。
    现在,使用如下所示的命令运行JAR文件:
    
    java –jar <JARFILE>
    
    启动此程序前应启动上一章介绍的服务器
    现在,该应用程序已在Tomcat端口8081上启动,如下所示-
    现在,从Web浏览器中点击以下URL,然后查看Admin Server UI。 http://localhost:9090/
    现在,单击“应用墙”>“详情”按钮,然后在Admin Server UI中查看执行器端点。