服务监控之 Spring Boot Admin.

摘要:
在这样的背景下,就诞生了另外一个开源软件:SpringBootAdmin。SpringBootAdmin是一个针对SpringBootActuator进行UI美化封装的监控工具。集群的每个应用都认为是一个客户端,通过HTTP或者使用Eureka注册到SpringBootAdminServer中进行展示,SpringBootAdminUI使用AngularJs将数据展示在前端。下面将给大家介绍如何使用SpringBootAdmin对SpringBoot应用进行监控。

一、概述

 开始阅读这篇文章之前,建议先阅读下《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();
    }
}
  • 效果

服务监控之 Spring Boot Admin.第1张

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.第2张

三、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. 效果图

服务监控之 Spring Boot Admin.第3张
服务监控之 Spring Boot Admin.第4张

2.源代码地址

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

免责声明:文章转载自《服务监控之 Spring Boot Admin.》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ms17010复现、ms16-075提权Mac下Boost环境搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jquery页面中添加键盘按键事件,如ESC事件

$(document).keydown(function(event){ if(event.keyCode == 38 || event.keyCode == 104){ i--; if(i<=0){ i=0; }}else if (event.keyCode == 40 || event.keyCode == 98){ i++; if(i&g...

移动设备上的触摸事件

在 iOS 的 Safari 浏览器中,增加了一些触摸(touch)事件和手势(gesture)事件,这里总结一下它们的用法。 一、触摸事件 iOS 浏览器的触摸事件包括 touchstart,touchmove,touchend,touchcancel。Android 的浏览器中也同样支持这些事件。这些触摸事件的触发条件如下: touchstart:手...

如何在win7下通过easyBCD引导安装Ubuntu14.04

不需要U盘 在Win7下装Ubuntu双系统 本文测试安装的是32位的ubuntu-14.10-desktop-i386.iso 系统。 准备: Ubuntu系统ISO文件。EasyBCD 软件。  Step 1. 在windows里面把空余空间腾出来 计算机右键,管理,磁盘管理,通过压缩卷等方法得到要分给 Ubuntu系统的分区。或者原来你就有某个盘用于...

vue中 拖动元素边框 改变元素宽度

先上效果图: 如图所示,通过拖动来改变表单的宽度。 但实际上,这边并不是表单的边框,而是一个单独的组件。通过监听鼠标的down,move以及up事件。 我们可以单独的写个组件handle.vue。 <template> <div @mousedown="mouseDown"></div> </templa...

2020-05-15 rocketmq-spring-starter结合disconf使用

项目中要用到rocketmq,想要封装一个方便使用的client,网上搜索到官方的一个项目: https://github.com/apache/rocketmq-spring 简单写了demo测试了下,感觉用起来接近rabbitmq,决定在实际项目里测试下。 disconf配置如下: rocketmq.name-server=127.0.0.1:987...

迁移与裁剪linux系统

系统启动流程: POST 加电自检,载入一段程序完成基本及核心硬件的检测 --> BIOS 根据BIOS中设定的引导次序,查找对应设备上的MBR --> 读取MBR的BootLoader,BootLoader中配置了要引导的操作系统的内核的位置 --> 读取内核,内核进行初始化,需要initrd 将Kernel和真正的根文件系统连接起来...