• SpringBootAdmin环境搭建与使用


    概述

    Spring Boot 有一个非常好用的监控和管理的源软件,这个软件就是 Spring Boot Admin。该软件能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。

    主要的功能点有:

    显示应用程序的监控状态
    应用程序上下线监控
    查看 JVM,线程信息
    可视化的查看日志以及下载日志文件
    动态切换日志级别
    Http 请求信息跟踪
    其他功能点……

    可点击 https://github.com/codecentric/spring-boot-admin 更多了解 Spring-boot-admin。

    搭建服务流程说明

    admin-server admin 监控服务
    admin-order amdin 客户端服务

    Admin-server

    引入依赖

           <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>de.codecentricgroupId>
                <artifactId>spring-boot-admin-starter-serverartifactId>
                <version>2.3.1version>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-securityartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    创建配置文件

    server:
      port: 9000
    
    
    spring:
      application:
        ## 注册服务名
        name: springboot-admin
      security:
        user:
          name: admin
          password: admin
    
    management:
      endpoint:
        health:
          show-details: always
          
          
    logging:
      file:
        path: C:\Users\Administrator\logs\sba
        name: springboot-admin.log      
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    配置启动类

    /**
     * SpringAdmin的服务端
     *
     * @author Promsing(张有博)
     * @version 1.0.0
     * @since 2022/8/20 - 19:24
     */
    @SpringBootApplication
    @EnableAdminServer
    public class AdminApplication {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(AdminApplication.class, args);
            ConfigurableEnvironment environment = context.getEnvironment();
    
            String serverPort = environment.getProperty("server.port");
            System.out.println("SpringBootAdmin: http://127.0.0.1:" + serverPort);
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    开启鉴权

    package com.promsing.admin.config;
    
    import de.codecentric.boot.admin.server.config.AdminServerProperties;
    import org.springframework.boot.autoconfigure.security.SecurityProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.Customizer;
    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;
    import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
    import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
    
    import java.util.UUID;
    
    @Configuration(proxyBeanMethods = false)
    public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    
        private final AdminServerProperties adminServer;
    
        private final SecurityProperties security;
    
        public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
            this.adminServer = adminServer;
            this.security = security;
        }
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
    
            http.authorizeRequests(
                    (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() // <1>
                            .antMatchers(this.adminServer.path("/actuator/info")).permitAll()
                            .antMatchers(this.adminServer.path("/actuator/health")).permitAll()
                            .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() // <2>
            ).formLogin(
                    (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() // <3>
            ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) // <4>
                    .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5>
                            .ignoringRequestMatchers(
                                    new AntPathRequestMatcher(this.adminServer.path("/instances"),
                                            HttpMethod.POST.toString()), // <6>
                                    new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
                                            HttpMethod.DELETE.toString()), // <6>
                                    new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // <7>
                            ))
                    .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    部署成功

    SpringBootAdmin首页
    详情页面

    Cleinet

    引入依赖

      
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
    
            <dependency>
                <groupId>de.codecentricgroupId>
                <artifactId>spring-boot-admin-starter-clientartifactId>
                <version>2.3.1version>
            dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    添加配置文件+鉴权

    
    spring:
      application:
        name: msb-web
      messages:
        basename: i18n/login
      boot:
        admin:
          client:
            url: http://127.0.0.1:9000
            instance:
              prefer-ip: true # 使用ip注册进来
            username: admin
            password: admin
    server:
      port: 9001
    
    
    management:
      endpoint:
        health:
          show-details: always
      endpoints:
        enabled-by-default: true
        web:
          base-path: /actuator
          exposure:
            include: '*'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    客户端开启日志配置

    
    spring:
      application:
        name: msb-web
      messages:
        basename: i18n/login
      boot:
        admin:
          client:
            url: http://127.0.0.1:9000
            instance:
              prefer-ip: true # 使用ip注册进来
            username: admin
            password: admin
    server:
      port: 9001
    
    
    management:
      endpoint:
        health:
          show-details: always
      endpoints:
        enabled-by-default: true
        web:
          base-path: /actuator
          exposure:
            include: '*'
    
    
    ## 开启日志配置
    logging:
      file:
        path: C:\Users\Administrator\logs\sba
        name: boot-web.log
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    查看日志配置

    服务端开启邮件提醒

    引入依赖

            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    重新编辑配置文件

    spring:
      application:
        ## 注册服务名
        name: springboot-admin
      security:
        user:
          name: admin # 登录的账号密码
          password: admin
      mail:
        host: smtp.qq.com # 发件人使用的qq邮箱服务
        username: 975819442@qq.com
        # 授权码,不是密码,在qq邮箱设置‐账号里面有生成授权码
        password: xsfmzwhtefbbhi ##授权码
        port: 465 ## 注意端口号
      boot:
        admin:
          notify:
            mail:
              # 发件人
              from: 975819442@qq.com
              # 收件人,多个中间用,分隔
              to: zhangyb2800@163.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏

  • 相关阅读:
    Java微信小程序奶茶在线预定点单系统 uniapp小程序
    【Mybatis实战】01——demo入门案例
    R语言使用purrr包的pmap函数或者map2函数来向量化普通标量函数来处理向量数据、计算两个向量之间的按元素的最大公约数GCD
    全网最详细Centos7搭建Redis集群
    经典算法之快速排序(QuickSort)
    JavaScript中的异步编程
    Prism 入门03,模块化介绍使用
    [附源码]java毕业设计家庭医生系统
    【Python百日进阶-Web开发-Feffery】Day397 - fac其他02:AntdBackTop回到顶部
    「实用场景教程」如何用日程控件DHTMLX Scheduler制作酒店预订日历?(一)
  • 原文地址:https://blog.csdn.net/promsing/article/details/126556511