Spring Boot Admin对SpringBoot服务进行监控

Spring Boot Admin对SpringBoot服务进行监控

前面文章使用 Spring Boot Actuator 监控应用介绍了 Spring Boot Actuator 的使用,Spring Boot Actuator 提供

了对单个 Spring Boot 的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了 Spring Boot 应

用的整个生命周期。

但是这样监控也有一些问题:第一,所有的监控都需要调用固定的接口来查看,如果全面查看应用状态需要调用很

多接口,并且接口返回的 Json 信息不方便运营人员理解;第二,如果 Spring Boot 应用集群非常大,每个应用都

需要调用不同的接口来查看监控信息,操作非常繁琐低效。在这样的背景下,就诞生了另外一个开源软件:

Spring Boot Admin

1、什么是Spring Boot Admin?

Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过

HTTP 或者使用 Eureka 注册到 admin server 中进行展示,Spring Boot Admin UI 部分使用 VueJs 将数据展示在

前端。这篇文章给大家介绍如何使用 Spring Boot Admin 对 Spring Boot 应用进行监控。

2、监控单体应用

这节给大家展示如何使用 Spring Boot Admin 监控单个 Spring Boot 应用。

2.1 父依赖


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.0.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-boot-admin-simpleartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-admin-simplename>
    <description>spring-boot-admin-simpledescription>
    <packaging>pompackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Edgware.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

2.2 Admin Server 端

2.2.1 项目依赖

<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.0modelVersion>
    <parent>
        <groupId>com.examplegroupId>
        <artifactId>spring-boot-admin-simpleartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-boot-admin-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-admin-servername>
    <description>spring-boot-admin-serverdescription>
    <packaging>jarpackaging>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-serverartifactId>
            <version>2.1.0version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>
2.2.2 配置文件
server.port=8000

服务端设置端口为:8000。

2.2.3 启动类
package com.example;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootAdminServerApplication.class, args);
    }

}

完成上面三步之后,启动服务端,浏览器访问http://localhost:8000可以看到以下界面:

Spring Boot Admin对SpringBoot服务进行监控_第1张图片

2.3 Admin Client 端

2.3.1 项目依赖

<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.0modelVersion>
    <parent>
        <groupId>com.examplegroupId>
        <artifactId>spring-boot-admin-simpleartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-boot-admin-clientartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-admin-clientname>
    <description>spring-boot-admin-clientdescription>
    <packaging>jarpackaging>

    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>

        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-clientartifactId>
            <version>2.1.0version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>
2.3.2 配置文件
server.port=8001

spring.application.name=Admin Client
spring.boot.admin.client.url=http://localhost:8000
management.endpoints.web.exposure.include=*
  • spring.boot.admin.client.url 配置 Admin Server 的地址

  • management.endpoints.web.exposure.include=* 打开客户端 Actuator 的监控。

2.3.3 启动类
package com.example;

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

@SpringBootApplication
public class SpringBootAdminClientApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpringBootAdminClientApplication.class, args);
	}

}

配置完成之后,启动 Client 端,Admin 服务端会自动检查到客户端的变化,并展示其应用

Spring Boot Admin对SpringBoot服务进行监控_第2张图片

页面会展示被监控的服务列表,点击详项目名称会进入此应用的详细监控信息。

Spring Boot Admin对SpringBoot服务进行监控_第3张图片

Spring Boot Admin对SpringBoot服务进行监控_第4张图片

通过上图可以看出,Spring Boot Admin 以图形化的形式展示了应用的各项信息,这些信息大多都来自于 Spring

Boot Actuator 提供的接口。

3、监控微服务

如果我们使用的是单个 Spring Boot 应用,就需要在每一个被监控的应用中配置 Admin Server 的地址信息;如果

应用都注册在 Eureka 中就不需要再对每个应用进行配置,Spring Boot Admin 会自动从注册中心抓取应用的相关

信息。

如果我们使用了 Spring Cloud 的服务发现功能,就不需要在单独添加 Admin Client 客户端,仅仅需要 Spring

