springBootAdmin监控

ops/2024/10/18 18:22:11/

简介

用于对 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

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

security安全防护配置

java">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();}@Overrideprotected 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,auditeventsmonitor: # 监控发送请求的超时时间default-timeout: 20000security: # 设置账号密码user:name: adminpassword: admin
# 服务端点详细监控信息
management:   trace:http:enabled: trueendpoints:web:exposure:include: "*"endpoint:health:show-details: always

访问 192.168.11.50:9111 项目启动如下

在这里插入图片描述

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

java">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);}@Overrideprotected Mono<Void> doNotify(InstanceEvent event, Instance instance) {// 服务名String serviceName = instance.getRegistration().getName();// 服务urlString 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: 9222spring:application:name: clientboot: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: trueendpoints:web:exposure:include: "*"endpoint:health:show-details: alwayslogfile: # 日志(想在线看日志才配)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: localsecurity:user:name: "admin"password: "admin"application:name: jeecg-monitorcloud:nacos:discovery:server-addr: @config.server-addr@password: nacosusername: nacosmetadata:#如果项目有设置server.servlet.context-path:=/monitor,需要开启以下两个注释
#          management:
#            context-path: ${server.servlet.context-path}/actuatoruser.name: ${spring.security.user.name}user.password: ${spring.security.user.password}# 服务端点详细监控信息
management:trace:http:enabled: trueendpoints: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}/actuatorserver-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.ppmy.cn/ops/29703.html

相关文章

MVC和DDD的贫血和充血模型对比

文章目录 架构区别MVC三层架构DDD四层架构 贫血模型代码示例 充血模型代码示例 架构区别 MVC三层架构 MVC三层架构是软件工程中的一种设计模式&#xff0c;它将软件系统分为 模型&#xff08;Model&#xff09;、视图&#xff08;View&#xff09;和控制器&#xff08;Contro…

自动化测试 selenium基础

前言 我们都知道测试开发工程师的任务是根据用户需求测试用例的同时,害的开发自动化工具来减轻测试压力且提高测试的效率以及质量,这一节我们就来简单谈谈开发简单的自动化工具基础 什么是自动化测试呢?就是将我们需要做的测试交给机器去做,也就是使用代码来模拟人对于机器的行…

Linux CentOS7部署ASP.NET Core应用程序,并配置Nginx反向代理服务器和Supervisor守护服务

前言&#xff1a; 本篇文章主要讲解的是如何在Linux CentOS7操作系统搭建.NET Core运行环境并发布ASP.NET Core应用程序&#xff0c;以及配置Nginx反向代理服务器。因为公司的项目一直都是托管在Window服务器IIS上&#xff0c;对于Linux服务器上托管.NET Core项目十分好奇。因为…

PHP的数组练习实验

实 验 目 的 掌握索引和关联数组&#xff0c;以及下标和元素概念&#xff1b; 掌握数组创建、初始化&#xff0c;以及元素添加、删除、修改操作&#xff1b; 掌握foreach作用、语法、执行过程和使用&#xff1b; 能应用数组输出表格和数据。 任务1&#xff1a;使用一维索引数…

SpringCloud知识点梳理

1. Spring Cloud 综述 1.1 Spring Cloud 是什么 [百度百科]Spring Cloud是⼀系列框架的有序集合。它利⽤Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中⼼、消息总线、负载均衡、断路器、数据监控等,都可以⽤ Spring Boot的开发⻛格…

[实例] Unity Shader 利用顶点着色器模拟简单水波

我们都知道顶点着色器可以用来改变模型各个顶点的位置&#xff0c;那么本篇我们就利用顶点着色器来做一个模拟简单水波的应用。 1. 简谐运动 在进行模拟水波之前&#xff0c;我们需要了解简谐运动&#xff08;Simple Harmonic Motion&#xff09;公式&#xff1a; 其中&#…

【算法基础实验】图论-深度优先搜索和深度优先路径

深度优先(DFS) 理论基础 深度优先搜索&#xff08;DFS, Depth-First Search&#xff09;是图和树的遍历算法中的一种&#xff0c;它从一个节点开始&#xff0c;沿着树的边走到尽可能深的分支&#xff0c;直到节点没有子节点为止&#xff0c;然后回溯继续搜索下一个分支。DFS …

Mac 安装John the Ripper 破解rar(zip)压缩文件

注&#xff1a;仅以此篇记录我满足好奇心所逝去的十几个小时。&#xff08;自娱自乐&#xff09; 1、首先利用 brewhome 包管理工具 安装john the ripper &#xff1a; brew install john-jumbo 如果没有安装brewhome 利用如下命令安装&#xff1a; /bin/zsh -c "$(c…