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

devtools/2024/11/14 15:00:49/

服务端:

1. 新建monitor服务 pom依赖

java">    <!-- 注意这些只是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: 8000spring:application:name: springboot-adminsecurity:user:name: adminpassword: 123456boot:admin:ui:title: test-服务监控中心 #自定义服务端名称context-path: /
management:endpoint:health:show-details: alwayslogging:file:# 服务端日志文件夹位置path: ./logs/springboot-admin

3.security config配置:

java">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();}@Beanpublic 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. 启动类添加注解:

java">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配置:

java">        <!-- 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配置:

java">spring:#spring boot adminboot:admin:client:api-path: instancesurl: http://127.0.0.1:8000instance:prefer-ip: true # 使用ip注册进来username: adminpassword: 123456management:endpoint:logfile:# 你的客户端日志文件地址external-file: ./logs/client.logenabled: truehealth:show-details: alwaysendpoints:enabled-by-default: trueweb:base-path: /actuatorexposure:include: "*"

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

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

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

java">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 {@Beanpublic BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {return new BeanPostProcessor() {@Overridepublic 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.ppmy.cn/devtools/114953.html

相关文章

HarmonyOS开发实战( Beta5.0)橡皮擦案例实践详解

鸿蒙HarmonyOS开发往期必看&#xff1a; HarmonyOS NEXT应用开发性能实践总结 最新版&#xff01;“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线&#xff01;&#xff08;从零基础入门到精通&#xff09; 介绍 本示例通过ohos.graphics.drawing库和blendMode颜色混合实…

小阿轩yx-通过state模块定义主机状态

小阿轩yx-通过state模块定义主机状态 前言 前面学习了远程执行模块&#xff0c;这些模块的执行类似语段 shell 脚本&#xff0c;每次执行都会触发一次相同的功能&#xff0c;在大量的 minion 上运行远程命令当然是重要的&#xff0c;但是对于 minion 的环境控制&#xff0c;使…

【Linux庖丁解牛】—Linux基本指令(上)!

&#x1f308;个人主页&#xff1a;秋风起&#xff0c;再归来~&#x1f525;系列专栏&#xff1a; Linux庖丁解牛 &#x1f516;克心守己&#xff0c;律己则安 目录 1、 pwd命令 2、ls 指令 3、cd 指令 4、Linux下的根目录 5、touch指令 6、 stat指令 7、mkdi…

Linux系统中的进程调度队列

目录 一、进程调度队列结构 二、活动队列与过期队列 1.queue[140] 2.bitmap[5] 一、进程调度队列结构 Linux系统中&#xff0c;每一个CPU都有一个进程调度队列runqueue&#xff0c;如图所示 二、活动队列与过期队列 运行队列runqueue中有两个指针*active、*expired。*acti…

堆的向下调整算法和TOPK问题

目录 1.什么是堆&#xff1f; 1.1 向下调整建堆的时间复杂度计算 1.2 堆的结构体设计 2.堆的功能实现&#xff1a; 2.1 堆的插入&#xff1a; 2.2 堆的删除&#xff1a; 2.3 堆排序&#xff1a; 2.4 向下调整建堆&#xff1a; 2.5 TOPK问题&#xff1a; 2.6 向上调整算…

浅析OceanBase数据库的向量化执行引擎

本篇博客是偏数据库系统概念性的内容&#xff0c;不会深入到 OceanBase 中各个算子和表达式的在向量化中的详细设计和实现。 背景 为了提升OceanBase社区版用户解决问题的效率&#xff0c;OceanBase官方不久前推出了《OceanBase 从入门到实践》系列课程。在第七期直播课程后&a…

visual studio2015安装番茄助手

VS2015安装使用番茄助手Visual Assist_vs2015番茄助手-CSDN博客 【VS和番茄助手的安装步骤】https://www.bilibili.com/video/BV1K24y1n7Xk?vd_sourcefad0750b8c666dbeaf016e547f99a602 【番茄助手VS2019安装】https://www.bilibili.com/video/BV13p4y1v7bG?vd_sourcefad0…

在Flask中实现日志记录

在Flask中实现日志记录是一个关键的功能&#xff0c;它有助于监控应用的运行情况、调试问题以及记录重要的运行信息。以下是在Flask中实现日志记录的详细步骤和最佳实践&#xff1a; 一、使用Python内置的logging模块 Flask应用通常会使用Python的logging模块来进行日志记录。…