• Spring Boot -- Spring Boot之监控中心


    一、监控管理

    首先我们来了解一下监控中心是什么?

    针对微服务的服务状态包括http请求资源、服务器内存变化(堆、内存、线程、日志管理等)、检测服务配置连接地址是否可用(模拟访问,懒加载情况下)、统计现在有多少个bean(是spring容器中的bean)、统计Spring MVC的@ResultMapping(统计http接口)。

    Spring Boot提供了两种监控中心:

    • Actuator监控应用:没有界面,返回json格式;

    • AdminUI:底层使用的就是Actuator监控应用,实现可视化的界面;

    1、Actuator监控应用

    Actuator是Spring Boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用http的各种请求来监管、审计、收集应用的运行情况,特别对于微服务管理十分有意义。

    2、新建Actuator项目

    新建一个项目springboot-actuator,添加依赖:

     <!--  spring-boot-starter-parent  整合第三方常用框架依赖信息(各种引来信息)-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <!--  spring-boot-starter-web 是Spring Boot整合Spring MVC Web  -->
            <!--  相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--  actuator监控中心  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
        </dependencies>

    新建包com.goldwind.controller,在包下创建IndexController.java:

    package com.goldwind.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @Author: zy
     * @Description: 控制器
     * @Date: 2020-2-7
     */
    @RestController
    public class IndexController {
    
        @RequestMapping("/index")
        public String index(){
            return "index";
        }
    }

    在src/java/resources下新建application.yml文件:

    #通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的;
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    运行程序,访问http://localhost:8080/actuator/mappings可以查看当前支持哪些API请求:

    根据端点的作用,可以将spring-boot-starter-actuator模块中原生端点分为三大类:

    • 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息;
    • 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如内存信息、线程池信息、HTTP请求统计等;
    • 操作控制类:提供了对应用的关闭等操作类功能;

    我们介绍几个常见的原生端点,通过actuator/+端点名就可以获取相应的信息:

    路径

    作用

    /actuator/beans

    显示应用程序中所有Spring bean的完整列表。

    /actuator/configprops

    显示所有配置信息。

    /actuator/env

    陈列所有的环境变量。

    /actuator/mappings

    显示所有@RequestMapping的url整理列表。

    /actuator/health

    显示应用程序运行状况信息 up表示成功 down失败

    /actuator/info

    查看自定义应用信息

    更多相关信息可以参考博客:SpringBoot actuator 应用监控

    3、新建admin-ui-server项目

    新建一个项目springboot-admin-ui-server,添加依赖:

    <?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.goldwind.com</groupId>
        <artifactId>springboot-admin-ui-server</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <!--  spring-boot-starter-parent  整合第三方常用框架依赖信息(各种引来信息)-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <!--  spring-boot-starter-web 是Spring Boot整合Spring MVC Web  -->
            <!--  相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--  支持admin-ui的关键配置  -->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>2.2.2</version>
            </dependency>
    
        </dependencies>
    
    </project>

    新建application.yml文件,配置端口号和服务名称:

    server:
      port: 8090
    
    
    spring:
      application:
        name: admin-ui-server

    新增启动类,在包com.goldwind下新建App.java文件,必须添加@EnableAdminServer注解:

    package com.goldwind;
    
    import de.codecentric.boot.admin.server.config.EnableAdminServer;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @Author: zy
     * @Description: 启动类
     * @Date: 2020-2-8
     */
    @SpringBootApplication
    @EnableAdminServer
    public class App {
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }

    启动springboot-admin-ui-server,浏览器中打开监控中心的UI管理页面:http://localhost:8090/applications

    4、新建admin-ui-client项目

    新建一个项目springboot-admin-ui-client,添加依赖:

    <?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.goldwind.com</groupId>
        <artifactId>springboot-admin-ui-client</artifactId>
        <version>1.0-SNAPSHOT</version>
    
    
        <!--  spring-boot-starter-parent  整合第三方常用框架依赖信息(各种引来信息)-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
        </parent>
    
    
        <dependencies>
            <!--  spring-boot-starter-web 是Spring Boot整合Spring MVC Web  -->
            <!--  相当于把第三方常用Maven依赖信息,在parent项目中封装好了,使用Spring Boot提供依赖信息关联整合的Jar包 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--  支持admin-ui的关键配置  -->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>2.2.2</version>
            </dependency>
    
        </dependencies>
    
    </project>

    新建application.yml文件,配置服务名称,端口号默认是8080,并注册当前服务到admin-ui-server服务中:

    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    spring:
      application:
        name: admin-ui-client
      #注册当前服务到admin-ui-server服务中
      boot:
        admin:
          client:
            url: http://localhost:8090

    新增启动类,在包com.goldwind下新建App.java文件:

    package com.goldwind;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @Author: zy
     * @Description: 启动类
     * @Date: 2020-2-8
     */
    @SpringBootApplication
    public class App {
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }

    启动服务,再次打开UI管理页面:

    客户端服务已经注册到监控中心,可以点击项目查看监控信息:

    接下来就可以分析客户端服务的运行时状态了。

    参考文章:

    [1] SpringBoot中的监控中心

  • 相关阅读:
    所写既所思
    OO总结
    OO第三单元作业总结
    OO第二单元作业总结
    OO第一单元作业总结
    oo第四单元与课程总结
    oo第三单元总结——jml
    oo第二单元博客总结
    面向对象设计与构造-第一单元作业总结
    个人总结-不说再见
  • 原文地址:https://www.cnblogs.com/zyly/p/12273086.html
Copyright © 2020-2023  润新知