news 2026/4/23 10:35:29

Spring Boot Admin Server服务搭建及配置

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Boot Admin Server服务搭建及配置

1. 概述

Spring Boot Admin 是一个用于管理和监控 Spring Boot 应用的 Web 应用。每个应用作为客户端注册到 Admin 服务器,底层通过 Spring Boot Actuator 接口实现监控功能。

本文将详细介绍如何配置 Spring Boot Admin 服务器,以及如何将应用注册为客户端。

2. Admin 服务搭建

首先创建一个基础的 Spring Boot Web 应用,并添加以下 Maven 依赖:

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>3.1.5</version></dependency>

添加依赖后,@EnableAdminServer注解就可用。在主类添加该注解:

@EnableAdminServer@SpringBootApplicationpublicclassSpringBootAdminServerApplication(exclude=AdminServerHazelcastAutoConfiguration.class){publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootAdminServerApplication.class,args);}}

至此,Admin 服务器已准备就绪,可以启动并注册客户端应用。

3. 客户端配置

现在配置第一个 Spring Boot 应用作为客户端。添加以下 Maven 依赖:

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

接下来配置客户端指向 Admin 服务器的地址:

spring.boot.admin.client.url=http://localhost:8080

⚠️踩坑提醒:Spring Boot 2 默认只暴露healthinfo接口,其他接口需手动开启:

management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

4. 安全配置

Admin 服务器能访问应用的敏感接口,强烈建议为 Admin 和客户端添加安全配置

4.1 Admin 服务器安全配置

添加以下 Maven 依赖:

<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui</artifactId><version>1.5.7</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId><version>3.1.5</version></dependency>

创建安全配置类:

@Configuration@EnableWebSecuritypublicclassWebSecurityConfig{privatefinalAdminServerPropertiesadminServer;publicWebSecurityConfig(AdminServerPropertiesadminServer){this.adminServer=adminServer;}@BeanpublicSecurityFilterChainfilterChain(HttpSecurityhttp)throwsException{SavedRequestAwareAuthenticationSuccessHandlersuccessHandler=newSavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(this.adminServer.getContextPath()+"/");http.authorizeHttpRequests(req->req.requestMatchers(this.adminServer.getContextPath()+"/assets/**").permitAll().requestMatchers(this.adminServer.getContextPath()+"/login").permitAll().anyRequest().authenticated()).formLogin(formLogin->formLogin.loginPage(this.adminServer.getContextPath()+"/login").successHandler(successHandler)).logout((logout)->logout.logoutUrl(this.adminServer.getContextPath()+"/logout")).httpBasic(Customizer.withDefaults()).csrf(csrf->csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(newAntPathRequestMatcher(this.adminServer.getContextPath()+"/instances",HttpMethod.POST.toString()),newAntPathRequestMatcher(this.adminServer.getContextPath()+"/instances/*",HttpMethod.DELETE.toString()),newAntPathRequestMatcher(this.adminServer.getContextPath()+"/actuator/**"))).rememberMe(rememberMe->rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));returnhttp.build();}}

4.2 客户端安全配置

添加安全配置后,客户端将无法注册到服务器。需在客户端配置中添加认证信息:

spring.boot.admin.client.username=admin spring.boot.admin.client.password=admin

如果客户端本身也开启了安全保护,需发送元数据供 Admin 服务器访问:

spring.security.user.name=client spring.security.user.password=client spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name} spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}

生产建议:凭据通过 HTTP 传输不安全,务必使用 HTTPS。

5. 监控与管理功能

Spring Boot Admin 支持自定义显示的监控指标:

spring.boot.admin.routes.endpoints=env, metrics, trace, jolokia, info, configprops

其他实用功能包括:

  • 通过 Jolokia 管理 JMX Bean
  • 日志级别管理

5.1 集群支持(Hazelcast)

添加 Hazelcast 依赖即可启用集群:

<dependency><groupId>com.hazelcast</groupId><artifactId>hazelcast</artifactId><version>4.0.3</version></dependency>

持久化配置示例:

