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

news2024/9/20 18:49:55

服务端:

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.coloradmin.cn/o/2145418.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32 芯片启动过程

目录 一、前言二、STM32 的启动模式三、STM32 启动文件分析1、栈 Stack2、堆 Heap3、中断向量表 Vectors3.1 中断响应流程 4、复位程序 Reset_Handler5、中断服务函数6、用户堆栈初始化 四、STM32 启动流程分析1、初始化 SP、PC 及中断向量表2、设置系统时钟3、初始化堆栈并进入…

【Linux】POSIX信号量与、基于环形队列实现的生产者消费者模型

目录 一、POSIX信号量概述 信号量的基本概念 信号量在临界区的作用 与互斥锁的比较 信号量的原理 信号量的优势 二、信号量的操作 1、初始化信号量&#xff1a;sem_init 2、信号量申请&#xff08;P操作&#xff09;&#xff1a;sem_wait 3、信号量的释放&#xff08…

树——数据结构

这次我来给大家讲解一下数据结构中的树 1. 树的概念 树是一种非线性的数据结构&#xff0c;它是由n(n>0&#xff09;个有限结点组成一个具有层次关系的集合。 叫做树的原因&#xff1a;看起来像一棵倒挂的树&#xff0c;根朝上&#xff0c;叶朝下。 特殊结点&#xff1a…

Vim编辑器常用命令

目录 一、命令模式快捷键 二、编辑/输入模式快捷键 三、编辑模式切换到命令模式 四、搜索命令 一、命令模式快捷键 二、编辑/输入模式快捷键 三、编辑模式切换到命令模式 四、搜索命令

深圳铨顺宏科技展邀您体验前沿人工智能技术

我们诚挚地邀请您参加即将举行的展会&#xff0c;探索RFID技术在资产与人员管理中的广泛应用。这些展会将为您提供一个深入了解前沿技术和创新解决方案的机会。 东莞台湾名品博览会&#xff08;东莞台博会&#xff09;展会时间&#xff1a;9月5日至8日。此次展会展示了来自台湾…

路由器全局配置DHCP实验简述

一、路由器配置 reset saved-configuration Warning: The action will delete the saved configuration in the device. The configuration will be erased to reconfigure. Continue? [Y/N]:y Warning: Now clearing the configuration in the device. Info: Succeeded in c…

如何配置 Apache 反向代理服务器 ?

将 Apache 配置为反向代理意味着将 Apache 设置为侦听和引导 web 流量到后端服务器或服务。这有助于管理和平衡服务器上的负载&#xff0c;提高安全性&#xff0c;并使您的 web 服务更高效。您还可以将其设置为监听标准 HTTP 和 HTTPS 端口上的请求&#xff0c;并将其重定向到运…

基于Leaflet和天地图的直箭头标绘实战-源码分析

目录 前言 一、Leaflet的特种标绘库 1、特种标绘对象的定义 2、Plot基类定义 3、直线箭头的设计与实现 二、在天地图中进行对象绘制 1、引入天地图资源 2、标绘对象的调用时序 3、实际调用过程 三、总结 前言 在博客中介绍过geoman标绘的具体实现&#xff0c;使用Leaf…

Linux驱动开发 ——架构体系

只读存储器&#xff08;ROM&#xff09; 1.作用 这是一种非易失性存储器&#xff0c;用于永久存储数据和程序。与随机存取存储器&#xff08;RAM&#xff09;不同&#xff0c;ROM中的数据在断电后不会丢失&#xff0c;通常用于存储固件和系统启动程序。它的内容在制造时或通过…

教师薪酬管理系统的设计与实现

摘 要 传统信息的管理大部分依赖于管理人员的手工登记与管理&#xff0c;然而&#xff0c;随着近些年信息技术的迅猛发展&#xff0c;让许多比较老套的信息管理模式进行了更新迭代&#xff0c;老师信息因为其管理内容繁杂&#xff0c;管理数量繁多导致手工进行处理不能满足广…

