SpringBoot-Admin 服务监控

简单介绍

Spring Boot Actuator 是 Spring Boot 自带的一个功能模块,

提供了一组已经开箱即用的生产环境下常用的特性和服务,比如应用程序的健康检查、信息暴露、度量收集、日志记录等。

在实际项目中,Actuator 可以帮助我们快速了解应用程序的运行状态和性能瓶颈。

整合SpringBoot-Admin监控,Spring Boot Admin 就是将 Spring Boot Actuator中提供的endpoint信息可视化展示。

环境

Springboot: 2.1.1.RELEASE

注意:不同的SpringBoot版本,所用到的依赖会不一致。本版本亲测可用哦。

服务端简单搭建

核心依赖


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.6</version>
</dependency> <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.7</version>
</dependency>

启动类


@EnableAdminServer //开启admin服务端
@SpringBootApplication
public class BootAdminServerApplication { public static void main(String[] args) {
SpringApplication.run(BootAdminServerApplication.class, args);
} }

配置文件

server:
port: 8001
servlet:
context-path: /admin-server
spring:
application:
name: admin-server

启动服务后,访问:http://localhost:8001/admin-server

安全认证

核心依赖


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

SecuritySecureConfig 拦截器Code


@EnableWebSecurity
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServer) {
this.adminContextPath = adminServer.getContextPath();
} @Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests()
.antMatchers(adminContextPath + "/login",
adminContextPath + "/assets/**",
adminContextPath + "/manage/**",
adminContextPath + "/actuator/**",
adminContextPath + "/login.html"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler)
.and()
.logout().logoutUrl(adminContextPath + "/logout")
.and()
.httpBasic()
.and()
.csrf().disable();
} }

修改配置

新增以下配置

spring:
security:
user:
name: admin
password: admin

启动服务后,访问:http://localhost:8001/admin-server

输入用户名,密码即可。

客户端

核心依赖

  <!--加入spring-boot-admin连接端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

配置文件

如果你不想开启安全认证请参考以下的配置。

admin-server 未开启安全认证


server:
port: 7001 spring:
application:
name: admin-client
# 配置spring-boot-admin服务端的地址
boot:
admin:
client:
url: 'http://localhost:8001/admin-server' #展示全部细节信息
management:
endpoints:
web:
exposure:
include: '*'
#允许admin工程远程停止本应用
endpoint:
health:
enabled: true
show-details: always
shutdown:
enabled: true

admin-server 开启安全认证

server:
port: 7001 spring:
application:
name: admin-client
# 配置spring-boot-admin服务端的地址
boot:
admin:
client:
url: 'http://localhost:8001/admin-server'
username: admin
password: admin #展示全部细节信息
management:
endpoints:
web:
exposure:
include: '*'
#允许admin工程远程停止本应用
endpoint:
health:
enabled: true
show-details: always
shutdown:
enabled: true

查看Admin-Server端页面

如何实现服务 告警通知

当我们的服务发生异常时, 可以通过邮件、微信、钉钉等发送告警信息。

Server 端新增以下配置类。

@Component
public class AdminNotifier extends AbstractStatusChangeNotifier { private static final Logger log = LoggerFactory.getLogger(AdminNotifier.class); /**
* 消息模板
*/
private static final String template = "<<<%s>>> \n 【服务名】: %s(%s) \n 【状态】: %s(%s) \n 【服务ip】: %s \n 【详情】: %s"; private String titleAlarm = "系统告警"; private String titleNotice = "系统通知"; private String[] ignoreChanges = new String[]{"UNKNOWN:UP", "DOWN:UP", "OFFLINE:UP"}; public AdminNotifier(InstanceRepository repository) {
super(repository);
} @Override
protected boolean shouldNotify(InstanceEvent event, Instance instance) {
if (!(event instanceof InstanceStatusChangedEvent)) {
return false;
} else {
InstanceStatusChangedEvent statusChange = (InstanceStatusChangedEvent) event;
String from = this.getLastStatus(event.getInstance());
String to = statusChange.getStatusInfo().getStatus();
return Arrays.binarySearch(this.ignoreChanges, from + ":" + to) < 0 && Arrays.binarySearch(this.ignoreChanges, "*:" + to) < 0 && Arrays.binarySearch(this.ignoreChanges, from + ":*") < 0;
}
} @Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { if (event instanceof InstanceStatusChangedEvent) {
log.info("Instance {} ({}) is {}", instance.getRegistration().getName(),
event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
String messageText = null;
switch (status) {
// 健康检查没通过
case "DOWN":
log.info("发送 健康检查没通过 的通知!");
messageText = String
.format(template, titleAlarm, instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus(), "健康检查没通过通知",
instance.getRegistration().getServiceUrl(), JSONObject.toJSONString(instance.getStatusInfo().getDetails()));
log.info(messageText);
break;
// 服务离线
case "OFFLINE":
log.info("发送 服务离线 的通知!");
messageText = String
.format(template, titleAlarm, instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus(), "服务离线通知",
instance.getRegistration().getServiceUrl(), JSONObject.toJSONString(instance.getStatusInfo().getDetails()));
log.info(messageText);
break;
//服务上线
case "UP":
log.info("发送 服务上线 的通知!");
messageText = String
.format(template, titleNotice, instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus(), "服务上线通知",
instance.getRegistration().getServiceUrl(), JSONObject.toJSONString(instance.getStatusInfo().getDetails()));
log.info(messageText);
break;
// 服务未知异常
case "UNKNOWN":
log.info("发送 服务未知异常 的通知!");
messageText = String
.format(template, titleAlarm, instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus(), "服务未知异常通知",
instance.getRegistration().getServiceUrl(), JSONObject.toJSONString(instance.getStatusInfo().getDetails()));
log.info(messageText);
break;
default:
break;
}
} else {
log.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(),
event.getType());
}
});
}
}

