springBootAdmin监控

简介

用于对 Spring Boot 应用的管理和监控。可以用来监控服务是否健康、是否在线、以及一些jvm数据等等

Spring Boot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用 http 通讯方式实现数据交互;单体项目中需要整合 spring-boot-admin-client 才能让应用被监控

在 SpringCloud 项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服务应用整合 spring-boot-admin-client 就可以实现应用的管理和监控

单体项目

服务端

引入spring-boot-admin服务端依赖

	  <!--用于检查系统的监控情况-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
  <!--Spring Boot Admin Server监控服务端-->
 <dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-server</artifactId>
     <version>2.3.1</version>
 </dependency>
   <!--增加安全防护,防止别人随便进-->
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
  </dependency>

启动类上开启admin@EnableAdminServer

@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
    public static void main(String[] args) {
        SpringApplication.run(MonitorApplication.class, args);
    }
}

security安全防护配置

package org.demo.monitor.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * @author scott
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 登录成功处理类
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //静态文件允许访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                //登录页面允许访问
                .antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
                //其他所有请求需要登录
                .anyRequest().authenticated()
                .and()
                //登录页面配置,用于替换security默认页面
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                //登出页面配置,用于替换security默认页面
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        "/instances",
                        "/actuator/**"
                );

    }

}

yml配置

server:
  port: 9111
spring:
  boot:
    admin:
      ui:
        title: 服务监控中心
      client:
        instance:
          metadata:
            tags:
              environment: local
      #要获取的client的端点信息
      probed-endpoints: health,env,metrics,httptrace:trace,threaddump:dump,jolokia,info,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents
      monitor: # 监控发送请求的超时时间
        default-timeout: 20000
  security: # 设置账号密码
    user:
      name: admin
      password: admin
# 服务端点详细监控信息
management:   
  trace:
    http:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

访问 192.168.11.50:9111 项目启动如下

在这里插入图片描述

自定义服务状态变化后,提醒功能

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Component
public class WarnNotifier extends AbstractStatusChangeNotifier {
	public WarnNotifier(InstanceRepository repository) {
		super(repository);
	}

	@Override
	protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
		// 服务名
		String serviceName = instance.getRegistration().getName();
		// 服务url
		String serviceUrl = instance.getRegistration().getServiceUrl();
		// 服务状态
		String status = instance.getStatusInfo().getStatus();
		// 详情
		Map<String, Object> details = instance.getStatusInfo().getDetails();
		// 当前服务掉线时间
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(date);
		// 拼接短信内容
		StringBuilder str = new StringBuilder();
		str.append("服务名:【" + serviceName + "】 \r\n");
		str.append("服务状态:【"+ status +"】 \r\n");
		str.append("地址:【" + serviceUrl + "】\r\n");
		str.append("时间:" + format +"\r\n");

		return Mono.fromRunnable(()->{
			// 这里写你服务发生改变时,要提醒的方法
			// 如服务掉线了,就发送短信告知
		});
	}
}

客户端pom.xml

 <dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-client</artifactId>
     <version>2.3.1</version>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

yml配置

server:
  port: 9222

spring:
  application:
    name: client
  boot:
    admin:
      client: # spring-boot-admin 客户端配置
        url: http://localhost:9111 #服务端连接地址
        username: admin # 服务端账号
        password: admin # 服务端密码
        instance:
          prefer-ip: true # 使用ip注册

# 服务端点详细监控信息
management:
#	health:  # 检测服务状态是通过http://localhost:9111/actuator/health接口,可去掉不用检测项
#		mail: # 健康检测时,不要检测邮件
#			enabled: false 
  trace:
    http:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
    logfile: # 日志(想在线看日志才配)
      external-file: ./logs/client-info.log # 日志所在路径

微服务项目

添加依赖

<?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">
    <parent>
        <artifactId>jeecg-visual</artifactId>
        <groupId>org.jeecgframework.boot</groupId>
        <version>3.6.3</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>jeecg-cloud-monitor</artifactId>

    <dependencies>
        <!--Spring Boot Admin Server监控服务端-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--安全模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--undertow容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

yml配置

server:
  port: 9111
spring:
  boot:
    admin:
      ui:
        title: 服务监控中心
      client:
        instance:
          metadata:
            tags:
              environment: local
  security:
    user:
      name: "admin"
      password: "admin"
  application:
    name: jeecg-monitor
  cloud:
    nacos:
      discovery:
        server-addr: @config.server-addr@
        password: nacos
        username: nacos
        metadata:
          #如果项目有设置server.servlet.context-path:=/monitor,需要开启以下两个注释
#          management:
#            context-path: ${server.servlet.context-path}/actuator
          user.name: ${spring.security.user.name}
          user.password: ${spring.security.user.password}

# 服务端点详细监控信息
management:
  trace:
    http:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

客户端

