Spring Boot Admin 介绍

﹏ヽ暗。殇╰゛Y 2022-03-17 21:00 450阅读 0赞

1.概述

Spring Boot Admin是一个Web应用,用于管理和监视Spring Boot应用程序的运行状态。每个Spring Boot应用程序都被视为客户端并注册到管理服务器。背后的数据采集是由Spring Boot Actuator端点提供。

在本文中,我们将描述配置Spring Boot Admin服务器的步骤以及应用程序如何成为客户端。

2.设置管理服务器

首先,我们需要创建一个简单的Spring Boot Web应用程序,添加以下Maven依赖项:

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-server</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>de.codecentric</groupId>
  8. <artifactId>spring-boot-admin-server-ui</artifactId>
  9. <version>1.5.4</version>
  10. </dependency>

添加此之后,@ EnableAdminServer将可用。我们将其添加到主类中,如下例所示:

  1. @EnableAdminServer
  2. @SpringBootApplication
  3. public class SpringBootAdminServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringBootAdminServerApplication.class, args);
  6. }
  7. }

此时,服务器已准备好启动并可以注册客户端应用程序。

3.设置客户端

在我们设置了管理服务器之后,我们可以将一个Spring Boot应用程序注册为客户端。我们必须添加以下Maven依赖项:

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>

剩下的唯一事情就是配置客户端以访问管理服务器的URL。为此,我们只需添加以下属性:

  1. spring.boot.admin.url=http://localhost:8080
  2. management.security.enabled=false

4.安全配置

Spring Boot Admin服务器可以访问应用程序的敏感端点,因此建议为管理员和客户端应用程序添加一些安全配置。

首先,我们将专注于配置管理服务器的安全性。我们必须添加以下Maven依赖项:

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-server-ui-login</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>

这将为管理应用程序添加登录界面以启用安全性。

之后,我们将添加一个安全配置类,如下所示:

  1. @Configuration
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http
  6. .formLogin()
  7. .loginPage("/login.html")
  8. .loginProcessingUrl("/login")
  9. .permitAll();
  10. http
  11. .logout().logoutUrl("/logout");
  12. http
  13. .csrf().disable();
  14. http
  15. .authorizeRequests()
  16. .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
  17. .permitAll();
  18. http
  19. .authorizeRequests()
  20. .antMatchers("/**")
  21. .authenticated();
  22. http.httpBasic();
  23. }
  24. }

这是一个简单的安全配置,但添加后,我们会注意到客户端无法再注册到服务器。

为了将客户端注册到新的安全服务器,我们必须在客户端的属性文件中添加更多配置:

  1. spring.boot.admin.username=admin
  2. spring.boot.admin.password=admin

现在管理服务器处于安全的环境中,但客户端不是。在生产系统中,我们试图监控的应用程序自然也应该受到保护。

因此,我们也将为客户端添加安全性 - 我们将在管理服务器的UI界面中注意到客户端信息不再可用。

我们必须添加一些我们将发送到管理服务器的元数据。服务器使用此信息连接到客户端的端点:

  1. management.security.enabled=true
  2. security.user.name=client
  3. security.user.password=client
  4. spring.boot.admin.client.metadata.user.name=${security.user.name}
  5. spring.boot.admin.client.metadata.user.password=${security.user.password}

当然,通过HTTP发送凭据并不安全 - 因此通信需要通过HTTPS进行。

5.监控和管理功能

可以将Spring Boot Admin配置为仅显示我们认为有用的信息。我们只需要更改默认配置并添加我们自己需要的指标:

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

随着我们的进一步操作,我们将看到还有一些其他功能可以探索。我们可以使用Jolokia和Loglevel进行JMX bean管理。

Spring Boot Admin还支持使用Hazelcast进行群集复制。我们只需添加以下Maven依赖项,让其自动配置完成剩下的工作:

  1. <dependency>
  2. <groupId>com.hazelcast</groupId>
  3. <artifactId>hazelcast</artifactId>
  4. </dependency>

如果我们想要一个持久化的Hazelcast实例,我们将使用自定义配置:

  1. @Configuration
  2. public class HazelcastConfig {
  3. @Bean
  4. public Config hazelcast() {
  5. return new Config()
  6. .setProperty("hazelcast.jmx", "true")
  7. .addMapConfig(new MapConfig("spring-boot-admin-application-store")
  8. .setBackupCount(1)
  9. .setEvictionPolicy(EvictionPolicy.NONE))
  10. .addListConfig(new ListConfig("spring-boot-admin-event-store")
  11. .setBackupCount(1)
  12. .setMaxSize(1000));
  13. }
  14. }

6.通知

接下来,让我们讨论一下如果我们的注册客户端出现问题,如何从管理服务器接收通知。

以下通知程序可用于配置:

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

6.1 邮件通知

这里我们将专注于为管理服务器配置邮件通知。为此,我们必须添加邮件启动程序依赖项,如下所示:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>

在此之后,我们必须添加一些邮件配置:

  1. spring.mail.host=smtp.example.com
  2. spring.mail.username=smtp_user
  3. spring.mail.password=smtp_password
  4. spring.boot.admin.notify.mail.to=admin@example.com

现在,每当我们的注册客户将其状态从UP更改为OFFLINE或其他情况时,都会向上面配置的地址发送电子邮件。对于其他通知程序,配置类似。

6.2 即时通信Hipchat通知

正如我们所看到的,与Hipchat的整合非常简单; 只需设置几个必需属性:

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

有了这些定义后,我们会在Hipchat注意到,只要客户的状态发生变化,我们就会收到通知。

6.3。自定义通知配置

我们可以配置一个自定义通知系统,为我们提供一些强大的工具。我们可以使用提醒通知程序发送预定通知,直到客户端状态发生变化。

或者我们可能希望向过滤的客户端集发送通知。为此,我们可以使用过滤通知程序:

  1. Configuration
  2. @EnableScheduling
  3. public class NotifierConfiguration {
  4. @Autowired private Notifier notifier;
  5. @Bean
  6. public FilteringNotifier filteringNotifier() {
  7. return new FilteringNotifier(notifier);
  8. }
  9. @Bean
  10. @Primary
  11. public RemindingNotifier remindingNotifier() {
  12. RemindingNotifier remindingNotifier
  13. = new RemindingNotifier(filteringNotifier());
  14. remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5));
  15. return remindingNotifier;
  16. }
  17. @Scheduled(fixedRate = 60_000L)
  18. public void remind() {
  19. remindingNotifier().sendReminders();
  20. }
  21. }

7.结论

本入门教程介绍了使用Spring Boot Admin监视和管理其它Spring Boot应用程序必须执行的简单步骤。

自动配置允许我们只需要添加一些基本的配置,就能够得到一个正常工作的管理服务器。

发表评论

表情:
评论列表 (有 0 条评论,450人围观)

还没有评论,来说两句吧...

相关阅读