首页 > 其他分享 >【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin

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

时间:2023-04-29 11:04:20浏览次数:34  
标签:SpringBoot Admin Spring boot springframework Boot org



文章目录

  • Spring Boot 集成 Spring Boot Admin
  • Spring Boot Admin 服务端
  • 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<[email protected]>
          # 接受人,多个逗号隔开
          to: [email protected]
          # 抄送人,多个逗号隔开
          cc: [email protected]
  # 登录配置
  security:
    user:
      name: "admin"
      password: "123456"
  # 邮件发送者信息
  mail:
    host: smtp.qq.com
    username: [email protected]
    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"


标签:SpringBoot,Admin,Spring,boot,springframework,Boot,org
From: https://blog.51cto.com/u_15269008/6236653

相关文章

  • SpringBoot RabbitMQ死信队列
    1.死信定义无法被消费的消息,称为死信。如果死信一直留在队列中,会导致一直被消费,却从不消费成功,专门有一个存放死信的队列,称为死信队列(DDX,dead-letter-exchange)。死信队列DLX,DeadLetterExchange的缩写,又死信邮箱、死信交换机。其实DLX就是一个普通的交换机,和一般的交换......
  • SpringCloud Stream集成RabbitMQ
    1.概述SpringCloudStream框架抽象出了三个最基础的概念来对各种消息中间件提供统一调用:DestinationBinders:负责集成外部消息系统的组件。DestinationBinding:由Binders创建的,负责沟通外部消息系统、消息发送者和消息消费者的桥梁。Message:消息发送者与消息消费......
  • Spring Cloud Gateway RCE
    SpringCloudGatewayRCE目录SpringCloudGatewayRCE一、基本介绍二、漏洞复现三、原理分析四、修复方法一、基本介绍CVE编号:CVE-2022-22947​SpringCloudGateway是Spring中的一个API网关。其3.1.0及3.0.6版本(包含)以前存在一处SpEL表达式注入漏洞,当攻击者可以访问Actuato......
  • springboot 发送邮件
    @AutowiredprivateJavaMailSenderjavaMailSender;publicStringsendEmail(ToMailtoMail){SimpleMailMessagemessage=newSimpleMailMessage();message.setFrom("[email protected]");message.setTo("my_M......
  • EHCache spring
    EHCachespring  import:importorg.springframework.cache.annotation.Cacheable;  注解(我放在service方法上,注意这个注解,如果用SPRING的Cacheable,就跟这一样;如果用GOOGLE的,这几个配置及import都得一致):@Cacheable(value="baseCache",key="'myid_'+#date")publicString......
  • jeecgboot整合JdbcTemplate方便多表联合查询
    感觉jeecgboot处理复杂的多表联合查询有点费劲,就自己实现了JdbcTemplate的整合,其实也不是整合吧,因为jeecgboot已经把JdbcTemplate整合进来了。我查了下项目的依赖关系,发现jeecg-boot-base-core模块依赖了mybatis-plus-boot-starter,而mybatis-plus-boot-starter依赖了spri......
  • Spring Boot经验
    Spring、SpringBoot经验本文记录作者在实际使用Spring或则SpringBoot过程中遇到比较好的案例或则经验,以供开发学习使用1.校验篇生产过程中前后端都会进行数据格式的校验,后端校验一般采用JSR303的校验模式1.1使用引入依赖<dependency><groupId>javax.validation<......
  • Spring Boot 和 Docker 实现微服务部署
    Springboot开发轻巧的微服务提供了便利,Docker的发展又极大的方便了微服务的部署。这篇文章介绍一下如果借助maven来快速的生成微服务的镜像以及快速启动服务。其实将SpringBoot项目用Docker部署也不是什么多么神秘的技术,也要先生成镜像,再用镜像启动容器,如果说有什么方便......
  • Spring Cloud 系列之 Eureka 实现服务注册与发现
    如果你对SpringCloud体系还不是很了解,可以先读一下SpringCloud都有哪些模块Eureka是Netflix开源的服务注册发现组件,服务发现可以说是微服务架构的核心功能了,微服务部署之后,一定要有服务注册和发现的能力,Eureka就是担任这个角色的。如果你用过dubbo的话,那一定知道dubbo......
  • Spring 实现自定义 bean 的扩展
    Springmvc提供了扩展xml的机制,用来编写自定义的xmlbean,例如dubbo框架,就利用这个机制实现了好多的dubbobean,比如 <dubbo:application>、<dubbo:registry> 等等,只要安装这个标准的扩展方式实现配置即可。扩展自定义bean的意义何在假设我们要使用一个开源框架或者一套......