Spring Boot Admin介紹與實踐

1. 簡介

Spring Boot Admin是一個Web應用程序,用來對Spring Boot應用進行監控和管理。每個應用都可以被認為是一個客戶端,它們可以註冊到Spring Boot Admin服務上。

在這篇文章中將介紹如何配置Spring Boot Admin服務以及如何讓Spring Boot應用成為客戶端。

Spring Boot Admin介紹與實踐

2. 創建Admin Server

首先創建一個簡單的Spring Boot web應用程序並添加下面依賴:

 de.codecentric
 spring-boot-admin-starter-server
 2.1.6

然後在應用的main class中添加註解@EnableAdminServer,如下面所示:

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

到這裡我們已經完成Admin Server的配置,啟動後就可以將Spring Boot應用註冊到Server上。

3.設置客戶端

當我們配置完並啟動admin server後,就可以讓我們的Spring Boot應用作為客戶端註冊到Server上。我們必須添加下面的依賴:

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

下面我們在Spring Boot應用中配置Admin Server的URL,配置如下:

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

從Spring Boot 2開始,默認情況下只開啟health和info endpoints

下面我們將所有的endpoints都打開,配置如下:

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

4.Security配置

為了server和client之間的連接安全,我們往往需要在server端和客戶端進行安全配置。

在服務端添加如下依賴:

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


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

在Server中添加Security類,如下所示:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 private final AdminServerProperties adminServer;
 
 public WebSecurityConfig(AdminServerProperties adminServer) {
 this.adminServer = adminServer;
 }
 
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 SavedRequestAwareAuthenticationSuccessHandler successHandler = 
 new SavedRequestAwareAuthenticationSuccessHandler();
 successHandler.setTargetUrlParameter("redirectTo");
 successHandler.setDefaultTargetUrl(this.adminServer.getContextPath() + "/");
 
 http
 .authorizeRequests()
 .antMatchers(this.adminServer.getContextPath() + "/assets/**").permitAll()
 .antMatchers(this.adminServer.getContextPath() + "/login").permitAll()
 .anyRequest().authenticated()
 .and()
 .formLogin()
 .loginPage(this.adminServer.getContextPath() + "/login")
 .successHandler(successHandler)
 .and()
 .logout()
 .logoutUrl(this.adminServer.getContextPath() + "/logout")
 .and()
 .httpBasic()
 .and()
 .csrf()
 .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
 .ignoringRequestMatchers(
 new AntPathRequestMatcher(this.adminServer.getContextPath() + 
 "/instances", HttpMethod.POST.toString()), 
 new AntPathRequestMatcher(this.adminServer.getContextPath() + 
 "/instances/*", HttpMethod.DELETE.toString()),
 new AntPathRequestMatcher(this.adminServer.getContextPath() + "/actuator/**"))
 .and()
 .rememberMe()
 .key(UUID.randomUUID().toString())
 .tokenValiditySeconds(1209600);
 }
}

在客戶端的property文件中添加如下配置才能連接到有安全配置的server上:

spring.boot.admin.client.username=admin
spring.boot.admin.client.password=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}

5.通知配置

5.1 郵件通知

admin server添加如下依賴:

 org.springframework.boot
 spring-boot-starter-mail
 2.1.7.RELEASE

然後配置一下屬性:

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

5.2 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/

5.3 自定義通知

@Configuration
public class NotifierConfiguration {
 private final InstanceRepository repository;
 private final ObjectProvider> otherNotifiers;
 
 public NotifierConfiguration(InstanceRepository repository, 
 ObjectProvider> otherNotifiers) {
 this.repository = repository;
 this.otherNotifiers = otherNotifiers;
 }
 
 @Bean
 public FilteringNotifier filteringNotifier() {
 CompositeNotifier delegate = 
 new CompositeNotifier(this.otherNotifiers.getIfAvailable(Collections::emptyList));
 return new FilteringNotifier(delegate, this.repository);
 }
 
 @Bean
 public LoggingNotifier notifier() {
 return new LoggingNotifier(repository);
 }
 
 @Primary
 @Bean(initMethod = "start", destroyMethod = "stop")
 public RemindingNotifier remindingNotifier() {
 RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier(), repository);
 remindingNotifier.setReminderPeriod(Duration.ofMinutes(5));
 remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(60));
 return remindingNotifier;
 }
}


分享到:


相關文章: