栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 监控及集成Spring Boot Admin使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 监控及集成Spring Boot Admin使用

SpringBoot 监控 SpringBoot 监控概述

SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、配置属性 、日志信息等。

SpringBoot 监控使用

使用步骤:
1、导入依赖坐标


    org.springframework.boot
    spring-boot-starter-actuator

2、启动项目访问http://localhost:8080/acruator
启动项目控制台会输出:

......   : Exposing 13 endpoint(s) beneath base path '/actuator'

3、在application.properties中加入如下信息

info.name = QingBo

# 开启健康检查完整信息
management.endpoint.health.show-details=always

# 将所有的监控endpoint暴露出来
management.endpoints.web.exposure.include=*

4、访问http://localhost:8080/actuator后输出信息如下

{
  "_links":{
    "self":{
      "href":"http://localhost:8080/actuator",
      "templated":false
    },
    "beans":{
      "href":"http://localhost:8080/actuator/beans",
      "templated":false
    },
    "caches-cache":{
      "href":"http://localhost:8080/actuator/caches/{cache}",
      "templated":true
    },
    "caches":{
      "href":"http://localhost:8080/actuator/caches",
      "templated":false
    },
    "health":{
      "href":"http://localhost:8080/actuator/health",
      "templated":false
    },
    "health-path":{
      "href":"http://localhost:8080/actuator/health/{*path}",
      "templated":true
    },
    "info":{
      "href":"http://localhost:8080/actuator/info",
      "templated":false
    },
    "conditions":{
      "href":"http://localhost:8080/actuator/conditions",
      "templated":false
    },
    "configprops":{
      "href":"http://localhost:8080/actuator/configprops",
      "templated":false
    },
    "configprops-prefix":{
      "href":"http://localhost:8080/actuator/configprops/{prefix}",
      "templated":true
    },
    "env":{
      "href":"http://localhost:8080/actuator/env",
      "templated":false
    },
    "env-toMatch":{
      "href":"http://localhost:8080/actuator/env/{toMatch}",
      "templated":true
    },
    "loggers":{
      "href":"http://localhost:8080/actuator/loggers",
      "templated":false
    },
    "loggers-name":{
      "href":"http://localhost:8080/actuator/loggers/{name}",
      "templated":true
    },
    "heapdump":{
      "href":"http://localhost:8080/actuator/heapdump",
      "templated":false
    },
    "threaddump":{
      "href":"http://localhost:8080/actuator/threaddump",
      "templated":false
    },
    "metrics-requiredMetricName":{
      "href":"http://localhost:8080/actuator/metrics/{requiredMetricName}",
      "templated":true
    },
    "metrics":{
      "href":"http://localhost:8080/actuator/metrics",
      "templated":false
    },
    "scheduledtasks":{
      "href":"http://localhost:8080/actuator/scheduledtasks",
      "templated":false
    },
    "mappings":{
      "href":"http://localhost:8080/actuator/mappings",
      "templated":false
    }
  }
}
路径描述
/beans描述应用程序上下文里全部的Bean,以及它们的关系
/env获取全部环境属性
/env/{name}根据名称获取特定的环境属性值
/health报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
/info获取应用程序的定制信息,这些信息由info打头的属性提供
/mappings描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
/metrics报告各种应用程序度量信息,比如内存用量和HTTP请求计数
/metrics/{name}报告指定名称的应用程序度量值
/trace提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)
SpringBoot 监控 - Spring Boot Admin
  • Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。

  • Spring Boot Admin 有两个角色,客户端(Client)和服务端(Server)。

  • 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册

  • Spring Boot Admin Server 的UI界面将Spring Boot Admin Client的Actuator Endpoint上的一些监控信息。

使用步骤:

admin-server:

1、创建 admin-server 模块

利用IDEA快速构建或创建SpringBoot的Maven项目来引入依赖包:下面是IDEA快速构建的方式


2、导入依赖坐标 admin-starter-server



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.7
         
    
    com.qingbo
    qingbo-springboot-actuator-admin-server
    0.0.1-SNAPSHOT
    qingbo-springboot-actuator-admin-server
    qingbo-springboot-actuator-admin-server
    
        1.8
        2.4.3
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            de.codecentric
            spring-boot-admin-starter-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
        
    
    


3、在引导类上启用监控功能@EnableAdminServer

package com.qingbo;

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

@EnableAdminServer
@SpringBootApplication
public class SpringbootActuatorAdminServerApplication {

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

}

4、在application.properties中配置如下信息修改一下端口号:

#指定端口号
server.port=9000
admin-client:

1、创建 admin-client 模块

利用IDEA快速构建或创建SpringBoot的Maven项目来引入依赖包:下面是IDEA快速构建的方式

2、导入依赖坐标 admin-starter-client



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.7
         
    
    com.qingbo
    qingbo-springboot-actuator-admin-client
    0.0.1-SNAPSHOT
    qingbo-springboot-actuator-admin-client
    qingbo-springboot-actuator-admin-client
    
        1.8
        2.4.3
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            de.codecentric
            spring-boot-admin-starter-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
        
    



3、配置相关信息:server地址等

在application.properties中配置如下信息

# 开启健康检查完整信息
management.endpoint.health.show-details=always

# 将所有的监控endpoint暴露出来
management.endpoints.web.exposure.include=*

4、启动server和client服务,访问server
浏览器输入如下地址:

http://localhost:9000

浏览器展现结果为:

在Client端加入一个Controller然后访问

package com.qingbo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/findAll")
    public String findAll(){
        return "SUCCESS";
    }

}

启动项目浏览器访问地址:http://localhost:8080/user/findAll

浏览器打开http://localhost:9000/在性能下可看到访问的次数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号