Spring-Boot-Admin(服务监控)

时间:2022-03-28 01:24:30
  1. 版本说明
    SpringBoot3.0 + SpringBootAdmin3.0.Y-->测试发现监控中心服务端无法监控服务端自身服务,安全配
    置存在类和方法找不到的问题,后来将版本改为SpringBoot2.7 + SpringBootAdmin2.7.Y-->问题解决。
    Spring-Boot-Admin(服务监控)
  2. pom.xml
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    	<version>2.7.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-server</artifactId>
    	<version>2.7.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-client</artifactId>
    	<version>2.7.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
    	<groupId>org.projectlombok</groupId>
    	<artifactId>lombok</artifactId>
    	<version>1.18.24</version>
    	<scope>provided</scope>
    </dependency>
  3. application.yml
    server:
      port: 8181
      servlet:
        context-path: /admin-server
    spring:
      application:
        name: admin-server
    
    --- # 监控中心服务端配置
    spring:
      security:
        user:
          name: vvvvvv
          password: 123456
      boot:
        admin:
          ui:
            title: MY服务监控中心
          context-path: /kkkkkk
    
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-server.log
    
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk
      instance:
        service-url: http://127.0.0.1:8181/admin-server
      username: vvvvvv
      password: 123456
    
  4. 补充说明
    1.项目访问地址:http://localhost:8181/admin-server
    server:
      port: 8181
      servlet:
        context-path: /admin-server
    
    2.监控中心该项目的服务名称:admin-server
    spring:
      application:
        name: admin-server
    
    3.监控中心访问地址:http://localhost:8181/admin-server/kkkkkk (用户名:vvvvvv 密码:123456)
    spring:
      security:
        user:
          name: vvvvvv
          password: 123456
      boot:
        admin:
          ui:
            title: MY服务监控中心
          context-path: /kkkkkk
    
    4.将监控中心自身服务添加到监控中心
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-server.log
     
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk # 监控中心访问地址
      instance:
        service-url: http://127.0.0.1:8181/admin-server # 项目访问地址
      username: vvvvvv
      password: 123456
  5. 监控配置
     
    @Configuration
    @EnableAdminServer
    public class AdminServerConfig {
    
        @Lazy
        @Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
        @ConditionalOnMissingBean(Executor.class)
        public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
            return builder.build();
        }
    
    }
  6. 安全配置
    @EnableWebSecurity
    public class SecurityConfig {
    
        private final String adminContextPath;
    
        public SecurityConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }
    
        @Bean
        public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");
    
            return httpSecurity
                    .headers().frameOptions().disable()
                    .and().authorizeRequests()
                    .antMatchers(adminContextPath + "/assets/**"
                        , adminContextPath + "/login"
                        , "/actuator"
                        , "/actuator/**"
                    ).permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().loginPage(adminContextPath + "/login")
                    .successHandler(successHandler).and()
                    .logout().logoutUrl(adminContextPath + "/logout")
                    .and()
                    .httpBasic().and()
                    .csrf()
                    .disable()
                    .build();
        }
    
    }
  7. 监控通知
    被监控的服务上线或者下线将触发监控通知,可在此处发邮件或者短信通知相关运维人员。
    @Slf4j
    @Component
    public class CustomNotifier extends AbstractEventNotifier {
    
        protected CustomNotifier(InstanceRepository repository) {
            super(repository);
        }
    
        @Override
        @SuppressWarnings("all")
        protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
            return Mono.fromRunnable(() -> {
                // 实例状态改变事件
                if (event instanceof InstanceStatusChangedEvent) {
                    String registName = instance.getRegistration().getName();
                    String instanceId = event.getInstance().getValue();
                    String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
                    log.info("Instance Status Change: [{}],[{}],[{}]", registName, instanceId, status);
                }
    
            });
        }
    
    }
  8. 监控中心
    主要的功能点有:显示应用程序的监控状态、应用程序上下线监控、查看 JVM、线程
    信息、可视化的查看日志以及下载日志文件、动态切换日志级别、Http 请求信息跟踪
    Spring-Boot-Admin(服务监控)
    Spring-Boot-Admin(服务监控)
    Spring-Boot-Admin(服务监控)
  9. 添加服务
    1.pom.xml
    <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-client</artifactId>
    	<version>2.7.7</version>
    </dependency>
    
    2.application.yml
    server:
      port: 8282
      servlet:
        context-path: /admin-client
    spring:
      application:
        name: admin-client
    
    --- # Actuator 监控端点的配置项
    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: ALWAYS
        logfile:
          external-file: ./logs/admin-client.log
    
    --- # 监控配置
    spring.boot.admin.client:
      # 增加客户端开关
      enabled: true
      # 设置 Spring Boot Admin Server 地址
      url: http://127.0.0.1:8181/admin-server/kkkkkk
      instance:
        service-url: http://127.0.0.1:8282/admin-client
        name: client
      username: vvvvvv
      password: 123456

    Spring-Boot-Admin(服务监控)
    Spring-Boot-Admin(服务监控)

  10. 项目简介

    用于对 Spring Boot 应用的管理和监控。可以用来监控服务是否健康、是否在线、以及一些jvm数据等等。
    
    Spring Boot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),
    服务端和客户端之间采用 http 通讯方式实现数据交互;单体项目中需要整合 spring-boot-admin-client
    才能让应用被监控。
    
    在 SpringCloud 项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服
    务应用整合 spring-boot-admin-client 就可以实现应用的管理和监控。