iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot Admin怎么用
  • 570
分享到

SpringBoot Admin怎么用

2023-06-25 16:06:01 570人浏览 泡泡鱼
摘要

这篇文章给大家分享的是有关SpringBoot Admin怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。介绍Spring Boot Admin是一个GitHub上的一个开源项目,它在spring Boot

这篇文章给大家分享的是有关SpringBoot Admin怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

介绍

Spring Boot Admin是一个GitHub上的一个开源项目,它在spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot 应用程序的一个简单的界面,提供如下功能:

  • 显示 name/id 和版本号

  • 显示在线状态

  • Logging日志级别管理

  • JMX beans管理

  • Threads会话和线程管理

  • Trace应用请求跟踪

  • 应用运行参数信息,如:

Java 系统属性

Java 环境变量属性

内存信息

Spring 环境属性

Spring Boot Admin 包含服务端和客户端,按照以下配置可让Spring Boot Admin运行起来。

使用

Server端

1、pom文件引入相关的jar包

新建一个admin-server的Spring Boot项目,在pom文件中引入server相关的jar

   <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-server</artifactId>            <version>1.5.3</version>        </dependency>        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-server-ui</artifactId>            <version>1.5.3</version>        </dependency>        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-starter-client</artifactId>            <version>1.5.3</version>        </dependency>

其中spring-boot-admin-starter-client的引入是让server本身能够发现自己(自己也是客户端)。

2、 application.yml配置

在application.yml配置如下,除了server.port=8083的配置是server 对外公布的服务端口外,其他配置是server本身作为客户端的配置,包括指明指向服务端的地址和当前应用的基本信息,使用@@可以读取pom.xml的相关配置。

在下面Client配置的讲解中,可以看到下面类似的配置。

server:  port: 8083spring:  boot:    admin:      url: Http://localhost:8083info:  name: server  description: @project.description@  version: @project.version@

3、配置日志级别

在application.yml的同级目录,添加文件logback.xml,用以配置日志的级别,包含的内容如下:

<?xml version="1.0" encoding="UTF-8"?><configuration>    <include resource="org/springframework/boot/logging/logback/base.xml"/>    <logger name="org.springframework.web" level="DEBUG"/>    <jmxConfigurator/></configuration>

在此处配置成了DEBUG,这样可以通过控制台日志查看server端和client端的交互情况。

4、添加入口方法注解

在入口方法上添加@EnableAdminServer注解。

@Configuration@EnableAutoConfiguration@EnableAdminServerpublic class ServerApplication {    public static void main(String[] args) {        SpringApplication.run(ServerApplication.class, args);    }}

5、启动项目

启动admin-server项目后,可以看到当前注册的客户端,点击明细,还可以查看其他明细信息。

SpringBoot Admin怎么用

Client端

在上述的Server端配置中,server本身也作为一个客户端注册到自己,所以client配置同server端配置起来,比较见到如下。

创建一个admin-client项目,在pom.xml添加相关client依赖包。

1、pom.xml添加client依赖

<dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-starter-client</artifactId>            <version>1.5.3</version>        </dependency>

2、application.yml配置

在application.yml配置注册中心地址等信息:

spring:  boot:    admin:      url: http://localhost:8083info:  name: client  description: @project.description@  version: @project.version@endpoints:  trace:    enabled: true    sensitive: false

3、配置日志文件

在application.yml的同级目录,添加文件logback.xml,用以配置日志的级别,包含的内容如下:

<?xml version="1.0" encoding="UTF-8"?><configuration>    <include resource="org/springframework/boot/logging/logback/base.xml"/>    <logger name="org.springframework.web" level="DEBUG"/>    <jmxConfigurator/></configuration>

配置为DEBUG的级别,可以输出和服务器的通信信息,以便我们在后续心跳检测,了解Spring Boot Admin的实现方式。

4、启动Admin-Client应用

启动客户端项目,在服务端监听了客户端的启动,并在页面给出了消息提示,启动后,服务端的界面显示如下:(两个客户端都为UP状态)

SpringBoot Admin怎么用

以上就可以使用Spring Boot Admin的各种监控服务了,下面谈一谈客户端和服务端怎么样做心跳检测的。

心跳检测/健康检测原理

原理

在Spring Boot Admin中,Server端作为注册中心,它要监控所有的客户端当前的状态。要知道当前客户端是否宕机,刚发布的客户端也能够主动注册到服务端。

