21-07-28
banq
Codecentric 的Spring Boot Admin 项目是对 Actuator免费补充。该工具不属于 Spring,但已在社区中牢固确立地位。
设置 Spring Boot 管理
创建了一个新的 Spring Boot 服务来设置管理 UI。除了对 Spring Web的必需依赖之外,这还会在 POM 中接收一个 spring-boot-admin-starter-server 条目。版本 2.4.1 目前是最新的:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
使用@EnableAdminServer进行注解:
@EnableAdminServer @SpringBootApplication public class AdminMonitorApplication { public static void main(String[] args) { SpringApplication.run(AdminMonitorApplication.class, args); } } |
然后可以启动监视器。在不更改端口的情况下,它最初可以像任何新服务一样通过 http://localhost:8080 访问。如果要更改端口,可以通过在 application.properties 中设置属性 server.port 来实现
注册客户服务
那些应该在管理服务器上注册进行监控的人除了需要依赖 spring-boot-starter-actuator 之外,还会收到 POM 中的依赖 spring-boot-admin-starter-client :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.4.1</version> </dependency> |
此外,必须为要监视的应用程序提供管理服务器的 URL。这是在客户端应用程序的属性文件 application.properties 中完成的。如上一篇文章所述,您还必须指定要发布的端点:
spring.boot.admin.client.url=http://localhost:8081 management.endpoints.web.exposure.include=* |
重新启动应用程序后,它现在出现在要监控的应用程序列表中。
保护管理服务器
可以在管理服务器中查看应用程序日志文件等敏感数据。根据配置,您甚至可以更改数据。当然,这应该不是网络中的每个人都可以实现的,而是为服务的管理员保留的。
因此,建议使用 Spring Security 保护管理服务器。多亏了 Spring Boot 的“约定优于配置”的方法,这并没有那么耗时。首先,安全启动器集成在管理服务器的 POM 中。此外,还添加了对管理服务器的漂亮登录页面的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>2.4.1</version> </dependency> |
管理员访问的用户名和密码存储在管理服务器的 application.properties 中:
spring.security.user.name=admin spring.security.user.password=mypassword |
最后但并非最不重要的是,根据 Spring Boot Admin 文档添加了一个安全配置类:
@Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; private final SecurityProperties security; public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) { this.adminServer = adminServer; this.security = security; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.path("/")); http.authorizeRequests( (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() .antMatchers(this.adminServer.path("/actuator/info")).permitAll() .antMatchers(this.adminServer.path("/actuator/health")).permitAll() .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() ).formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()), new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) )) .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); } } |
其中,配置中指定除了静态资产和登录页面之外的所有访问只能由经过身份验证的用户进行。还定义了登录和注销的路径。
重新启动并调用 URL http://localhost:8080后,您将首先进入登录页面。
输入 application.properties 中存储的数据后,将显示包含受监控服务的页面。不幸的是,在服务器中看不到更多的客户端。这些已被新的安全设置锁定,无法再注册。
因此,客户端应用程序也应在它们的 application.properties 中配置用户名和密码:
spring.boot.admin.client.username=admin spring.boot.admin.client.password=mypassword |
然后客户端重新出现在要监控的应用程序列表中。
Spring 是一个强大的大型框架,而且通常情况下,关于 Spring Boot Admin 有更多的发现。一个好的开始是Github 中的快速指南-以代码为中心的存储库。