美文网首页
SpringBoot整合SpringBootAdmin监控

SpringBoot整合SpringBootAdmin监控

作者: 轻轻敲醒沉睡的心灵 | 来源:发表于2021-06-11 12:06 被阅读0次

    SpringBootAdmin分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用http通讯方式实现数据交互。服务端server需要单独启动一个服务,客户端client需要集成到各个微服务中。

    1. 服务端server搭建

    1.1 新建springbootadmin服务端项目,导包

    我使用的springboot是2.3.11,但是spring-boot-admin-starter-server好像只有2.3.1

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
            
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>
            
    <!--springboot admin 安全登录相关-->
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    

    1.2 yml配置文件

    # 1.server
    server:
      port: 10086
      servlet:
        context-path: /admin
    # 2.log
    logging:
      config: classpath:logback-spring.xml
      
    # 配置一个账号和密码
    spring:
      security:
        user:
          name: test
          password: test
    

    1.3 安全配置类

    import org.springframework.context.annotation.Configuration;
    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 de.codecentric.boot.admin.server.config.AdminServerProperties;
    
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        private String adminContextPath;
    
        public SecurityConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }
        
        
        protected void configure(HttpSecurity http) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");
            http.authorizeRequests()
                    // 1.配置所有静态资源和登录页可以公开访问
                    .antMatchers(adminContextPath + "/assets/**").permitAll().antMatchers(adminContextPath + "/login")
                    .permitAll().anyRequest().authenticated().and()
                    // 2.配置登录和登出路径
                    .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
                    .logoutUrl(adminContextPath + "/logout").and()
                    // 3.开启http basic支持,admin-client注册时需要使用
                    .httpBasic().and().csrf()
                    // 4.开启基于cookie的csrf保护
                    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                    // 5.忽略这些路径的csrf保护以便admin-client注册
                    .ignoringAntMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
        }
    }
    

    1.4 启动后,可以登录了

    登录界面

    2. 客户端集成client

    2.1 导包

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

    2.2 yml配置

    #开放端点用于SpringBoot Admin的监控
    management:
      endpoints:
        web:
          exposure:
            include: '*' # 暴露所有端点
      endpoint:
        health:
          show-components: always # 端点监控检查
    
    # 2.log
    logging: 
      # 日志文件的名字,SpringBootAdmin使用
      file:
        name: logs/client1.log
    # 3.spring
    spring: 
      application:
        name: testclient1
      # SpringBootAdmin
      boot:
        admin:
          client:
            url:
            - http://192.168.6.150:10086/admin
            # 配置 admin-server的账号和密码
            username: test
            password: test
    

    2.3 启动客户端

    客户端启动后,就会在 服务里面看到,

    客户端
    点进去,就可以看到监控信息了
    监控信息
    日志界面
    注意,这里日志是客户端日志文件里面的内容,要想sql能写入到日志文件,需要注意2点:
      1. 客户端DAO层,级别设成debug


        DAO层日志级别
      1. mybatis配置
        mybatis
        log-impl配置要关闭,开启后只在控制台打印,日志文件中没有

    相关文章

      网友评论

          本文标题:SpringBoot整合SpringBootAdmin监控

          本文链接:https://www.haomeiwen.com/subject/hhgmeltx.html