客户端不用引spring-boot-admin-starter-clien依赖,springbootadmin会去服务列表里找

如果客户端服务有配置context-path路径,则需添加yml配置

spring:
  cloud:
    nacos:
      discovery:
        metadata:  # minitor监控的context-path配置
          management:
            context-path: ${server.servlet.context-path}/actuator

注意:如果安装的springbootadmin服务的和客户端不在一个局域网,需要设置客户端注册到nacos的IP地址,springbootadmin才能通过实际ip地址找到客户端

spring:
  cloud:
    nacos:
      config:
        server-addr: @config.server-addr@
        group: @config.group@
        namespace: @config.namespace@
        username: @config.username@
        password: @config.password@
      discovery:
        metadata:  # minitor监控的context-path配置
          management:
            context-path: ${server.servlet.context-path}/actuator
        server-addr: ${spring.cloud.nacos.config.server-addr}
        group: @config.group@
        namespace: @config.namespace@
        username: @config.username@
        password: @config.password@
        ip: 192.168.11.50

如果需要查看日记,就需要每一个客户端都需要设置读取各自日记的文件名字

# 服务端点详细监控信息
management:
  endpoint:
    logfile: # 日志(想在线看日志才配)
      external-file: ./logs/client-info.log # 日志所在路径

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/589314.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【设计模式】创建者模式之 工厂方法 抽象工厂

工厂方法模式(Factory Method) 一个特定功能&#xff0c;往往有多种实现方式&#xff0c;但是很难有某一个实现可以适用于所有情况&#xff0c;因此往往需要根据特定的场景选择不同的实现。试想&#xff1a;把选择具体实现的代码放在业务中会发生什么&#xff1f;每当我们需要…

【C++】vector类的增删改查模拟实现(图例超详细解析!!!)

目录 一、前言 二、源码引入 三、vector的模拟实现 ✨实现框架 ✨前情提要 ✨Member functions —— 成员函数 ⚡构造函数 ⭐无参构造 ⭐迭代器区间构造 ⭐n个值构造 ⚡拷贝构造 ⚡运算符赋值重载 ⚡析构函数 ✨Element access —— 元素访问 ⚡operator[ ] …

一文全面了解 wxWidgets 布局器(Sizers)

目录 Sizers背后的理念 共同特征 最小大小 边框 对齐方式 伸缩因子 使用 Sizer 隐藏控件 wxBoxSizer wxStaticBoxSizer wxGridSizer wxFlexGridSizer 布局器&#xff08;Sizers&#xff09;&#xff0c;由wxWidgets类层次结构中的wxSizer类及其派生类表示&#xff0…

电商核心技术揭秘四十五:营销与广告策略(下)

相关系列文章 电商技术揭秘相关系列文章合集&#xff08;1&#xff09; 电商技术揭秘相关系列文章合集&#xff08;2&#xff09; 电商技术揭秘相关系列文章合集&#xff08;3&#xff09; 电商技术揭秘四十一&#xff1a;电商平台的营销系统浅析 电商技术揭秘四十二&#…

苹果可能将OpenAI技术集成至iOS/iPadOS 18

&#x1f989; AI新闻 &#x1f680; 苹果可能将OpenAI技术集成至iOS/iPadOS 18 摘要&#xff1a;苹果正在与OpenAI就将GPT技术部署在iOS/iPadOS 18中进行谈判。这项技术被视为可能增强的Siri功能&#xff0c;即“AI聊天机器人”。除Siri外&#xff0c;新技术还可能改善Spotl…

mac idea 下载spring 源码遇到的问题

一、Kotlin: warnings found and -Werror specified 这个问题网上看了很多文章多说是缺少cglib、objenesis包。然后执行了 实际还是没有什么用 解决&#xff1a; 最后自己看了一下前面一个警告。说的就是版本太低。所以我觉得是这个前置问题导致的 然后搜索了改这个Kotlin版本…

在Mac上恢复已删除文件夹的最佳方法

“嗨&#xff0c;我从我的Mac Documents文件夹中删除了很多文件夹。已删除的文件夹包含我的重要文档和文件&#xff0c;是否可以取回它们&#xff1f;垃圾桶已被清洁软件清空。如何在我的Mac上恢复已删除的文件夹&#xff1f; 当您在 Mac 上删除 1 或 2 个文件夹时&#xff0c…

免费开源语音克隆-GPT-SoVITS-WebUI只需 5 秒的声音样本

语音克隆-GPT-SoVITS-WebUI 强大的少样本语音转换与语音合成Web用户界面。 功能&#xff1a; 零样本文本到语音&#xff08;TTS&#xff09;&#xff1a; 输入 5 秒的声音样本&#xff0c;即刻体验文本到语音转换。 少样本 TTS&#xff1a; 仅需 1 分钟的训练数据即可微调模型…