服务端和客户端之间通过特定的接口通信(/health接口)通信,来监听客户端的状态。因为客户端和服务端不能保证发布顺序。

有如下的场景需要考虑:

  • 客户端先启动,服务端后启动

  • 服务端先启动,客户端后启动

  • 服务端运行中,客户端下线

  • 客户端运行中,服务端下线

所以为了解决以上问题,需要客户端和服务端都设置一个任务监听器,定时监听对方的心跳,并在服务器及时更新客户端状态。

上文的配置使用了客户端主动注册的方法。

调试准备

为了理解Spring Boot Admin的实现方式,可通过DEBUG 和查看日志的方式理解服务器和客户端的通信(心跳检测)

  • 在pom.xml右键spring-boot-admin-server和spring-boot-admin-starter-client,Maven-> DownLoad Sources and Documentation

  • 在logback.xml中设置日志级别为DEBUG

客户端发起POST请求

客户端相关类

  • ReGIStrationApplicationListener

  • ApplicationRegistrator

在客户端启动的时候调用RegistrationApplicationListener的startRegisterTask,该方法每隔 registerPeriod = 10_000L,(10秒:默认)向服务端POST一次请求,告诉服务器自身当前是有心跳的。

RegistrationApplicationListener

@EventListener    @Order(Ordered.LOWEST_PRECEDENCE)    public void onApplicationReady(ApplicationReadyEvent event) {        if (event.getApplicationContext() instanceof WebApplicationContext && autoRegister) {            startRegisterTask();        }    }    public void startRegisterTask() {        if (scheduledTask != null && !scheduledTask.isDone()) {            return;        }        scheduledTask = taskScheduler.scheduleAtFixedRate(new Runnable() {            @Override            public void run() {                registrator.register();            }        }, registerPeriod);        LOGGER.debug("Scheduled registration task for every {}ms", registerPeriod);    }

ApplicationRegistrator

 public boolean register() {        boolean isRegistrationSuccessful = false;        Application self = createApplication();        for (String adminUrl : admin.getAdminUrl()) {            try {                @SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl,                        new HttpEntity<>(self, HTTP_HEADERS), Map.class);                if (response.getStatusCode().equals(httpstatus.CREATED)) {                    if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {                        LOGGER.info("Application registered itself as {}", response.getBody());                    } else {                        LOGGER.debug("Application refreshed itself as {}", response.getBody());                    }                    isRegistrationSuccessful = true;                    if (admin.isRegisterOnce()) {                        break;                    }                } else {                    if (unsuccessfulAttempts.get() == 0) {                        LOGGER.warn(                                "Application failed to registered itself as {}. Response: {}. Further attempts are logged on DEBUG level",                                self, response.toString());                    } else {                        LOGGER.debug("Application failed to registered itself as {}. Response: {}", self,                                response.toString());                    }                }            } catch (Exception ex) {                if (unsuccessfulAttempts.get() == 0) {                    LOGGER.warn(                            "Failed to register application as {} at spring-boot-admin ({}): {}. Further attempts are logged on DEBUG level",                            self, admin.getAdminUrl(), ex.getMessage());                } else {                    LOGGER.debug("Failed to register application as {} at spring-boot-admin ({}): {}", self,                            admin.getAdminUrl(), ex.getMessage());                }            }        }        if (!isRegistrationSuccessful) {            unsuccessfulAttempts.incrementAndGet();        } else {            unsuccessfulAttempts.set(0);        }        return isRegistrationSuccessful;    }

在主要的register()方法中,向服务端POST了Restful请求,请求的地址为/api/applications

并把自身信息带了过去,服务端接受请求后,通过sha-1算法计算客户单的唯一ID,查询hazelcast缓存数据库,如第一次则写入,否则更新。

服务端接收处理请求相关类

RegistryController

@RequestMapping(method = RequestMethod.POST)    public ResponseEntity<Application> register(@RequestBody Application application) {        Application applicationWithSource = Application.copyOf(application).withSource("http-api")                .build();        LOGGER.debug("Register application {}", applicationWithSource.toString());        Application registeredApp = registry.register(applicationWithSource);        return ResponseEntity.status(HttpStatus.CREATED).body(registeredApp);    }

ApplicationRegistry

