欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 添加admin监控的方法

程序员文章站 2022-06-29 13:03:45
一、spring boot  admin简介 spring boot admin github开源地址: 它主要的作用是在spring boot actu...

一、spring boot  admin简介

spring boot admin github开源地址:

它主要的作用是在spring boot actuator的基础上提供简洁的web ui展示。

二、项目使用:

1、搭建一个maven web项目

2、pom依赖配置

<dependency> 
  <groupid>org.springframework.boot</groupid> 
  <artifactid>spring-boot-starter-web</artifactid> 
</dependency> 
<dependency> 
  <groupid>org.springframework.boot</groupid> 
  <artifactid>spring-boot-starter-security</artifactid> 
</dependency> 
<dependency> 
  <groupid>de.codecentric</groupid> 
  <artifactid>spring-boot-admin-starter-client</artifactid> 
</dependency> 
<dependency> 
  <groupid>de.codecentric</groupid> 
  <artifactid>spring-boot-admin-server</artifactid> 
</dependency> 
<dependency> 
  <groupid>de.codecentric</groupid> 
  <artifactid>spring-boot-admin-server-ui</artifactid> 
</dependency> 
<dependency> 
  <groupid>de.codecentric</groupid> 
  <artifactid>spring-boot-admin-server-ui-login</artifactid> 
</dependency> 

在pom.xml中添加上以上配置

admin服务端:spring-boot-admin-server、spring-boot-admin-server-ui

admin客户端:spring-boot-admin-starter-client  (加上该项能监控服务端自身的运行状态,其他项目只需要引入client就可以引入监控)

安全:spring-boot-starter-security

登录验证:spring-boot-admin-server-ui-login (也可以自行添加简单的登录界面)

3、application.yml

info: 
 app: 
  name: imard 
  version: v1.0.0 
[html] view plain copy
logging: 
 file: "d:/logs/imard/boot.log" 
management: 
 context-path: "/actuator" 
spring: 
 application: 
  name: "@pom.artifactid@" 
 boot: 
  admin: 
   url: http://www.test.com:8080 
 profiles: 
  active: 
   - secure 
--- 
spring: 
 profiles: insecure 
management: 
 security: 
  enabled: false 
security: 
 basic: 
  enabled: false 
--- 
spring: 
 profiles: secure 
 boot: 
  admin: 
   username: "${security.user.name}" 
   password: "${security.user.password}" 
   client: 
    metadata: 
     user.name: "${security.user.name}" 
     user.password: "${security.user.password}" 
 
security: 
 user: 
  name: user 
  password: pass 

其中:spring.boot.admin.url声明admin服务端地址(其他项目会通过这个url主动的注册到admin监控中)
            info配置app的基本信息

              在本机hosts中做了映射

4、application.java

@configuration 
@enableautoconfiguration 
@enableadminserver 
public class application extends springbootservletinitializer { 
  @override 
  protected springapplicationbuilder configure(springapplicationbuilder application) { 
    return application.sources(application.class); 
  } 
 
  public static void main(string[] args) { 
    springapplication.run(application.class, args); 
  } 
} 

@enableadminserver 添加上该注解启动监控

5、securityconfig

@profile("secure") 
@configuration 
public class securityconfig extends websecurityconfigureradapter { 
  @override 
  protected void configure(httpsecurity http) throws exception { 
    http.formlogin().loginpage("/login.html").loginprocessingurl("/login").permitall(); 
    http.logout().logouturl("/logout"); 
    http.csrf().disable(); 
    http.authorizerequests() 
      .antmatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitall(); 
    http.authorizerequests().antmatchers("/api/**").permitall().antmatchers("/**") 
      .authenticated(); 
    // enable so that the clients can authenticate via http basic for registering 
    http.httpbasic(); 
  } 
} 

使用spring security配置一个基本的安全策略

6、监管管理

配置完1~5个步骤以后,使用application启动监控程序。

通过监控登录界面进行安全验证后,如下图:

spring boot 添加admin监控的方法

进入details就可以看到具体的项目监控信息(details、log、metrics、environment、logging、jmx、threads、audit、trace、heapdump)

总结

以上所述是小编给大家介绍的spring boot 添加admin监控的方法,希望对大家有所帮助