SpringBootAdmin超详细教程以及端点指标控制

╰半夏微凉° 2024-04-03 13:26 193阅读 0赞

文章目录

    • 1.服务端开发
    • 2.客户端开发
        • 配置多个客户端
      • 3.端点指标控制
        • actuator
        • 1.info端点描述了当前应用的基本信息,可以通过两种形式快速配置info端点的信息
        • 2.health端点指标控制
        • 3.metrics端点指标控制
        • 4.自定义端点
        • 小结:
      • 总结:

Spring Boot Admin,这是一个开源社区项目,用于管理和监控SpringBoot应用程序

1.服务端开发

创建一个sprinboot工程

image-20221017093721570

步骤①:导入springboot admin对应的starter,版本与当前使用的springboot版本保持一致,并将其配置成web工程

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-server</artifactId>
  4. <version>2.7.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>

步骤②在引导类上添加注解@EnableAdminServer,声明当前应用启动后作为SpringBootAdmin的服务器使用

  1. @SpringBootApplication
  2. @EnableAdminServer //要加上这个注解
  3. public class SpringbootServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootServerApplication.class, args);
  6. }
  7. }

注解一定要加上

image-20221017100326134

步骤③:启动项目,在页面输入访问地址访问服务器

  1. http://localhost:8080/applications

image-20221017101200560

2.客户端开发

创建一个spingboot工程,勾选下图坐标

image-20221017093826802

步骤①:导入springboot admin对应的starter,版本与当前使用的springboot版本保持一致,并将其配置成web工程

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. <version>2.7.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>

步骤②:设置当前客户端将信息上传到哪个服务器上,通过yml文件配置

  1. server:
  2. port: 80 #修改端口号,以免与服务端冲突
  3. spring:
  4. boot:
  5. admin:
  6. client:
  7. url: http://localhost:8080

上述配置的意思就是该客户端的信息传到localhost:8080的服务器上

步骤③:客户端启动,启动后再次访问服务端程序,界面如下

image-20221017102144732当前监控了1个程序,点击进去查看详细信息。

image-20221017102420733

由于当前没有设置开放哪些信息给监控服务器,所以目前看不到什么有效的信息。下面需要做两组配置。

  • 开放指定信息给服务器看
  • 允许服务器以HTTP请求的方式获取对应的信息

配置如下:

  1. server:
  2. port: 80
  3. spring:
  4. boot:
  5. admin:
  6. client:
  7. url: http://localhost:8080
  8. management:
  9. endpoint:
  10. health:
  11. show-details: always #开放所有的健康信息明细
  12. endpoints:
  13. web:
  14. exposure:
  15. include: "*" #开启查阅的内容项,使用*表示查阅全部。记得带引号,默认带health的内容项

image-20221017104712397

配置多个客户端

可以通过配置客户端的方式在其他的springboot程序中添加客户端坐标,在yml中添加配置,这样当前服务器就可以监控多个客户端程序了。每个客户端展示不同的监控信息。

  • 添加yml配置

    spring:
    boot:

    1. admin:
    2. client:
    3. url: http://localhost:8080

    management:
    endpoint:

    1. health:
    2. show-details: always #开放所有的健康信息明细

    endpoints:

    1. web:
    2. exposure:
    3. include: "*" #开启查阅的内容项,使用*表示查阅全部。记得带引号,默认带health的内容项
  • 添加客户端坐标,要与当前springboot工程版本号一致


    de.codecentric
    spring-boot-admin-starter-client
    2.7.4
    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. </dependency>

进入监控面板,如果你加载的应用具有功能,在监控面板中可以看到3组信息展示的与之前加载的空工程不一样。

  • 类加载面板中可以查阅到开发者自定义的类,如下图
  • 映射中可以查阅到当前应用配置的所有请求
  • 性能指标中可以查阅当前应用独有的请求路径统计数据

image-20221017114126158

  • 端点功能开启与关闭

启动个别端点用endpoint,health默认必须开启

  1. management:
  2. endpoint:
  3. health: # 端点名称
  4. show-details: always
  5. info: # 端点名称
  6. enabled: true # 是否开放,对单一端点的开启与关闭

为了方便开发者快速配置端点,springboot admin设置了13个较为常用的端点作为默认开放的端点,如果需要控制默认开放的端点的开放状态,可以通过配置设置,如下:

启动所有端点用endpoints

  1. management:
  2. endpoints:
  3. enabled-by-default: true # 是否开启默认端点,默认值true
  • 暴露端点功能

JMX默认都可以访问,而Web默认只有两个,health和info

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: "*"
  • 整体上来说,对于端点的配置有两组信息,一组是endpoints开头的,对所有端点进行配置;一组是endpoint开头的,对具体端点进行配置;web需要设置暴露端点,才可以在Web页面访问到

    management:
    endpoint: # 具体端点的配置

    1. health:
    2. show-details: always
    3. info:
    4. enabled: true

    endpoints: # 全部端点的配置

    1. web: #web可以访问端点开启
    2. exposure:
    3. include: "*"
    4. enabled-by-default: true

