一、概述

 开始阅读这篇文章之前,建议先阅读下《SpringBoot 之Actuator》,该篇文章提到 Spring Boot Actuator 提供了对单个Spring Boot的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了Spring Boot应用的整个生命周期。但是美中不足的是:

  1. 所有的监控都需要调用固定的接口来查看,如果全面查看应用状态需要调用很多接口,并且接口返回的 Json 信息不方便运营人员理解;
  2. 如果Spring Boot 应用集群非常大,每个应用都需要调用不同的接口来查看监控信息,操作非常繁琐低效。

 在这样的背景下,就诞生了另外一个开源软件:Spring Boot Admin。那么什么是 Spring Boot Admin 呢?Spring Boot Admin 是一个针对 Spring Boot Actuator 进行UI美化封装的监控工具。集群的每个应用都认为是一个客户端(或者说实例),通过HTTP或者使用 Eureka 注册到 Spring Boot Admin Server中进行展示,Spring Boot Admin UI 使用AngularJs将数据展示在前端。

 下面将给大家介绍如何使用Spring Boot Admin对Spring Boot应用进行监控。

二、spring-boot-admin-starter-server

下面介绍 spring-boot-admin-server 的构建,要监控的每个客户端(或者说实例),都可以把 Actuator 数据注册到 server 中进行 UI 渲染展示。

1. pom.xml

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

2. application.yml

server:
port: 3333 spring:
application:
name: monitor

3. Application.java

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

做完以上动作,我们一个 spring-boot-admin-server 项目就搭建好了。以下是一些附加的功能:权限认证和邮件预警。

4. 配置 spring-security 权限验证

  • pom.xml
        <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • application.yml
security:
user:
name: ${monitor.user}
password: ${monitor.password}
  • SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable(); // Requests for the login page and the static assets are allowed
//允许登录页面和静态资源的请求
http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
//这点重要:所有请求都需要认证
http.authorizeRequests().antMatchers("/**").authenticated(); // Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
  • 效果

5、邮件预警

  • pom.xml
        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.5.12.RELEASE</version>
</dependency>
  • application.yml
spring:
mail:
host: smtp.exmail.qq.com
port: 465
username: xxx
password: xxx
properties:
mail:
smtp:
auth: true
debug: true
timeout: 0
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
  • NotifierConfig.java
/**
* 重新配置消息通知
*
* @author : cuixiuyin
*/
@Configuration
@EnableScheduling
public class NotifierConfig {
private static final Logger log = LoggerFactory.getLogger(NotifierConfig.class); @Autowired
private JavaMailSender javaMailSender; @Autowired
private RemindingNotifier remindingNotifier; @Bean
@Primary
public RemindingNotifier remindingNotifier() { RemindingNotifier remindingNotifier = new RemindingNotifier(new AbstractEventNotifier() { @Override
protected void doNotify(ClientApplicationEvent event) throws Exception {
if (event instanceof ClientApplicationStatusChangedEvent) {
ClientApplicationStatusChangedEvent changedEvent = (ClientApplicationStatusChangedEvent) event;
log.info("Application {} ({}) is {}", event.getApplication().getName(), event.getApplication().getId(), changedEvent.getTo().getStatus());
String text = String.format("应用:%s 服务ID:%s,服务ip:%s 状态改变为:[%s ---> %s],时间:%s"
, event.getApplication().getName()
, event.getApplication().getId()
, event.getApplication().getHealthUrl()
, changedEvent.getFrom().getStatus()
, changedEvent.getTo().getStatus()
, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(changedEvent.getTimestamp())));
log.warn(text);
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxx@qq.com");
message.setTo("xxx@163.com");
message.setSubject(event.getApplication().getName() + "服务状态改变");
message.setText(text);
javaMailSender.send(message);
} else {
log.info("Application {} ({}) {}", event.getApplication().getName(), event.getApplication().getId(), event.getType());
}
}
});
// 每5分钟就需要提醒一次,并不一定会提醒,有 RemindingNotifier 里面的状态进行决定
remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5));
return remindingNotifier;
} /**
* 每隔一分钟检查还有那些需要进行提醒
*/
@Scheduled(fixedRate = 1_000L)
public void remind() {
remindingNotifier.sendReminders();
}
}
  • 效果

三、spring-boot-admin-starter-client

我们已经有了一个 spring-boot-admin-server,现在要做的就是如何把客户端(或者说实例)的 Actuator 数据注册到 Server 中。

1. pom.xml

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

2. application.yml

spring:
application:
name: dubbo-provider
boot:
admin:
enabled: true
client:
instance:
name: ${spring.application.name}
prefer-ip: true
url: http://127.0.0.1:3333
management:
endpoints:
web:
exposure:
include: '*'

