指标监控神器SpringBootAdmin保姆级教程

在这里插入图片描述

SpringBoot Admin

基于SpringBootAdmin的开源产品很多,我们选择这个:https://github.com/codecentric/spring-boot-admin

1.搭建Admin服务器

创建建对应的SpringBoot项目,添加相关依赖

        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-starter-server</artifactId>            <version>2.5.1</version>        </dependency>

然后放开Admin服务即可

image.png

然后启动服务,即可访问

image.png

这个时候没有服务注册,所以是空的,这时我们可以创建对应的客户端来监控

2.客户端配置

创建一个SpringBoot项目整合Actuator后添加Admin的客户端依赖

        <dependency>            <groupId>de.codecentric</groupId>            <artifactId>spring-boot-admin-starter-client</artifactId>            <version>2.5.1</version>        </dependency>

然后在属性文件中添加服务端的配置和Actuator的基本配置

server.port=8081# 配置 SpringBoot Admin 服务端的地址spring.boot.admin.client.url=http://localhost:8080# Actuator的基本配置management.endpoints.web.exposure.include=*

然后我们再刷新Admin的服务端页面

image.png

那么我们就可以在这个可视化的界面来处理操作了

image.png

3.服务状态

我们可以监控下MySQL的状态,先添加对应的依赖

        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>        </dependency>

然后添加对应的jdbc配置

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mysql-base?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123456

然后我们在Admin中的health中就可以看到对应的数据库连接信息

image.png

注意当我把MySQL数据库关闭后,我们来看看

image.png

我们可以看到Admin中的应用墙变灰了

image.png

image.png

启动服务后,发现又正常了,然后我们修改下数据库连接的超时时间

# 数据库连接超时时间spring.datasource.hikari.connection-timeout=2000

关闭数据库后,我们发下应用变红了

image.png

image.png

设置数据库连接超时后即可在有效的时间内发下应用的状态。

  • 绿色:正常状态
  • 灰色:连接客户端健康信息超时
  • 红色:可以看到具体的异常信息
  • 4.安全防护

    其实我们可以发现在SpringBootAdmin的管理页面中我们是可以做很多的操作的,这时如果别人知道了对应的访问地址,想想是不是就觉得恐怖,所以必要的安全防护还是很有必要的,我们来看看具体应该怎么来处理呢?

    由于在分布式 web 应用程序中有几种解决身份验证和授权的方法,Spring Boot Admin 没有提供默认的方法。默认情况下,spring-boot-admin-server-ui 提供了一个登录页面和一个注销按钮。

    导入依赖:

            <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-security</artifactId>        </dependency>

    然后添加对应的配置类

    package com.bobo.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.authentication.builders.AuthenticationManagerBuilder;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()                        .antMatchers(this.adminServer.path("/actuator/info")).permitAll()                        .antMatchers(this.adminServer.path("/actuator/health")).permitAll()                        .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()        ).formLogin(                (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()        ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())                .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())                        .ignoringRequestMatchers(                                new AntPathRequestMatcher(this.adminServer.path("/instances"),                                        HttpMethod.POST.toString()),                                new AntPathRequestMatcher(this.adminServer.path("/instances/*"),                                        HttpMethod.DELETE.toString()),                                new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))                        ))                .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));    }    // Required to provide UserDetailsService for "remember functionality"    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        auth.inMemoryAuthentication().withUser(security.getUser().getName())                .password("{noop}" + security.getUser().getPassword()).roles("USER");    }}

    然后对应的设置登录的账 密码

    spring.security.user.name=userspring.security.user.password=123456

    然后访问Admin管理页面

    image.png

    输入账 密码后可以进入,但是没有监控的应用了

    image.png

    原因是被监控的服务要连接到Admin服务端也是需要认证的

    image.png

    我们在客户端配置连接的账 密码即可

    image.png

    重启后访问Admin服务管理页面

    image.png

    搞定

    5.注册中心

    实际开发的时候我们可以需要涉及到的应用非常多,我们也都会把服务注册到注册中心中,比如nacos,Eureka等,接下来我们看看如何通过注册中心来集成客户端。就不需要每个客户端来集成了。

    image.png

    变为下面的场景

    image.png

    那么我们需要先启动一个注册中心服务,我们以Nacos为例

    image.png

    然后访问下Nacos服务

    image.png

    暂时还没有服务注册,这时我们可以注册几个服务,用我之前写过的案例来演示。

    image.png

    每个服务处理需要添加Nacos的注册中心配置外,我们还需要添加Actuator的配置

    image.png

    image.png

    然后启动相关的服务,可以看到相关的服务

    声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

    上一篇 2022年2月1日
    下一篇 2022年2月1日

    相关推荐