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

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

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. 启动类添加注解:

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 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免校验:

                .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 {@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.mrgr.cn/news/29279.html

相关文章:

  • JVM内存学习
  • 单指标RSRS沪深300择时:​年化13.7%,最大回撤-16​%(附代码与策略下载)
  • 「iOS」push与present
  • 智能生成ppt使用什么软件?这些AI应用不容错过
  • html详细知识
  • JMeter 中使用 Gson 操作请求中的Boby参数
  • 【mechine learning-11-梯度下降的数学公式推导】
  • 直流斩波电路
  • Selenium with Python学习笔记整理(网课+网站)
  • 0基础跟德姆(dom)一起学AI 数据处理和统计分析03-Numpy环境搭建
  • 换脸动漫转真人:ComfyUI无实物Cosplay
  • 养殖场中的分布式光伏发电
  • iptables限制网速
  • Android AlertDialog圆角背景不生效的问题
  • 昆明网站制作流程与设计技巧
  • 【C++】日期类基础题
  • 【C++】探秘二叉搜索树
  • C++速通LeetCode中等第3题-字母异位词分组
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • 交流电力控制电路之交流调压电路