Spring Boot Admin 介绍

1.概述

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

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

2.设置管理服务器

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


    de.codecentric
    spring-boot-admin-server
    1.5.4


    de.codecentric
    spring-boot-admin-server-ui
    1.5.4

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

@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminServerApplication.class, args);
    }
}

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

3.设置客户端

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


    de.codecentric
    spring-boot-admin-starter-client
    1.5.4

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

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

4.安全配置

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

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


    de.codecentric
    spring-boot-admin-server-ui-login
    1.5.4


    org.springframework.boot
    spring-boot-starter-security

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

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

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .formLogin()
          .loginPage("/login.html")
          .loginProcessingUrl("/login")
          .permitAll();
        http
          .logout().logoutUrl("/logout");
        http
          .csrf().disable();
        http
          .authorizeRequests()
          .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
          .permitAll();
        http
          .authorizeRequests()
          .antMatchers("/**")
          .authenticated();
        http.httpBasic();
    }
}

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

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

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

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

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

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

management.security.enabled=true
security.user.name=client
security.user.password=client
spring.boot.admin.client.metadata.user.name=${security.user.name}
spring.boot.admin.client.metadata.user.password=${security.user.password}

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

5.监控和管理功能

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

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

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

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


    com.hazelcast
    hazelcast

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

@Configuration
public class HazelcastConfig {
 
    @Bean
    public Config hazelcast() {
        return new Config()
          .setProperty("hazelcast.jmx", "true")
          .addMapConfig(new MapConfig("spring-boot-admin-application-store")
            .setBackupCount(1)
            .setEvictionPolicy(EvictionPolicy.NONE))
          .addListConfig(new ListConfig("spring-boot-admin-event-store")
            .setBackupCount(1)
            .setMaxSize(1000));
    }
}

6.通知

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

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

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

6.1 邮件通知

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


    org.springframework.boot
    spring-boot-starter-mail
    1.5.4

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

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

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

6.2 即时通信Hipchat通知

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

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

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

6.3。自定义通知配置

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

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

Configuration
@EnableScheduling
public class NotifierConfiguration {
 
    @Autowired private Notifier notifier;
 
    @Bean
    public FilteringNotifier filteringNotifier() {
        return new FilteringNotifier(notifier);
    }
 
    @Bean
    @Primary
    public RemindingNotifier remindingNotifier() {
        RemindingNotifier remindingNotifier 
          = new RemindingNotifier(filteringNotifier());
        remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5));
        return remindingNotifier;
    }
 
    @Scheduled(fixedRate = 60_000L)
    public void remind() {
        remindingNotifier().sendReminders();
    }
}

7.结论

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

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

你可能感兴趣的:(微服务)