SpringBoot整合Admin监控平台实现项目监控

简介

我们知道,SpringBoot默认是提供了一个健康检查的配置环境,但是那个监控实在是丑的没边。辣眼睛级别的丑。还好,有民间大佬写了一款spring boot admin监控平台。
先看看最后的效果图。
在这里插入图片描述
在spring boot admin中,分为 admin-client 端和 adin-server 端。通常就是理解为项目和注册中心。我们需要做的就是,将我们的项目的端点部署在注册中心上,让注册中心能监控到我们的项目心跳。

配置Admin-client端

其实在client依赖中,内部已经内置了acturactor依赖,也就是我们常说的健康检查。
在这里插入图片描述

Admin端就是我们的项目。当我们写好一个项目后,只需要引入下面的依赖,就能开启一个admin管理。

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

然后在properties中写上我们的配置信息:

# admin 监控平台配置信息
# 远程服务端的ip
spring.boot.admin.client.url=http://localhost:12000
# 展示全部的细节信息
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
# 允许admin工程远程停止本服务
management.endpoint.shutdown.enabled=true
# 使用真实IP进行注册
spring.boot.admin.client.instance.prefer-ip=true
# admin工程的账号密码
spring.boot.admin.client.username=zxy
spring.boot.admin.client.password=123456

特别注意:最后两行代码配置的前提是你的server端引入了spring-security的前提下才配置。

接着我们配置spring-admin-server端。

配置Admin-server端

这个端,你就可以理解为一个注册中心,注册中心里面被注册的就是我们一个个的client。所以,这个应该是一个单独的项目。你可以新建一个空白的springboot项目,引入web依赖即可。
项目建好之后我们需要配置三个地方:

引入spring-security和spring-admin-server依赖
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
在主启动类上开启Server服务以及编写Security配置类
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
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;

@SpringBootApplication
@EnableAdminServer
public class AdminApplication {

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

    //配置安全校验
    @Configuration
    @Order(Ordered.LOWEST_PRECEDENCE)
    public static 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();
        }
    }
}

可以看到,在类上多了一个@EnableAdminServer的注解。

配置端口以及Security账号密码

还记得我们的client里面配置的端口吗?没错,那个端口就是我们这个server的端口号,所以一定要保持一致。在properties中编写如下配置:

server.port=12000

spring.application.name=枪锋
#配置admin工程登录的账号密码
spring.security.user.name=zxy
spring.security.user.password=123456

以上就是server端的全部配置。

测试

接下来我们分别启动client端和server端。开启没有先后顺序,但是还是建议你先开启client,因为server开启之后没找到client会报错。

开启之后,在浏览器输入你刚才配置的ip+端口:例如我的就是http://localhost:12000。会出现下面的界面:
在这里插入图片描述

这个就是你配置了security之后,在properties中配置的账号和密码;
登录之后出现下列界面:
在这里插入图片描述
这个页面展示了你的项目地址信息;我们点进去
在这里插入图片描述
左侧就是我们全部的可执行的操作,右边则展示了我们的项目心跳信息。

至此全部配置完成。

ps:这些配置信息并不是全部的配置信息,如果想深入这个,请查阅spring boot官网,参考actuator的全部配置,链接:spring boot actuator

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鱼小洲

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

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

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

打赏作者

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

抵扣说明:

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

余额充值