spring-boot-admin的介绍和使用

概述

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

主要的功能点有:

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

搭建服务流程说明

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

创建Spring Boot Admin项目


版本说明:版本建议: Spring Boot 2.x=Spring Boot Admin 2.x  (比如Spring Boot 2.3.x 可以用Spring Boot Admin 2.3.x)

创建一个 Spring Boot 项目,用于展示各个服务中的监控信息,加上 Spring Boot Admin 的依赖,具体代码如下所示:

pom 依赖



        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            de.codecentric
            spring-boot-admin-starter-server
            2.5.6
        

        
        
            de.codecentric
            spring-boot-admin-server-ui
            2.5.6
        

        
        
            org.springframework.boot
            spring-boot-starter-security
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
启动类

添加 @EnableAdminServer

@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminApplication {

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

}
yml 配置

在属性文件中增加端口配置信息:

server:
  port: 8082
  servlet:
    context-path:
spring:
  application:
    name: springboot-admin-test
  security:
    user:
      name: "admin"
      password: "admin"
management:
  endpoint:
    health:
      show-details: always

启动程序,访问 Web 地址 http://127.0.0.1:8082/ 就可以看到主页面了,这个时候是没有数据的,如图 所示

spring-boot-admin的介绍和使用_第1张图片

客户端服务

流程

创建 amdin-order 服务,将服务注册到 admin-server 中

pom 依赖

        
            de.codecentric
            spring-boot-admin-starter-client
            2.4.0
        

        
            org.springframework.boot
            spring-boot-starter-actuator
            
            
                
                    ch.qos.logback
                    logback-classic
                
                
                    org.apache.logging.log4j
                    log4j-to-slf4j
                
            
        
yml 配置
spring:
  application:
    ## 注册服务名
    name: spring-boot-admin-test
  ## spring boot admin
  boot:
    admin:
      client:
        api-path:
        url: http://127.0.0.1:8082
        instance:
          prefer-ip: true # 使用ip注册进来
 
#endpoints config
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    enabled-by-default: true
    web:
      base-path: /actuator
      exposure:
        include: '*'
启动 admin-order 服务
@SpringBootApplication
public class AdminOrderApp {
 
    public static void main(String[] args) {
        SpringApplication.run(AdminOrderApp.class,args);
    }
 
}

重新刷新 admin 平台,admin-order 服务就可以监控

spring-boot-admin的介绍和使用_第2张图片

  • 绿色:健康
  • 灰色:连接客户端健康信息超时(超过10s)
  • 红色:就能看到具体异常信息 

Spring Boot Admin 监控平台

Insighs 信息

自定义的 Info 信息、健康状态、元数据,如图

spring-boot-admin的介绍和使用_第3张图片

Endpoint 端点接口信息

spring-boot-admin的介绍和使用_第4张图片

JVM 信息

spring-boot-admin的介绍和使用_第5张图片

Admin 中查看各个服务的日志 

客户端需要把日志同步ADMI服务中,通过JMX,客户端配置如下

添加 logback-spring.xml 配置



	
	
	
	
	
	
	
	
	
		
			${CONSOLE_LOG_PATTERN}
		
	
 
	
	
		${log.path}/debug.log
		
			${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz
			50MB
			30
		
		
			%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
		
	
 
	
	
		${log.path}/error.log
		
			${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz
			50MB
			30
		
		
			%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
		
		
			ERROR
		
	
 
	
		
	
 
 
	
	
		
		
	

yml 配置

management:
  endpoints:
    web:
      exposure:
        include: '*'
    enabled-by-default: true
  endpoint:
    health:
      show-details: ALWAYS
    # 日志记录
    logfile:
      external-file: D:/project/springcould-alibaba-example/logs/admin-order/debug.log
Spring Boot Admin 查询日志

重新打 Admin 监控平台,点击 admin-order 服务查看日志,如下

spring-boot-admin的介绍和使用_第6张图片

 日志文件等级配置

spring-boot-admin的介绍和使用_第7张图片

Admin 中 SpringSecurity

pom 依赖
 
        
            org.springframework.boot
            spring-boot-starter-security
        
SecuritySecureConfig
 
@Configuration(proxyBeanMethods = false)
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();
    }
}
yml 配置
spring:
  security:
    user:
      name: "admin"
      password: "admin"

从新启动 admin-server 服务

spring-boot-admin的介绍和使用_第8张图片

客户端注册 admin 服务需要配置 username 和 password 

spring:
  application:
    ## 注册服务名
    name: spring-boot-admin-test
  ## spring boot admin
  boot:
    admin:
      client:
        api-path:
        url: http://127.0.0.1:8082
        instance:
          prefer-ip: true # 使用ip注册进来
        username: admin
        password: admin

你可能感兴趣的:(java,数据库,开发语言)