SpringBoot笔记:SpringBoot集成SpringbootAdmin监控

SpringBootAdmin是什么

Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。Spring Boot Actuator 是Springboot体系中非常好用且强大的监控能力节点,极大的方便了我们对springboot应用进行业务监控。但是,Spring Boot Actuator 监控接口返回的都是json数据,需要我们进一步开发自己的监控系统。Spring Boot Admin 就是这样一个系统。

Spring Boot Admin并不是 Spring Boot 官方出品的开源软件,但是其软件质量和使用广泛度都非常的高,并且 Spring Boot Admin 会及时随着 Spring Boot 的更新而更新,当 Spring Boot 推出 2.X 版本时 Spring Boot Admin 也及时进行了更新。

它可以在列表中浏览所有被监控 spring-boot 项目的基本信息、详细的 Health 信息、内存信息、JVM 信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改 logger 的 level。

接入配置

server端配置

添加依赖,我本地使用的springboot版本为2.3.0.RELEASE

		<!-- Spring Boot Admin 的server端 -->
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.2.2</version>
		</dependency>
		<!-- 引入springboot-security 登录Spring oot Admin的时候进行安全认证 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

application.yml配置:

# springboot多环境配置
#端口,项目上下文
server:
  port: 8000
  servlet:
    context-path: /springboot-admin

spring:
  security:
    user:
      name: 'admin'
      password: 'admin@2021'

# 日志输出配置
logging:
  level:
    root: INFO
    org:
      springframework:
        security: WARN
        web: ERROR
  file:
    path: ./logs
    name: './logs/springboot-admin.log'
  pattern:
    file: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n'
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n'

SecuritySecureConfig.java 配置

package com.demo.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;

@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.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();
    }
}

最后启动程序入口配置:

package com.demo;

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

@EnableAdminServer
@SpringBootApplication
public class SpringbootMonitorApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMonitorApplication.class, args);
    }
}

client端配置

添加依赖

        <!-- Spring Boot Admin 的client端 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

增加核心配置,其他省略了

# 默认启动的是测试环境配置
spring:
  profiles:
    active: test
  # 客户端client注册到spring admin上server服务上
  application:
    name: springboot-params-demo
  boot:
    admin:
      client:
        # springboot admin server的注册地址,多个以逗号隔开,并把localhost换成ip
        url: http://hadoop-master:8000/springboot-admin
        username: admin
        password: admin@2021
# 需要暴露监控端口给springboot admin server 访问
management:
  endpoints:
    web:
      exposure:
        include: '*'

这样已经对接完成了,开始测试

测试效果

springboot Admin server登录地址:http://hadoop-master:8000/springboot-admin
先启动server端,然后再启动client端
图1
图2
图3

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

leo825...

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

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

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

打赏作者

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

抵扣说明:

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

余额充值