栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBootAdmin监控

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBootAdmin监控

Spring Boot Admin 是一个开源的社区项目,用于管理和监控 Spring Boot 应用程序。应用程序可以通过 http 的方式,或 Spring Cloud 服务发现机制注册到 SBA 中,然后就可以实现对 Spring Boot 项目的可视化管理和查看了 ​
Spring Boot Admin 可以监控 Spring Boot 单机或集群项目,它提供详细的健康 (Health)信息、内存信息、JVM 系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、Spring Boot 缓存查看和管理等功能。接下来我们一起来搭建和使用吧
搭建Admin服务[SpringBoot2.7.0]
  • pom.xml 配置文件

		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
		
		
			de.codecentric
			spring-boot-admin-starter-server 
			2.7.0
		
		
			org.springframework.boot
			spring-boot-starter-actuator 
		
		
			org.springframework.boot
			spring-boot-starter-security 
		
		
			org.springframework.boot
			spring-boot-starter-mail 
		



	
		
			org.springframework.boot
			spring-boot-maven-plugin
			2.7.0
		
	

  • application.yml 配置文件
spring:
  profiles:
    active: dev
  • application-dev.yml 配置文件
server:
  address: 127.0.0.1
  port: 9999
  servlet:
    context-path: /springBootAdmin
spring:
  application:
    name: springBootAdmin
  security:
    user:
      name: admin
      password: admin
  mail:
    protocol: smtp
    host: smtp.163.com
    username: lixing_xcx@163.com
    password: JFURNCZSERNXYCVE
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  • SpringBoot应用启动类配置
@SpringBootApplication
@EnableAdminServer // 开启SpringBootAdmin服务
@EnableWebSecurity // 开启SpringSecurity安全认证
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
  • SpringBoot2.7.0+ 配置文件
package com.example.demo.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SecurityConfig {

    //    @Bean
    //    UserDetailsService userDetailsService() {
    //        InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();
    //        // TODO 创建默认用户
    //        users.createUser(User.withUsername("admin").password("admin").roles("admin").build());
    //        users.createUser(User.withUsername("lixing").password("lixing").roles("admin").build());
    //        return users;
    //    }

//    @Bean
//    WebSecurityCustomizer webSecurityCustomizer() {
//        return new WebSecurityCustomizer() {
//            @Override
//            public void customize(WebSecurity web) {
//                // TODO 设置 /demo/noAuth 匿名登录,不经过 Spring Security 过滤器链
//                web.ignoring().antMatchers("/demo/noAuth");
//            }
//        };
//    }

    
//    @Bean
//    SecurityFilterChain securityFilterChain() {
//        List filters = new ArrayList<>();
//        return new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**"), filters);
//    }

    private final String adminContextPath;
    public SecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // 静态资源和登录页面可以不用认证
        http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                // 其他请求必须认证
                .anyRequest().authenticated()
                // 自定义登录和退出
                .and().formLogin().loginPage(adminContextPath + "/login")
                .and().logout().logoutUrl(adminContextPath + "/logout")
                // 启用HTTP-Basic, 用于Spring Boot Admin Client注册
                .and().httpBasic().and().csrf().disable();
        return http.build();
    }

}

  • SpringBoot2.7.0- 配置文件
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable();
    }
}
搭建Client服务[SpringBoot2.7.0]
  • pom.xml 配置文件

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

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			de.codecentric
			spring-boot-admin-starter-client 
			2.7.0
		
		
			org.springframework.boot
			spring-boot-starter-actuator 
		



	
		
			org.springframework.boot
			spring-boot-maven-plugin
			2.7.0
		
	

  • application.yml 配置文件
spring:
  profiles:
    active: dev
  • application-dev.yml 配置文件
server:
  address: 127.0.0.1
  port: 9998
  servlet:
    context-path: /springBootClient
# spring Boot Admin 服务端地址
spring:
  application:
    name: springBootClient
  boot:
    admin:
      client:
        url: http://127.0.0.1:9999/springBootAdmin
        username: admin
        password: admin
        instance:
          service-url: http://127.0.0.1:9998/springBootClient
          metadata:
            user.name: admin
            user.password: admin
            
# TODO SpringBootAdmin资源监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
    logfile:
      enabled: on
登录SpringBootAdmin控制台




配置动态日志
  • dev 日志配置
custom:
  log:
    level: debug
    path: D:logs
debug: on
logging:
  config: classpath:logback-spring.xml
  level:
    root: debug
    #com.example.demo.demo1: warn
    #com.example.demo.demo2: error
  file:
    name: D:logsinfo.log
  • fat 日志配置
custom:
  log:
    level: info
    path: D:logs
debug: off
logging:
  config: classpath:logback-spring.xml
  level:
    root: info
    #com.example.demo.demo1: warn
    #com.example.demo.demo2: error
  file:
    name: D:logsinfo.log
  • logback-spring.xml 配置






    
    
    ${CONTEXT_NAME}
    
    
    

    
    
    
    

    
    
    
    

    
    

    
    

    
    

    
    

    
    
       
        
            
                ${CONSOLE_LOG_PATTERN}
                UTF-8
            
        
        
        
            
        
    

    
    
       
        
            ${logPath}/info.log
            
            
            
            
            
            
                ${FILE_LOG_PATTERN}
                UTF-8
            
            
                ${logPath}/info-%d{yyyy-MM-dd}.%i.log
                ${MAX_FILE_SIZE}
                ${MAX_HISTORY}
            
        

        
        
            ${logPath}/warn.log
            
                WARN
                ACCEPT
                DENY
            
            
                ${FILE_LOG_PATTERN}
                UTF-8
            
            
                ${logPath}/warn-%d{yyyy-MM-dd}.%i.log
                ${MAX_FILE_SIZE}
                ${MAX_HISTORY}
            
        

        
        
            ${logPath}/error.log
            
                ERROR
                ACCEPT
                DENY
            
            
                ${FILE_LOG_PATTERN}
                UTF-8
            
            
                ${logPath}/error-%d{yyyy-MM-dd}.%i.log
                ${MAX_FILE_SIZE}
                ${MAX_HISTORY}
            
        

        
        
            0
            1024
            
        
        
            0
            1024
            
        
        
            0
            1024
            
        

        
            
            
            
        
    


  • 控制台配置info日志

  • 控制台配置warn日志

  • 控制台配置error日志

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号