【专题】2024中国生物医药出海现状与趋势蓝皮书报告合集PDF分享(附原数据表)

原文链接&#xff1a;https://tecdat.cn/?p37719 出海已成为中国医药产业实现提速扩容的重要途径。目前&#xff0c;中国医药产业发展态势良好&#xff0c;创新能力不断增强&#xff0c;然而也面临着医保政策改革和带量集采带来的压力。政府积极出台多项政策支持医药企业出海…

人工智能 | 基于ChatGPT开发人工智能服务平台

简介 ChatGPT 在刚问世的时候&#xff0c;其产品形态就是一个问答机器人。而基于ChatGPT的能力还可以对其做一些二次开发和拓展。比如模拟面试功能、或者智能机器人功能。 模拟面试功能包括个性化问题生成、实时反馈、多轮面试模拟、面试报告。 智能机器人功能提供24/7客服支…

字节跳动冯佳时:大语言模型在计算机视觉领域的应用、问题和我们的解法

演讲嘉宾&#xff5c;冯佳时 编辑 &#xff5c;蔡芳芳 近年来&#xff0c;大语言模型 (LLMs) 在文本理解与生成领域取得了显著进展。然而&#xff0c;LLMs 在理解和生成自然信号&#xff08;例如图像&#xff0c;视频&#xff09;等&#xff0c;还处在比较早期的探索阶段。为…

muduo - 概要简述

作者&#xff1a;陈硕 编程语言&#xff1a;C 架构模式&#xff1a;Reactor 代码链接&#xff1a;GitHub - chenshuo/muduo: Event-driven network library for multi-threaded Linux server in C11 设计自述&#xff1a;https://www.cnblogs.com/Solstice/archive/2010/08…

MybatisPlus:多条件 or()的使用

default List<ErpProductDO> selectByOE(String oe1, String oe2){return selectList(new LambdaUpdateWrapper<ErpProductDO>().eq(ErpProductDO::getOe,oe1).or().eq(ErpProductDO::getOe,oe2)); } 对应SQL为&#xff1a;

《探索云原生与相关技术》

在当今的科技领域中&#xff0c;云原生&#xff08;Cloud Native&#xff09;已经成为了一个热门的话题。它代表着一种构建和运行应用程序的全新方式。 云原生的概念 云原生是一套技术体系和方法论&#xff0c;旨在充分利用云计算的优势来构建更具弹性、可扩展性和高效性的应…

LeetCode 2332.坐上公交的最晚时间 (双指针 + 贪心)

给你一个下标从 0 开始长度为 n 的整数数组 buses &#xff0c;其中 buses[i] 表示第 i 辆公交车的出发时间。同时给你一个下标从 0 开始长度为 m 的整数数组 passengers &#xff0c;其中 passengers[j] 表示第 j 位乘客的到达时间。所有公交车出发的时间互不相同&#xff0c;…

python 识别省市、区县并组建三级信息数据库

一、网址&#xff1a; 全国行政区划信息查询平台 二、分析并搭建框架 检查网页源码&#xff1a; 检查网页源码可以发现&#xff1a; 所有省级信息全部在javaScript下的json中&#xff0c;会在页面加载时加载json数据&#xff0c;填充到页面的option中。 1、第一步&#xff1a…

1、2、3、4四个数字能组成多少个互不相同且无重复数字的三位数

要求 请编写函数fun&#xff0c;其功能是:找出用1、2、3、4四个数字&#xff0c;能组成多少个互不相同且无重复数字的三位数&#xff0c;然后把这些三位数按从小到大的顺序依次存入相应的数组xxx中&#xff0c;符合条件的个数由函数值返回 解题思路 本题要求求出一个三位数&…

c++基础入门三

文章目录 C基础入门(三)auto关键字auto简介使用细则一、可以和指针联合使用二、在一行定义多个变量 不能使用场景一、不能作为函数的参数二、不能用来声明数组 基于for的循环使用条件 指针空值nullptr C基础入门(三) 回顾上集&#xff0c;我们介绍了C的函数重载&#xff0c;引…