总结:

  1. 开发监控服务端需要导入坐标,然后在引导类上添加注解@EnableAdminServer,并将其配置成web程序即可
  2. 开发被监控的客户端需要导入坐标,然后配置服务端服务器地址,并做开放指标的设定即可
  3. 在监控平台中可以查阅到各种各样被监控的指标,前提是客户端开放了被监控的指标

3.端点指标控制

actuator

1.Actuator提供了SpringBoot生产就绪功能,通过端点的配置与访问,获取端点信息

2.端点描述了一组监控信息,SpringBoot提供了多个内置端点,也可以根据需要自定义端点信息

3.访问当前应用所有端点信息:/actuator

4.访问端点详细信息:/actuator/ 端点名称

1.info端点描述了当前应用的基本信息,可以通过两种形式快速配置info端点的信息
  • 配置形式

    在yml文件中通过设置info节点的信息就可以快速配置端点信息,版本高的需要加一下配置在yml中

    1. management:
    2. endpoint:
    3. health:
    4. show-details: always
    5. info:
    6. enabled: true
    7. endpoints:
    8. web:
    9. exposure:
    10. include: "*"
    11. enabled-by-default: true
    12. info:
    13. env:
    14. enabled: true #高版本的需要自己加一下,management.info.env.enabled设置为true
    15. info:
    16. author: QGC
    17. school: ccut
  • 编程形式

    @Component
    public class InfoConfig implements InfoContributor {

    1. //实现这个接口
    2. @Override
    3. public void contribute(Info.Builder builder) {
    4. //添加单个信息
    5. builder.withDetail("runTime", new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()));
    6. Map infoMap = new HashMap();
    7. infoMap.put("buildTime", "2022:10:17");
    8. builder.withDetails(infoMap); //添加一组信息
    9. }

    }

  • 页面效果

image-20221017123904762

2.health端点指标控制

health端点描述当前应用的运行健康指标,即应用的运行是否成功。通过编程的形式可以扩展指标信息。

  1. @Component
  2. public class HealthConfig extends AbstractHealthIndicator {
  3. @Override
  4. protected void doHealthCheck(Health.Builder builder) throws Exception {
  5. boolean condition = true;
  6. if(condition) {
  7. builder.status(Status.UP); //设置运行状态为启动状态
  8. builder.withDetail("runTime", System.currentTimeMillis());
  9. Map infoMap = new HashMap();
  10. infoMap.put("buildTime", "2022");
  11. builder.withDetails(infoMap);
  12. }else{
  13. builder.status(Status.OUT_OF_SERVICE); //设置运行状态为不在服务状态
  14. builder.withDetail("上线了吗","你做梦");
  15. }
  16. }
  17. }
  • 条件成功时

image-20221017124940251

  • 条件失败时

当任意一个组件状态不为UP时,整体应用对外服务状态为非UP状态。

image-20221017125115651

3.metrics端点指标控制

metrics端点描述了性能指标,除了系统自带的监控性能指标,还可以自定义性能指标。

  1. @Service
  2. public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
  3. @Autowired
  4. private BookDao bookDao;
  5. private Counter counter;
  6. public BookServiceImpl(MeterRegistry meterRegistry){
  7. counter = meterRegistry.counter("QGC用户付费操作次数:");
  8. }
  9. public Boolean insert(Book book) {
  10. return bookDao.insert(book) > 0;
  11. }
  12. public Boolean modify(Book book) {
  13. return bookDao.updateById(book) > 0;
  14. }
  15. public Boolean delete(Integer id) {
  16. //每次执行删除业务等同于执行了付费业务
  17. counter.increment();
  18. return bookDao.deleteById(id) > 0;
  19. }

在性能指标中就出现了自定义的性能指标监控项

image-20221017130513465

4.自定义端点

可以根据业务需要自定义端点,方便业务监控

  1. @Component
  2. @Endpoint(id="pay",enableByDefault = true)
  3. public class PayEndpoint {
  4. @ReadOperation
  5. public Object getPay(){
  6. Map payMap = new HashMap();
  7. payMap.put("level 1","226");
  8. payMap.put("level 2","912");
  9. payMap.put("level 3","0820");
  10. return payMap;
  11. }
  12. }

于此端点数据spirng boot admin无法预知该如何展示,所以通过界面无法看到此数据,通过HTTP请求路径可以获取到当前端点的信息,但是需要先开启当前端点对外功能,或者设置当前端点为默认开发的端点。

这里用postman来模拟请求

image-20221017132227811

小结:

1.端点的指标可以自定义,但是每种不同的指标根据其功能不同,自定义方式不同
2.info端点通过配置和编程的方式都可以添加端点指标
3.health端点通过编程的方式添加端点指标,需要注意要为对应指标添加启动状态的逻辑设定
4.metrics指标通过在业务中添加监控操作设置指标
5.可以自定义端点添加更多的指标

总结:

1.监控的意义

2.可视化监控平台————Spring Boot Admin

3.监控原理————Actuator

4.自定义监控指标

  • 系统端点添加监控指标
  • 自定义端点

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读

    相关 git详细教程

    GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流程 : 第一次提交 :   方案一 : 本地创建项目根目录, 然后与远程