Boot Server,其它内容会自动进行配置。

接下来我们以 Eureka 作为服务发现的示例来进行演示,实际上也可以使用 Consul 或者 Zookeeper。

3.1 Eureka注册中心

3.1.1 pom依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/>
    parent>
    <groupId>com.examplegroupId>
    <artifactId>spring-cloud-eurekaartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-cloud-eurekaname>
    <description>spring-cloud-eurekadescription>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Edgware.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
project>
3.1.2 配置文件
server:
  port: 8761
spring:
  application:
    name: eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false
3.1.3 启动类
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpringCloudEurekaApplication.class, args);
	}

}

3.2 生产者1

3.2.1 pom依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-cloud-producerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-cloud-producername>
    <description>spring-cloud-producerdescription>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Edgware.SR1spring-cloud.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>

        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-clientartifactId>
            <version>1.5.6version>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>
3.2.2 配置文件
server:
  port: 9000
spring:
  application:
    name: producer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false
3.2.3 启动类
package com.example;

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

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudProducerApplication {

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

}
3.2.4 控制器
package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(FallbackProvider.class);

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        logger.info("request one  name is "+name);
        return "hello "+name+",this is first messge";
    }
}

3.3 生产者2

3.3.1 pom依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/>
    parent>

    <groupId>com.examplegroupId>
    <artifactId>spring-cloud-producer-2artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-cloud-producer-2name>
    <description>spring-cloud-producer-2description>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Edgware.SR1spring-cloud.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>

        <dependency>
            <groupId>de.codecentricgroupId>
            <artifactId>spring-boot-admin-starter-clientartifactId>
            <version>1.5.6version>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>
3.3.2 配置文件
server:
  port: 9001
spring:
  application:
    name: producer-2
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false
3.3.3 启动类
package com.example;

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

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudProducer2Application {

	public static void main(String[] args) {

		SpringApplication.run(SpringCloudProducer2Application.class, args);
	}

}
3.3.4 控制器
package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(FallbackProvider.class);

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        logger.info("request two name is "+name);
        try{
            Thread.sleep(1000000);
        }catch ( Exception e){
            logger.error(" hello two error",e);
        }
        return "hello "+name+",this is two messge";
    }
}

3.4 AdminServer

3.4.1 pom依赖

<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.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>1.5.9.RELEASEversion>
		<relativePath/>
	parent>

	<groupId>com.examplegroupId>
	<artifactId>spring-boot-admin-serverartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>spring-boot-admin-servername>
	<description>spring-boot-admin-serverdescription>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
		<java.version>1.8java.version>
		<spring-cloud.version>Edgware.SR1spring-cloud.version>
	properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-eurekaartifactId>
		dependency>

		<dependency>
			<groupId>de.codecentricgroupId>
			<artifactId>spring-boot-admin-serverartifactId>
			<version>1.5.6version>
		dependency>

		<dependency>
			<groupId>de.codecentricgroupId>
			<artifactId>spring-boot-admin-server-uiartifactId>
			<version>1.5.6version>
		dependency>
	dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>${spring-cloud.version}version>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>
project>
3.4.2 配置文件
server:
  port: 8000
spring:
  application:
    name: admin-server
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

management.security.enabled: false
3.4.3 启动类
package com.example;

import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminServerApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpringBootAdminServerApplication.class, args);
	}

}

3.5 测试

启动4个应用,访问http://localhost:8761/,结果如下:

Spring Boot Admin对SpringBoot服务进行监控_第5张图片

访问http://localhost:8000/,结果如下:

Spring Boot Admin对SpringBoot服务进行监控_第6张图片

点击一个进入,结果如下:

Spring Boot Admin对SpringBoot服务进行监控_第7张图片

Spring Cloud 提供了示例代码可以参考这里:

https://github.com/codecentric/spring-boot-admin/tree/master/spring-boot-admin-samples/spring-boot-admin-sample-eureka/

重启启动服务端和客户端之后,访问服务端的相关地址就可以看到监控页面了。

你可能感兴趣的:(spring,boot,spring,boot)