前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot Application 监控管理利器: Spring Boot Admin

Spring Boot Application 监控管理利器: Spring Boot Admin

作者头像
happyJared
发布2019-04-18 20:29:00
6170
发布2019-04-18 20:29:00
举报
文章被收录于专栏:happyJaredhappyJared

文章前言

上篇文章了解了 Spring Boot Actuator,引入后即可通过访问不同的端点,来获得相应的监控信息。

对应 HTTP 方式请求,返回的数据都是 JSON 格式,这对于运维或是其他人员来说当然不是很方便直观,特别是当需要监控的应用越来越多时,如果还依旧通过地址栏来逐个访问,就显得过于繁琐和低效了。下面,我们再来认识下 Spring Boot Admin 这个 Spring Boot Application UI 监控管理工具。

快速上手

Spring Boot Admin 由 Server 和 Client 两个端组成,其中 Client 端通常是需要被监控的应用。先来配置下 Server 端的依赖,考虑到安全性方面的问题,这里还额外加入了 Security:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
    </dependencies>

接着在 application.yml 文件中配置登录用户及密码:

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

必要的 Security 配置:

@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();
        // 设置redirectTo参数和登录成功重定向地址
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                // 授予对静态资源和登录页面的公共访问权
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                // 除上面配置的其他请求都必须经过身份验证
                .anyRequest().authenticated()
                .and()
                // 配置登录和退出请求路径
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                // 启用HTTP-Basic,这是Spring Boot Admin Client注册所必需的
                .httpBasic().and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
    
}

最后,在启动类上加入 @EnableAdminServer ,运行服务后访问:http://localhost:8080 ,输入用户名密码 admin 进入系统:

@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminServerApplication.class, args);
    }

}

Server 端到此就搭建好了,下面再来处理 Client 端服务,同样的先加入相关依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

由于引入了 Security 模块,为了可以正常访问到 Actuator 的 Endpoints,这里还需要做相应的配置处理:

@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().ignoringAntMatchers("/actuator/**").disable();
    }

}

在配置文件中加入必要信息:

server:
  port: 8081
spring:
  application:
    name: spring-boot-admin-client
  boot:
    admin:
      client:
        # Spring Boot Admin Server 服务地址,可配置多个
        url: http://localhost:8080
        instance:
          name: ${spring.application.name}
          prefer-ip: true
        # Spring Boot Admin Server 认证信息
        username: admin
        password: admin
        # 设置为true,客户端将只注册1个 Admin Server 服务(按定义的顺序)
        # 当该 Admin Server 服务宕机,将自动注册下个 Admin Server 服务器;
        # 如果为false,将针对所有管理服务器进行注册
        register-once: true
# Actuator 配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
info:
  version: @project.version@
  name: @project.artifactId@
  author: happyJared
  blog: https://blog.mariojd.cn/

最后,启动 Client 服务,我们来查看 Admin Server 提供的 UI 监控管理系统:

点击 Instances 进入,这时就可以方便的查看该应用的所有监控状态信息。

此外,Spring Boot Admin 还支持动态更改 Logger Level、提供异常监控告警等功能,这些姿势可以自行解锁。

参考阅读

Spring Boot Admin Spring Boot Admin Reference Guide 监控管理之Spring Boot Admin使用

示例源码 欢迎关注我的个人公众号:超级码里奥 如果这对您有帮助,欢迎点赞和分享,转载请注明出处

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章前言
  • 快速上手
  • 参考阅读
相关产品与服务
多因子身份认证
多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档