SpringCloudNetflix(15)SpringBootAdmin客户端

2020-04-23

创建 Spring Boot Admin Client

创建一个工程名为 monitoring-client 的项目,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>

    <parent>
        <groupId>top.misterzhang</groupId>
        <artifactId>dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>monitoring-client</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>
        <!--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>

        <!--健康状态客户端-->
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </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.client.MonitoringClientApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

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

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

Application

程序入口类没有特别需要修改的地方

package top.misterzhang.monitoring.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author zhangfan
 * @date 2020/4/23
 * @SpringBootApplication       SpringBoot场景启动器
 * @EnableDiscoveryClient
 */
@SpringBootApplication
@EnableDiscoveryClient
public class MonitoringClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitoringClientApplication.class,args);
    }
}

application.yml

设置端口号为:8085,并设置 Spring Boot Admin 的服务端地址

spring:
  application:
  #服务名字
    name: monitoring-client
  boot:
    admin:
      client:
      #SpringBootAdmin服务地址
        url: http://localhost:8084
  #链路追踪服务地址
  zipkin:
    base-url: http://localhost:9411

server:
#端口号
  port: 8085

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

主要增加了 Spring Boot Admin Client 相关配置

spring:
  boot:
    admin:
      client:
        url: http://localhost:8084

需要监控的服务都要导入依赖添加配置

测试服务监控

把多个应用都添加为客户端依次启动多个应用,打开浏览器访问:http://localhost:8084 界面显示如下
image.png
从图中可以看到,我们的服务已经上线了,至此说明监控中心搭建成功

WallBoard

image.png


标题:SpringCloudNetflix(15)SpringBootAdmin客户端
作者:张范
地址:http://misterzhang.top/articles/2020/04/10/1586510637083.html