如此,我们就把客户端(或者说实例)的 Actuator 数据注册到 Server 中了。

附录

1. 效果图



2.源代码地址

Github 演示代码地址:https://github.com/JMCuixy/dubbo-demo

服务监控之 Spring Boot Admin.的更多相关文章

  1. Spring Boot 2.X(十七):应用监控之 Spring Boot Admin 使用及配置

    Admin 简介 Spring Boot Admin 是 Spring Boot 应用程序运行状态监控和管理的后台界面.最新UI使用vue.js重写里. Spring Boot Admin 为已注册的 ...

  2. 如何做自己的服务监控?spring boot 2.x服务监控揭秘

    Actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...

  3. 微服务架构之spring boot admin

    Spring boot admin是可视化的监控组件,依赖spring boot actuator收集各个服务的运行信息,通过spring boot actuator可以非常方便的查看每个微服务的He ...

  4. 如何做自己的服务监控?spring boot 1.x服务监控揭秘

    1.准备 下载可运行程序:http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/ 2.添加服务监控依赖 <d ...

  5. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...

  6. SpringBoot | 第二十八章:监控管理之Spring Boot Admin使用

    前言 上一章节,我们介绍了Actuator的使用,知道了可通过访问不同的端点路径,获取相应的监控信息.但使用后也能发现,返回的监控数据都是以JSON串的形式进行返回的,对于实施或者其他人员来说,不是很 ...

  7. Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用

    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...

  8. spring-boot-plus集成Spring Boot Admin管理和监控应用(十一)

    spring-boot-plus集成Spring Boot Admin管理和监控应用 spring boot admin Spring Boot Admin用来管理和监控Spring Boot应用程序 ...

  9. Spring Boot Admin 2.1.4最新实战教程

    环境的搭建 首先搭建eruka的注册中心 pom.xml <?xml version="1.0" encoding="UTF-8"?> <pr ...

随机推荐

  1. JVM系列三(垃圾收集器).

    一.概述 1. 哪些内存需要回收 上篇文章 我们介绍了 Java 内存运行时区域的各个部分,其中程序计数器.虚拟机栈.本地方法栈三个区域随线程而生,随线程而灭,在这几个区域内就不需要过多考虑回收的问题 ...

  2. 【CentOS7】设置静态IP地址

    [CentOS7]设置静态IP地址 转载:https://www.cnblogs.com/yangchongxing/p/10645871.html 图像化修改 nmtui 查看当前网卡名称 # if ...

  3. Python基础-day02-1

    判断(if)语句 目标 开发中的应用场景 if 语句体验 if 语句进阶 综合应用 01. 开发中的应用场景 生活中的判断几乎是无所不在的,我们每天都在做各种各样的选择,如果这样?如果那样?-- 程序 ...

  4. laravel起步的一些小问题

    工作中主要使用的是.NET,PHP只是我业余喜欢的一门语言,而之前一直用的是yii2框架,觉得Yii2是最好的框架了,然而,laravel在业界的名声太大,被誉为:最优雅的框架,所以,我决定花点时间研 ...

  5. 更改Android设备System目录的文件的写入权限

    有时候我们需要修改/system目录中文件的权限,比如将该目录下的脚本设置写入权限等,但该目录默认只有read权限,此时应该怎么办? 1.安卓设备请确保root;2.连接安卓设备,确保安卓设备打开了“ ...

  6. Csharp:HttpWebRequest or HttpClient

    /// <summary> /// Define other methods and classes here /// </summary> /// <param nam ...

  7. mysql之行转列与列转行

    mysql之行转列与列转行是数据查询的常见操作,以更好的来展示数据,下面就详细说说怎么实现. 行转列 行转列的话,就是将一条一条的行数据记录转换为一条列数据展示,一般来说是根据某一列来做汇总数据的操作 ...

  8. Centos 下安装 Nginx(新)

    今天重新实践了下 CentOS 7.6 下安装 Nginx,总结了一条更直接并简单的方式 从官方获取写入 nginx.repo 的方式 从官网查看文档,获取 nginx.repo 的文档内容,将其内容 ...

  9. Pyhton中变量和数据类型

    一.变量 Python中变量的命名规则: 1.变量名只能包含数字.字母.下划线,且不能用数字打头. eg: message_1是对的但1_message就是错误的 2.变量名不能包含空格. 3.在变量 ...

  10. TimeSpan的用法

    TimeSpan的属性和方法: 下面的列表涵盖了其中的一部分: 属性: Add:与另一个TimeSpan值相加. Days: 返回用天数计算的TimeSpan值.Hours: 返回用小时计算的Time ...