public Application register(Application application) {        Assert.notNull(application, "Application must not be null");        Assert.hasText(application.getName(), "Name must not be null");        Assert.hasText(application.getHealthUrl(), "Health-URL must not be null");        Assert.isTrue(checkUrl(application.getHealthUrl()), "Health-URL is not valid");        Assert.isTrue(                StringUtils.isEmpty(application.getManagementUrl())                        || checkUrl(application.getManagementUrl()), "URL is not valid");        Assert.isTrue(                StringUtils.isEmpty(application.getServiceUrl())                        || checkUrl(application.getServiceUrl()), "URL is not valid");        String applicationId = generator.generateId(application);        Assert.notNull(applicationId, "ID must not be null");        Application.Builder builder = Application.copyOf(application).withId(applicationId);        Application existing = getApplication(applicationId);        if (existing != null) {            // Copy Status and Info from existing registration.            builder.withStatusInfo(existing.getStatusInfo()).withInfo(existing.getInfo());        }        Application registering = builder.build();        Application replaced = store.save(registering);        if (replaced == null) {            LOGGER.info("New Application {} registered ", registering);            publisher.publishEvent(new ClientApplicationRegisteredEvent(registering));        } else {            if (registering.getId().equals(replaced.getId())) {                LOGGER.debug("Application {} refreshed", registering);            } else {                LOGGER.warn("Application {} replaced by Application {}", registering, replaced);            }        }        return registering;    }

HazelcastApplicationStore (缓存数据库)

在上述更新状态使用了publisher.publishEvent事件订阅的方式,接受者接收到该事件,做应用的业务处理,在这块使用这种方式个人理解是为了代码的复用性,因为服务端定时轮询客户端也要更新客户端在服务器的状态。

pulishEvent设计到的类有:

  • StatusUpdateApplicationListener->onClientApplicationRegistered

  • StatusUpdater–>updateStatus

这里不详细展开,下文还会提到,通过日志,可以查看到客户端定时发送的POST请求:

SpringBoot Admin怎么用

服务端定时轮询

在服务器宕机的时候,服务器接收不到请求,此时服务器不知道客户端是什么状态,(当然可以说服务器在一定的时间里没有收到客户端的信息,就认为客户端挂了,这也是一种处理方式),在Spring Boot Admin中,服务端通过定时轮询客户端的/health接口来对客户端进行心态检测。

这里设计到主要的类为:

StatusUpdateApplicationListene

@EventListener    public void onApplicationReady(ApplicationReadyEvent event) {        if (event.getApplicationContext() instanceof WebApplicationContext) {            startStatusUpdate();        }    }    public void startStatusUpdate() {        if (scheduledTask != null && !scheduledTask.isDone()) {            return;        }        scheduledTask = taskScheduler.scheduleAtFixedRate(new Runnable() {            @Override            public void run() {                statusUpdater.updateStatusForAllApplications();            }        }, updatePeriod);        LOGGER.debug("Scheduled status-updater task for every {}ms", updatePeriod);    }

StatusUpdater

public void updateStatusForAllApplications() {        long now = System.currentTimeMillis();        for (Application application : store.findAll()) {            if (now - statusLifetime > application.getStatusInfo().getTimestamp()) {                updateStatus(application);            }        }    }public void updateStatus(Application application) {        StatusInfo oldStatus = application.getStatusInfo();        StatusInfo newStatus = queryStatus(application);        boolean statusChanged = !newStatus.equals(oldStatus);        Application.Builder builder = Application.copyOf(application).withStatusInfo(newStatus);        if (statusChanged && !newStatus.isOffline() && !newStatus.isUnknown()) {            builder.withInfo(queryInfo(application));        }        Application newState = builder.build();        store.save(newState);        if (statusChanged) {            publisher.publishEvent(                    new ClientApplicationStatusChangedEvent(newState, oldStatus, newStatus));        }    }

感谢各位的阅读!关于“SpringBoot Admin怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: SpringBoot Admin怎么用

本文链接: https://www.lsjlt.com/news/305971.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot Admin怎么用
    这篇文章给大家分享的是有关SpringBoot Admin怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。介绍Spring Boot Admin是一个Github上的一个开源项目,它在Spring Boot ...
    99+
    2023-06-25
  • SpringBoot Admin怎么样
    这篇文章主要介绍SpringBoot Admin怎么样,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!公司有个SpringBoot项目需要加个监控,网上找了下发现大家都在推荐SpringBootAdmin。S...
    99+
    2023-06-26
  • 如何创建SpringBoot Admin应用
    本篇内容介绍了“如何创建SpringBoot Admin应用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、SrpingBoot Admi...
    99+
    2023-06-05
  • Spring Boot Admin怎么用
    这篇文章给大家分享的是有关Spring Boot Admin怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1、介绍官网地址Spring Boot Admin 是开源社区孵化的项目,用于对 Spring Bo...
    99+
    2023-06-25
  • ceph admin socket怎么用
    这篇文章给大家分享的是有关ceph admin socket怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。ceph admin socket利用ceph admin socket可以获得ceph的在线参数,...
    99+
    2023-06-27
  • SpringBoot整合SpringBoot-Admin实现监控应用功能
    目录搭建Admin Server引入依赖Admin Server启动类application.yml配置测试搭建Admin Client引入依赖application.yml配置存在...
    99+
    2023-05-20
    SpringBoot整合SpringBoot-Admin SpringBoot Admin监控应用
  • springboot admin监控的作用和使用方法是什么
    这篇“springboot admin监控的作用和使用方法是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springb...
    99+
    2023-06-08
  • SpringBoot Admin使用及心跳检测原理分析
    目录介绍使用Server端Client端心跳检测/健康检测原理原理调试准备客户端发起POST请求服务端定时轮询介绍 Spring Boot Admin是一个Github上的一个开源项...
    99+
    2024-04-02
  • laravel-admin与vue结合怎么用
    这篇文章主要介绍laravel-admin与vue结合怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!由于 Laravel-admin 采用的是 pjax 的方式刷新页面,意味着...
    99+
    2024-04-02
  • Admin 怎么在Spring Boot中使用
    本篇文章为大家展示了Admin 怎么在Spring Boot中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、前言Spring Boot Admin 用于监控基于 Spring Boot 的应...
    99+
    2023-05-31
    springboot mi admin
  • GS Admin限流功能怎么使用
    今天小编给大家分享一下GS Admin限流功能怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。仓库giee: ...
    99+
    2023-07-04
  • angular admin怎么搭建
    要搭建Angular Admin,你可以按照以下步骤进行操作:1. 确保你的计算机已经安装了Node.js和npm。你可以在命令行中...
    99+
    2023-10-11
    angular
  • SpringBoot Admin健康检查功能的实现
    目录admin实现admin功能创建客户端主动上报的服务端实现效果异常通知邮件通知其他通知代码地址admin 监控检查,检查的是什么了。检查的是应用实例状态,说白了就是被查服务提供信...
    99+
    2024-04-02
  • django怎么自定义admin
    要自定义Django的admin界面,可以按照以下步骤进行:1. 创建一个继承自`django.contrib.admin.Mode...
    99+
    2023-10-08
    django
  • 五分钟解锁springboot admin监控新技巧
    最近这一个月由于项目进度紧张,将近一个月没有动静。分享一下最近体会的springboot监控的一些心得体会,供一些规模不是很大的团队做一些监控。 适用场景: 1、项目规模不大 2、用...
    99+
    2024-04-02
  • SpringBoot Admin集成诊断利器Arthas示例实现
    目录前言SpringBoot AdminSBA 集成客户端配置在配置中心加入配置实现开关效果结束前言 Arthas 是 Alibaba开源的Java诊断工具,具有实时查看系统的运行状...
    99+
    2024-04-02
  • Grafana怎么重置admin密码
    这篇文章将为大家详细讲解有关Grafana怎么重置admin密码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Grafana重置admin密码方法:1、查看Grafana配置文件,确定【grafana.d...
    99+
    2023-06-14
  • 怎么使用Laravel admin后台管理插件
    本篇内容主要讲解“怎么使用Laravel admin后台管理插件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Laravel admin后台管理插件”吧...
    99+
    2024-04-02
  • SpringBoot详解整合Spring Boot Admin实现监控功能
    目录监控监控的意义可视化监控平台监控原理自定义监控指标监控 ​ 在说监控之前,需要回顾一下软件业的发展史。最早的软件完成一些非常简单的功能,代码不多,错误也少。随着软件功能的逐步完善...
    99+
    2024-04-02
  • SpringBoot Admin 如何实现Actuator端点可视化监控
    目录SpringBoot Admin 实现Actuator端点可视化监控简介Spring Boot Admin ServerSpring Boot Admin Client启动客户端...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作