【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin

Spring Boot 集成 Spring Boot Admin

Spring Boot Admin 是服务端、客户端模式。如果把两个端搭建在同一个项目中也可以,但是客户端要是挂了,服务端也挂了,所以可以但没必要

搭建独立的 Spring Boot Admin 监控模块,目前已实现功能:

  • 实现 Spring Boot 应用监控
  • 实现 Spring Boot Admin 登录认证
  • 实现 Spring Boot Admin 监听到日志异常发送消息到邮箱
  • 实现 Spring Boot Admin 时时查看应用日志

Spring Boot Admin 服务端

        <!-- Spring Boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot 端点监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Spring Boot Admin 服务端 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <!-- Spring Boot Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- Spring Boot Email -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!-- Spring Boot Thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

启动类增加注解服务端注解 @EnableAdminServer

package com.qboot.bootadmin;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBootAdmin 监控服务端
 *
 * @author Tellsea
 * @date 2023/2/4
 */
@SpringBootApplication
@EnableAdminServer
public class QbootBootAdminApplication {

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

由于监控信息输入敏感信息,所以需要增加一个登录拦截器,只有登录成功才能访问

package com.qboot.bootadmin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

/**
 * 权限验证
 *
 * @author Tellsea
 * @date 2023/2/6
 */
@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");
        http.headers().frameOptions().disable();
        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()
                .httpBasic().and()
                .csrf().disable();
    }
}

为了实时监控项目问题,Spring Boot Admin 还配备了监听服务状态等信息发送邮件给指定邮箱

application.yml 配置如下

server:
  port: 9090
  servlet:
    context-path: /qboot-extend-boot-admin

# Spring 配置
spring:
  profiles:
    active: @profileActive@
  application:
    name: qboot-extend-boot-admin

application-dev.yml 配置如下

# Spring 配置
spring:
  # Boot Admin 配置
  boot:
    admin:
      ui:
        public-url: http://localhost:${server.port}
      notify:
        mail:
          # 开启邮箱通知
          enabled: true
          # 不需要发送通知的状态:从状态A:到状态B
          ignore-changes: { "UNKNOWN:UP" }
          # 发件人
          from: qboot-extend-boot-admin<3210054449@qq.com>
          # 接受人,多个逗号隔开
          to: 3210054449@qq.com
          # 抄送人,多个逗号隔开
          cc: 3210054449@qq.com
  # 登录配置
  security:
    user:
      name: "admin"
      password: "123456"
  # 邮件发送者信息
  mail:
    host: smtp.qq.com
    username: 3210054449@qq.com
    password: lppmkekscejodefi

Spring Boot Admin 客户端

客户端的主配置文件中需要增加如下配置

# 端点监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

# Logback 日志配置
logging:
  file:
    name: ./logs/sys-info.log
  config: classpath:logback.xml

客户端直接注册到服务端即可,修改 application-dev.yml 配置文件

# Spring配置
spring:
  # Boot Admin 监控
  boot:
    admin:
      client:
        enabled: true
        url: http://localhost:9090/qboot-extend-boot-admin
        username: "admin"
        password: "123456"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tellsea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值