栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot Admin服务监控

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot Admin服务监控

Spring Boot Admin服务监控 一、介绍 1、SBA简介

官网参考文档

Spring Boot Admin(SBA)是一个开源的社区项目,用于管理和监控 Spring Boot 应用程序。应用程序可以通过 http 的方式,或 Spring Cloud 服务发现机制注册到 SBA 中,然后就可以实现对 Spring Boot 项目的可视化管理和查看了。

Spring Boot Admin 可以监控 Spring Boot 单机或集群项目,它提供详细的健康 (Health)信息、内存信息、JVM 系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、Spring Boot 缓存查看和管理等功能。

2、SBA工程介绍

Spring Boot Admin分为服务端和客户端,服务端、客户端都是独立的web项目,服务端是监控程序,客户端是被监控的程序,这里需要创建两个SpringBoot工程,一个对应服务端,一个对应客户端

二、SBA服务端构建 1、SBA服务端简单构建

首先新建一个SpringBoot工程,在pom.xml中导入相关依赖


    de.codecentric
    spring-boot-admin-starter-server
    
    2.6.6



    org.springframework.boot
    spring-boot-starter-web

其次在配置文件设置好端口后,在启动类上添加注解@EnableAdminServer,启动项目打开浏览器访问http://ip:port即可访问

2、服务端访问权限设置 2.1 Spring Security账号登录

SBA 默认是没有权限验证的,而生产环境一定要配置权限验证,我们这里通过添加 Spring Security 框架来实现权限拦截。首先引入相关依赖



    org.springframework.boot
    spring-boot-starter-security

在application.properties里设置账号密码

#配置一个账号和密码
spring.security.user.name=admin
spring.security.user.password=123456

编写配置类

@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
@Component
public class HttpHeadersProviderConfig implements HttpHeadersProvider {
    @Value("${server.port}")
    private String port;

    @Override
    public HttpHeaders getHeaders(Instance instance) {
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置约定好的请求头参数
        httpHeaders.add("spring-boot-admin-service", port);
        return httpHeaders;
    }
}
3、数据监控与通知 3.1 自带邮件报警通知

邮件相关可以参考SpringBoot异步、邮件、定时任务

首先引入邮件相关依赖


    org.springframework.boot
    spring-boot-starter-mail

在 SBA 的配置文件 application.properties 中添加以下收、发邮箱的配置

# 配置发送邮箱
spring.boot.admin.notify.mail.from=xxx@qq.com
# 配置接收邮箱
spring.boot.admin.notify.mail.to=xxx@qq.com
# 配置邮箱 smtp 地址(qq 发送邮箱的固定 host 是 smtp.qq.com)
spring.mail.host=smtp.qq.com
# 配置邮箱授权码(此处为授权码,而非密码,获取授权码本文下一步有说明),需要开启邮箱SMTP服务
spring.mail.password=xxxxxx
# 配置邮箱的账户名(这个是上面配置发送邮件的账户名)
spring.mail.username=xxx@qq.com

经过以上配置之后,无需添加任何代码,就可以实现项目状态改变的邮件提醒功能了。

3.2 自定义通知

自定义通知,当实例状态发生改变,及时通知(发邮件、企业微信、钉钉都可以,自己实现)

@Component
public class CustomNotifierConfig extends AbstractStatusChangeNotifier {

    public CustomNotifierConfig(InstanceRepository repository) {
        super(repository);
    }

    @Override
    protected Mono doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            if (event instanceof InstanceStatusChangedEvent) {
                System.out.println("实例名称:"+instance.getRegistration().getName());
                System.out.println("实例服务地址:"+instance.getRegistration().getServiceUrl());
                String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
                switch (status) {
                    case "DOWN":
                        System.out.println("健康检查没通过!");
                        break;
                    case "OFFLINE":
                        System.out.println("服务离线!");
                        break;
                    case "UP":
                        System.out.println("服务上线!");
                        break;
                    case "UNKNOWN":
                        System.out.println("服务未知异常!");
                        break;
                    default:
                        System.out.println(status);
                        break;
                }

            }
        });
    }
}
三、SBA客户端构建 1、简单构建

新建工程,导入相关依赖


  de.codecentric
  spring-boot-admin-starter-client
  2.6.6




  org.springframework.boot
  spring-boot-starter-actuator

在application.yml中配置

spring:  
  # Spring Boot Admin 监控服务器端地址
  boot:
    admin:
      client:
        port: 9000
        url: http://localhost:${spring.boot.admin.client.port}
        # 如果设置了账号密码就需要
        username: admin
        password: 123456

        
# 开启监控所有项
management:
  endpoints:
    #公开所有端点web接口
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      #显示db、redis、rabbti连接情况等
      show-details: always
    #启用端点,默认情况下,除shutdown以外的所有端点均已启用
    shutdown: true
    # 具体的日志路径
    logfile:
      external-file: xxx
    
#添加描述
info:
  describe: SpringBootAdmin,Test Client Service!
  author: shawn
  version: 1.0.0


配置完启动后就可以查看相关监控项,具体客户端的监控首页,有我们在客户端写的info信息、磁盘监控、堆、非堆内存监控、进程、线程监控、垃圾回收监控

2、actuator的web端口安全设置

客户端是要暴露actuator的web端口的,为了安全,客户端只允许服务端请求actuator的web接口(通过约定好的请求头来判断)

@WebFilter
@ServletComponentScan
@Component
public class ActuatorFilter implements Filter {
    @Value("${spring.boot.admin.client.port}")
    private String adminServicePort;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        //判断约定好的请求头参数
        if (request.getRequestURI().contains("/actuator") && !adminServicePort.equals(request.getHeader("spring-boot-admin-service"))){
            throw new RuntimeException("抱歉,你无权限访问,Actuator端口受保护! Sorry, you have no permission to access it,Actuator port protected!");
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}
四、总结

SpringBoot-Admin监控Client有两种模式:

  • Client端引入spring-boot-admin-starter-client依赖,配置好Server的相关信息。

  • 将所有Client端注册到服务发现(Eureka)组件中去,同时把Server端也注册,这样Server端就可以监控所有Client端了,不用对Client都添加依赖。

    可以参考:SpringCloud-2020.0.3版本简单入门


参考文章:

https://www.cnblogs.com/huanzi-qch/p/14894794.html

https://www.cnblogs.com/vipstone/p/15845489.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/873832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号