如下图所示:

文章参考:

https://juejin.cn/post/7124624039921844232

https://juejin.cn/post/7141272117277884447

【Springboot】SpringBoot-Admin 服务监控+告警通知的更多相关文章

  1. SpringBoot系列——admin服务监控

    前言 springboot项目部署起来后,如何实时监控项目的运行状况呢?本文记录使用springboot-admin对服务进行监控. springboot-admin介绍:https://codece ...

  2. Spring-Boot之Admin服务监控-9

    一.Spring Boot Admin用于管理和监控一个或者多个Spring Boot程序.Spring Boot Admin分为Server端和Client 端,Client端可以通过向Http S ...

  3. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  4. SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Prometheus+Grafana实现完整的微服务监控

      无论是使用SpringBootAdmin还是使用Prometheus+Grafana都离不开SpringBoot提供的核心组件Actuator.提到Actuator,又不得不提Micrometer ...

  5. 第七模块 :微服务监控告警Prometheus架构和实践

    119.监控模式分类~1.mp4 logging:日志监控,Logging 的特点是,它描述一些离散的(不连续的)事件. 例如:应用通过一个滚动的文件输出 Debug 或 Error 信息,并通过日志 ...

  6. Spring Cloud第十三篇 | Spring Boot Admin服务监控

    本文是Spring Cloud专栏的第十三篇文章,了解前十二篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  7. springcloud(九) springboot Actuator + admin 监控

    前一章讲的都是Feign项目(调用方)的监控.接下来讲的是服务提供方的监控 一.springboot actuator + springboot admin Spring Boot Admin 是一个 ...

  8. SpringBoot服务监控

    SpringBoot服务监控分为客户端和服务端,即服务端是监控方,客户端为被监控方. 例如需要对线上的SpringBoot服务project-A进行监控,则project-A 为客户端.而监控的服务p ...

  9. SpringBoot Admin应用监控搭建

    简介 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI. 参考手册地址:htt ...

  10. 面试官:聊一聊SpringBoot服务监控机制

    目录 前言 SpringBoot 监控 HTTP Endpoints 监控 内置端点 health 端点 loggers 端点 metrics 端点 自定义监控端点 自定义监控端点常用注解 来,一起写 ...

随机推荐

  1. Go/Python 基于gRPC传输图片

    python程序作为服务端,Go程序作为客户端,基于gPRC进行通信 客户端 定义proto文件: syntax = "proto3"; option go_package = & ...

  2. Python ArcPy批量计算多时相遥感影像的各项元平均值

      本文介绍基于Python中ArcPy模块,对大量长时间序列栅格遥感影像文件的每一个像元进行多时序平均值的求取.   在遥感应用中,我们经常需要对某一景遥感影像中的全部像元的像素值进行平均值求取-- ...

  3. java项目 学生成绩管理系统 (源码+数据库文件)

    ​ 需要的私信我 备注来意:项目名称 来了就点个赞再走呗,即将毕业的兄弟有福了 文章底部获取源码 java项目  学生成绩管理 (源码+数据库文件)技术框架:java+springboot+vue+m ...

  4. 机器学习04-(决策树、集合算法:AdaBoost模型、BBDT、随机森林、分类模型:逻辑回归)

    机器学习04 机器学习-04 集合算法 AdaBoost模型(正向激励) 特征重要性 GBDT 自助聚合 随机森林 分类模型 什么问题属于分类问题? 逻辑回归 代码总结 波士顿房屋价格数据分析与房价预 ...

  5. Java web文件服务器的简单实现

    先分享一个好用的聚合搜索引擎,https://www.tomfind.com/ ,支持谷歌.百度.搜狗.bing.360一键切换,支持网页.音乐.购物.视频搜索.支持抖音.快手.哔哩哔哩.微信.头条. ...

  6. 2022-12-03:部门工资最高的员工。以下数据Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。sql语句如何写? 输出结果如下: department emp

    2022-12-03:部门工资最高的员工.以下数据Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高.sql语句如何写? 输出结果如下: department empl ...

  7. Git开发、发布、缺陷分离模型概述(支持master/develop/feature/release/hotfix类型分支)

    Git是什么? Git是一种分布式版本控制系统,它可以记录文件的修改历史和版本变化,并可以支持多人协同开发.Git最初是由Linux开发者Linus Torvalds创建的,它具有高效.灵活.稳定等优 ...

  8. win10双系统Ubuntu的安装之旅(安装+美化+问题解决方案)

    一.前言 最近想用一下Ubuntu,于是乎开始了win10安装Ubuntu的旅程,安装的过程中是看到了很多前人已经写好的非常详细的教程,那这里我就不再重复造轮子啦,直接放上链接咯- 看看我的成果图- ...

  9. 前端学习 node 快速入门 系列 —— 事件循环

    事件循环 本篇将对以下问题进行讨论: 浏览器有事件循环,node 也有事件循环,两者有什么异同? node 核心特性(事件驱动和非阻塞 I/O )和事件循环有什么关系? node 中的高并发和高性能和 ...

  10. * daemon not running; starting now at tcp:5037

    今日使用weeplus run android时 看错误提示 ,是5037端口的问题 * daemon not running; starting now at tcp:5037 于是找到查看端口的 ...