当前位置: 首页 > article >正文

spring boot admin集成,springboot2.x集成监控

服务端:

1. 新建monitor服务 pom依赖

    <!-- 注意这些只是pom的核心东西,不是完整的pom.xml内容,不能直接使用,仅供参考使用 -->

        <packaging>jar</packaging>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- spring-boot-admin version使用你自己spring boot 版本,就是spring boot 版本是多少,springboot admin版本也为多少,,注意版本必须相同 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

        <!-- spring security 安全认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.7.5</version>
        </dependency>
    </dependencies>

 2.yml配置:

server:
  port: 8000

spring:
  application:
    name: springboot-admin
  security:
    user:
      name: admin
      password: 123456
  boot:
    admin:
      ui:
        title: test-服务监控中心 #自定义服务端名称
      context-path: /
management:
  endpoint:
    health:
      show-details: always

logging:
  file:
    # 服务端日志文件夹位置
    path: ./logs/springboot-admin

3.security config配置:

package com.test.monitor.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
 
/**
 * admin 监控 安全配置
 *
 * @author test
 */
@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();
    }
 
}

4. 启动类添加注解:

package com.test.monitor;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

客户端:

1. pom配置:

        <!-- spring-boot-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

2. yml配置:

spring:
  #spring boot admin
  boot:
    admin:
      client:
        api-path: instances
        url: http://127.0.0.1:8000
        instance:
          prefer-ip: true # 使用ip注册进来
        username: admin
        password: 123456



management:
  endpoint:
    logfile:
      # 你的客户端日志文件地址
      external-file: ./logs/client.log
      enabled: true
    health:
      show-details: always
  endpoints:
    enabled-by-default: true
    web:
      base-path: /actuator
      exposure:
        include: "*"

3. 客户端spring security添加/actuator免校验:

                .excludePathPatterns("/actuator", "/actuator/**")

4. config 添加配置 修复报错:

package com.test.subsystem.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
 
import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
@Configuration
public class PostProcessorConfig {
 
    @Bean
    public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                }
                return bean;
            }
 
            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
                List<T> copy = mappings.stream()
                        .filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }
 
            @SuppressWarnings("unchecked")
            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    field.setAccessible(true);
                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }
 
}

 5. 日志配置:

#在logback.xml新增此配置,可以打印actuator的HTTP requestmapping信息
    <logger name="org.springframework.boot.actuate.endpoint.web.servlet" level="trace"/>


服务端,客户端启动成功后页面:

 


http://www.kler.cn/news/310082.html

相关文章:

  • .net core8 使用JWT鉴权(附当前源码)
  • Python 之数据库操作(Python Database Operations)
  • Linux(ubuntu)(c语言程序)
  • C++(C++的文件I/O)
  • nanoGPT用红楼梦数据从头训练babyGPT-12.32M实现任意问答
  • Redis 5.0.4 安装教程
  • NFT Insider #147:Sandbox 人物化身九月奖励上线;Catizen 付费用户突破百万
  • PDF转图片的思路思考
  • Leetcode—环形链表||
  • 脚本基本规则
  • C++:日期类的实现
  • java 递归读取前10个匹配的文件所在的全路径
  • 松散绑定是什么?
  • 切换淘宝最新镜像源:优化NPM包管理的极致体验
  • windows C++ 并行编程-异步消息块(一)
  • 【系统架构设计师-2016年真题】案例分析-答案及详解
  • Java从入门到精通学习框架(三)
  • Mybatis+Druid+MybatisPlus多数据源配置
  • 闲鱼网页版开放,爬虫的难度指数级降低。
  • LDD学习启程(TODO)
  • 【React】React18新特性 - startTransition
  • vue-ts-demo
  • 【C-项目】网盘(一期,无限进程版)
  • 什么是数据治理?如何保障数据质量安全
  • 大舍传媒:尼日利亚传统新闻媒体宣传助力新兴行业蓬勃发展
  • 百收SEO蜘蛛池
  • Spring Boot 项目的 pom.xml 中,groupId、artifactId 等信息要如何定义?——定义规则及案例
  • 渗透测试综合靶场 DC-1 通关详解
  • (PySpark)RDD实验实战——求商品销量排行
  • 教师薪酬管理系统的设计与实现