Spring Boot Admin Server

使用 Spring Boot Actuator Endpoint 监控应用程序有点困难。 因为,如果有“n”个应用程序,每个应用程序都有单独的执行器端点,因此难以监控。 Spring Boot Admin Server 是一个用于管理和监控您的微服务应用程序的应用程序。

为了处理这种情况,CodeCentric Team 提供了一个 Spring Boot Admin UI,用于在一个地方管理和监视所有 Spring Boot 应用程序的 Actuator 端点。

为了构建 Spring Boot 管理服务器,我们需要在构建配置文件中添加以下依赖项。

Maven 用户可以在您的 pom.xml 文件中添加以下依赖项

<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server</artifactId>
   <version>1.5.5</version>
</dependency>
<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server-ui</artifactId>
   <version>1.5.5</version>
</dependency>

Gradle 用户可以在 build.gradle 文件中添加以下依赖项

compile group: 'de.codecentric', name: 'spring-boot-admin-server', version: '1.5.5'
compile group: 'de.codecentric', name: 'spring-boot-admin-server-ui', version: '1.5.5'

在主 Spring Boot 应用程序类文件中添加 @EnableAdminServer 注解。 @EnableAdminServer 注解用于使我们作为 Admin Server 来监控所有其他微服务。

package com.jiyik;

import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

现在,在 application.properties 文件中定义 server.port 和应用程序名称,如图所示

server.port = 9090
spring.application.name = adminserver

对于 YAML 用户,使用以下属性在 application.yml 文件中定义端口号和应用程序名称。

server:
   port: 9090
spring:
   application:
      name: adminserver

构建配置文件如下所示。

对于 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jiyik</groupId>
    <artifactId>adminServerProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

对于 Gradle 用户 – build.gradle 文件

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   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.jiyik'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {   
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter')
   compile group: 'de.codecentric', name: 'spring-boot-admin-server', version: '1.5.5'
   compile group: 'de.codecentric', name: 'spring-boot-admin-server-ui', version: '1.5.5'   
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

现在,我们可以创建一个可执行的 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序。

对于 Maven,使用下面给出的命令

$ mvn clean install

成功之后,我们可以在 target 目录下找到 JAR 文件。

对于 Gradle,使用下面给出的命令

$ gradle clean build

这里我们使用 IDEA 来启动服务(读者也可以使用上面两种方式中的一种生成可执行 jar 包)

现在,应用程序已在 Tomcat 端口 9090 上启动,如下所示

pring boot admin server

现在从 Web 浏览器中访问以下 URL 并查看 Admin Server UI。

http://localhost:9090/

spring boot admin server ui

查看笔记

扫码一下
查看教程更方便