抖音小店运营实战班,全新升级 从零到进阶精通 分享月销百万小店核心秘密

课程内容&#xff1a; 1 2024抖音电商发展趋势及抖店运营策略(直播2024 0412).mp4 2 1-1抖音小店入驻流程(直播2024 04 12),mp4 3 1-2个体店铺VS企业店铺有什么区别(直播20240412).mp4 4 1-3抖音小店店铺搭建(直播2024 04 12).mp4 5 2-1-如何避免违禁词(附违禁词大全)(直播…

设计模式之模板模式

模板模式&#xff08;Template Method Pattern&#xff09;是行为设计模式之一&#xff0c;它定义了一个操作中的算法骨架&#xff0c;而将一些步骤延迟到子类中实现。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤&#xff0c;从而达到复用算法框架…

每日一题(力扣213):打家劫舍2--dp+分治

与打家劫舍1不同的是它最后一个和第一个会相邻&#xff0c;事实上&#xff0c;从结果思考&#xff0c;最后只会有三种&#xff1a;1 第一家不被抢 最后一家被抢 2 第一家被抢 最后一家不被抢 3 第一和最后一家都不被抢 。那么&#xff0c;根据打家劫舍1中的算法 我们能算出在i…

蓝桥杯练习系统(算法训练)ALGO-951 预备爷的悲剧

资源限制 内存限制&#xff1a;512.0MB C/C时间限制&#xff1a;1.0s Java时间限制&#xff1a;3.0s Python时间限制&#xff1a;5.0s 问题描述 英语预备爷gzp是个逗(tu)比(hao)&#xff0c;为了在即将到来的英语的quiz中不挂科&#xff0c;gzp废寝忘食复习英语附录单词…

PAT (Advanced Level) - 1006 Sign In and Sign Out

模拟 #include <iostream> #include <cstring> #include <algorithm> using namespace std;int m; string open_id, open_time; string close_id, close_time;int main(){cin >> m;for (int i 0; i < m; i ) {string id, in_time, out_time;cin …

深度学习:基于Keras,使用长短期记忆人工神经网络模型(LSTM)对股票市场进行预测分析

前言 系列专栏&#xff1a;机器学习&#xff1a;高级应用与实践【项目实战100】【2024】✨︎ 在本专栏中不仅包含一些适合初学者的最新机器学习项目&#xff0c;每个项目都处理一组不同的问题&#xff0c;包括监督和无监督学习、分类、回归和聚类&#xff0c;而且涉及创建深度学…

Unity开发一个FPS游戏之四

在前面的系列中&#xff0c;我已介绍了如何实现一个基本的FPS游戏&#xff0c;这里将继续进行完善&#xff0c;主要是增加更换武器以及更多动作动画的功能。 之前我是采用了网上一个免费的3D模型来构建角色&#xff0c;这个模型自带了一把AR自动步枪&#xff0c;并且自带了一些…

链表经典面试题上

目录 创作不易&#xff0c;如若对您有帮助&#xff0c;还望三连&#xff0c;谢谢&#xff01;&#xff01;&#xff01; 题目一&#xff1a;203. 移除链表元素 - 力扣&#xff08;LeetCode&#xff09; 题目二&#xff1a;206. 反转链表 - 力扣&#xff08;LeetCode&#xff…

电脑如何查看一段时间内是否被人使用过?

前言 有时候我们可能会担心别人未经许可使用我们的电脑。为了确保自己不在场时电脑是否被使用过&#xff0c;以下两种方法可能会帮到你 第一种方法 WinX打开事件查看器。像WinX能快速打开很多东西&#xff0c;比如安装的应用(可以进行软件的删除)&#xff0c;设备管理器&…

网络性能测试工具iperf3 和iperf

目录 1. iperf工具介绍 2. 下载安装 3. 使用方法 1. iperf工具介绍 iperf 是一个网络性能测试工具&#xff0c;用于测量网络带宽和性能。它可以在客户端和服务器之间进行数据传输&#xff0c;并提供了丰富的选项来配置测试参数和输出格式。 iperf 和 iperf3 都是用于测量网…

什么是发售?

什么是发售? 很多人不知道什么是发售,因为这个词刚被广而告之,在这里普及一下什么是发售? 发售,它是通过一套流程,把你的产品疯狂大卖的一种技术。通常有三个步骤,就是造势、预售、发售。那么这三个词怎么理解呢? 第一步:造势 造势的核心是引发关注,但是不做销售…

【机器视觉】Segment Anything模型(SAM) C# 推理

Facebook开源的Segment Anything是一个基于大型预训练模型的计算机视觉工具&#xff0c;它使用一种新的范式来处理图像分割任务。这个范式不依赖于传统的预训练加微调&#xff08;pretrainfinetune&#xff09;方法&#xff0c;而是通过提示&#xff08;prompt&#xff09;加上…
最新文章