SpringCloudNetflix(14)SpringBootAdmin服务端

2020-04-23

随着开发周期的推移,项目会不断变大,切分出的服务也会越来越多,这时一个个的微服务构成了错综复杂的系统。对于各个微服务系统的健康状态、会话数量、并发数、服务资源、延迟等度量信息的收集就成为了一个挑战。Spring
Boot Admin 应运而生,它正式基于这些需求开发出的一套功能强大的监控管理系统。
Spring Boot Admin 有两个角色组成,一个是 Spring Boot Admin Server,一个是 Spring Boot Admin Client。

创建 Spring Boot Admin Server

创建一个工程名为 monitoring-server 的项目,pom.xml 文件如下:
在总的pom中托管依赖

        <!--SpringBootAdmin服务监控客户端,依赖版本托管-->
        <de.codecentric.client.version>2.0.1</de.codecentric.client.version>
        <!--SpringBootAdmin服务监控服务端,依赖版本托管-->
        <de.codecentric.server.version>2.0.0</de.codecentric.server.version>

dependencyManagement中添加

<!--&lt;!&ndash;SpringBootAdmin服务监控服务端,依赖声明&ndash;&gt;-->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>${de.codecentric.server.version}</version>
            </dependency>
            <!--SpringBootAdmin服务监控客户端,依赖声明-->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>${de.codecentric.client.version}</version>
            </dependency>

添加完成之后总的POM为

<?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">
    <!--pom文件的模型版本是4.0.0-->
    <modelVersion>4.0.0</modelVersion>

    <!--继承Spring Boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <!--组名,比方说在百度干,就是域名反写,com.baidu-->
    <groupId>top.misterzhang</groupId>
    <!--和下边的name一样,最好这个就是项目名-->
    <artifactId>dependencies</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <!--做项目依赖为pom-->
    <packaging>pom</packaging>

    <!--项目描述可以删除-->
    <name>dependencies</name>

    <properties>
        <!-- Environment Settings -->
        <java.version>1.8</java.version>
        <!--文件拷贝时的编码-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!--托管版本-->
        <!-- Spring Cloud 的版本 -->
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <!--&lt;!&ndash;托管链路追踪中,zipkin版本&ndash;&gt;-->
        <zipkin.version>2.10.1</zipkin.version>
        <!--SpringBootAdmin服务监控客户端,依赖版本托管-->
        <de.codecentric.client.version>2.0.1</de.codecentric.client.version>
        <!--SpringBootAdmin服务监控服务端,依赖版本托管-->
        <de.codecentric.server.version>2.0.0</de.codecentric.server.version>
    </properties>

    <!--声明SpringCloud使用的过程中可能会用到的依赖-->
    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud依赖,一定要放到dependencyManagement中,起到管理版本的作用即可 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--&lt;!&ndash; 声明链路追踪所用三个依赖的版本 &ndash;&gt;-->
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin</artifactId>
                <!--注入托管的版本-->
                <version>${zipkin.version}</version>
            </dependency>
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin-server</artifactId>
                <version>${zipkin.version}</version>
            </dependency>
            <dependency>
                <groupId>io.zipkin.java</groupId>
                <artifactId>zipkin-autoconfigure-ui</artifactId>
                <version>${zipkin.version}</version>
            </dependency>
            <!--&lt;!&ndash;SpringBootAdmin服务监控服务端,依赖声明&ndash;&gt;-->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>${de.codecentric.server.version}</version>
            </dependency>
            <!--SpringBootAdmin服务监控客户端,依赖声明-->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>${de.codecentric.client.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- Compiler 插件, 设定 JDK 版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <configuration>
                            <archive>
                                <manifest>
                                    <!-- Add directory entries -->
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                    <addClasspath>true</addClasspath>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- resource -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>

            <!-- install -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
            </plugin>

            <!-- clean -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>

            <!-- ant -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>

            <!-- dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <!-- Java Document Generate -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!--YUI Compressor (CSS/JS压缩) =================报红 -->
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>yuicompressor-maven-plugin</artifactId>
                    <version>1.5.1</version>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>compress</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <jswarn>false</jswarn>
                        <nosuffix>true</nosuffix>
                        <linebreakpos>30000</linebreakpos>
                        <force>true</force>
                        <includes>
                            <include>**/*.js</include>
                            <include>**/*.css</include>
                        </includes>
                        <excludes>
                            <exclude>**/*.min.js</exclude>
                            <exclude>**/*.min.css</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <!-- 资源文件配置 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <!--配置远程仓库-->
    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <!--远程仓库地址-->
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>sonatype-repos</id>
            <name>Sonatype Repository</name>
            <!--远程仓库地址-->
            <url>https://oss.sonatype.org/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-repos-s</id>
            <name>Sonatype Repository</name>
            <!--远程仓库地址-->
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <!--远程仓库地址-->
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <!--远程仓库地址-->
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!--阿里云的依赖仓库-->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <!--阿里云的远程仓库地址-->
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <!--是关闭的-->
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

在项目中添加依赖

<?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">
    <!--POM文件的模型版本使用4.0.0-->
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>top.misterzhang</groupId>
        <!--继承自谁的依赖-->
        <artifactId>dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <!--父级依赖的相对路径-->
        <relativePath>../dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>monitoring-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--SpringBoot的Web场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Tomcat的场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!--新的反应式Web框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <!--SpringBoot健康监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--SpringBoot测试场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--SpringBootAdmin服务所有服务可视化监控的场景启动器-->
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>${de.codecentric.server.version}</version>
        </dependency>

        <!--链路追踪的场景启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <!--注册到注册中心的场景启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>top.misterzhang.monitoring.server.MonitoringServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要增加了 2 个依赖,org.jolokia:jolokia-corede.codecentric:spring-boot-admin-starter-server

<dependency>
    <groupId>org.jolokia</groupId>
    <artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

其中 spring-boot-admin-starter-server 的版本号为:2.0.0,这里没写版本号是因为我已将版本号托管到 dependencies 项目中

Application

通过 @EnableAdminServer 注解开启 Admin 功能

package top.misterzhang.monitoring.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author zhangfan
 * @date 2020/4/23
 * @SpringBootApplication       SpringBoot场景启动器
 * @EnableEurekaClient          注册到注册中心
 * @EnableAdminServer           开启监控管理系统服务端
 */
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class MonitoringServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitoringServerApplication.class,args);
    }
}

application.yml

设置端口号为:8084

spring:
  application:
  #服务名字
    name: monitoring-server
    #链路追踪服务端的服务地址
  zipkin:
    base-url: http://localhost:9411

server:
#端口号
  port: 8084

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: health,info

eureka:
  client:
    serviceUrl:
    #注册中心地址
      defaultZone: http://localhost:8761/eureka/

测试访问监控中心

打开浏览器访问:http://localhost:8084 会出现以下界面
image.png


标题:SpringCloudNetflix(14)SpringBootAdmin服务端
作者:张范
地址:http://misterzhang.top/articles/2020/04/10/1586500525398.html