@ConfigurationpublicclassHazelcastConfig{@BeanpublicConfighazelcast(){MapConfigeventStoreMap=newMapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1).setEvictionConfig(newEvictionConfig().setEvictionPolicy(EvictionPolicy.NONE)).setMergePolicyConfig(newMergePolicyConfig(PutIfAbsentMergePolicy.class.getName(),100));MapConfigsentNotificationsMap=newMapConfig("spring-boot-admin-application-store").setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1).setEvictionConfig(newEvictionConfig().setEvictionPolicy(EvictionPolicy.LRU)).setMergePolicyConfig(newMergePolicyConfig(PutIfAbsentMergePolicy.class.getName(),100));Configconfig=newConfig();config.addMapConfig(eventStoreMap);config.addMapConfig(sentNotificationsMap);config.setProperty("hazelcast.jmx","true");config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);TcpIpConfigtcpIpConfig=config.getNetworkConfig().getJoin().getTcpIpConfig();tcpIpConfig.setEnabled(true);tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"));returnconfig;}}

6. 通知机制

当客户端状态变化时,Admin 服务器支持多种通知方式:

  • 邮件
  • PagerDuty
  • OpsGenie
  • Hipchat
  • Slack
  • Let’s Chat

6.1 邮件通知

添加邮件依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.4.0</version></dependency>

配置邮件参数:

spring.mail.host=smtp.example.com spring.mail.username=smtp_user spring.mail.password=smtp_password [email protected]

6.2 Hipchat 通知

配置 Hipchat 集成:

spring.boot.admin.notify.hipchat.auth-token=<generated_token> spring.boot.admin.notify.hipchat.room-id=<room-id> spring.boot.admin.notify.hipchat.url=https://yourcompany.hipchat.com/v2/

6.3 自定义通知配置

通过组合通知器实现复杂逻辑:

@ConfigurationpublicclassNotifierConfiguration{privatefinalInstanceRepositoryrepository;privatefinalObjectProvider<List<Notifier>>otherNotifiers;publicNotifierConfiguration(InstanceRepositoryrepository,ObjectProvider<List<Notifier>>otherNotifiers){this.repository=repository;this.otherNotifiers=otherNotifiers;}@BeanpublicFilteringNotifierfilteringNotifier(){CompositeNotifierdelegate=newCompositeNotifier(this.otherNotifiers.getIfAvailable(Collections::emptyList));returnnewFilteringNotifier(delegate,this.repository);}@BeanpublicLoggingNotifiernotifier(){returnnewLoggingNotifier(repository);}@Primary@Bean(initMethod="start",destroyMethod="stop")publicRemindingNotifierremindingNotifier(){RemindingNotifierremindingNotifier=newRemindingNotifier(filteringNotifier(),repository);remindingNotifier.setReminderPeriod(Duration.ofMinutes(5));remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(60));returnremindingNotifier;}}

7. 总结

本文介绍了使用 Spring Boot Admin 监控管理 Spring Boot 应用的关键步骤。通过自动配置,只需少量代码即可搭建功能完整的监控服务器。

完整示例代码可在 GitHub 获取。


Spring Boot Admin 使用指南 | Baeldung中文网

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 14:10:04

【课程设计/毕业设计】基于ssm的体育器材管理系统设计与实现高校体育器材管理系统的设计与实现【附源码、数据库、万字文档】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/23 11:20:40

java毕设选题推荐:基于java的短剧推荐系统设计与实现基于Java+SSM的短剧推荐系统设计与实现【附源码、mysql、文档、调试+代码讲解+全bao等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/23 11:20:27

看完就会:8个一键生成论文工具测评,专科生毕业论文轻松搞定!

对于专科生而言&#xff0c;毕业论文写作不仅是学业的终点&#xff0c;更是能力的考验。然而&#xff0c;面对选题困难、资料查找繁琐、格式规范复杂等问题&#xff0c;许多同学感到无从下手。为了帮助大家更高效地完成论文&#xff0c;我们基于2026年的实测数据与用户反馈&…

作者头像 李华