01.14 Springboot2.0學習10 SpringBootAdmin管理分佈式應用

一、簡介

來自Codecentric的Spring Boot Admin 是一個管理和監控SpringBoot應用的工具。Spring Boot應用程序可以使用Spring Boot Admin Client通過進行主動HTTP註冊,或在服務端使用Spring Cloud(如Eureka,Consul)工具進行服務發現。


SpringBoot Admin前端使用AngularJS應用程序,可以監控:

  • 應用健康狀態
  • JVM、內存等詳細指標
  • 構建信息
  • 下載日誌文件
  • 環境變量
  • 線程信息
  • http信息
  • 計劃任務
  • 狀態信息

等。

本文運行環境:

  • jdk1.8+
  • maven
  • spring boot 2.2.2

本文示例中,客戶端通過http註冊到服務端。

二、實踐

2.1 創建Spring Boot Admin Server

首先創建一個Spring Boot程序。

Springboot2.0學習10 SpringBootAdmin管理分佈式應用

添加Maven引用

<code>  <dependency>
<groupid>de.codecentric/<groupid>
<artifactid>spring-boot-admin-starter-server/<artifactid>
<version>2.2.1/<version>
/<dependency>

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
<version>2.1.8.RELEASE/<version>
/<dependency>
<dependency>
<groupid>de.codecentric/<groupid>
<artifactid>spring-boot-admin-server-ui-login/<artifactid>
<version>1.5.7/<version>
/<dependency>/<code>

創建啟動類,加@EnableAdminServer註解

<code>package com.xundh.springboot.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}/<code>

配置端口

<code>spring:
application:
name: admin-server
server:
port: 8769/<code>

項目結構如下:

Springboot2.0學習10 SpringBootAdmin管理分佈式應用

2.2 創建Client程序

再創建一個Spring boot 應用程序

添加Maven引用

<code>        <dependency>
<groupid>de.codecentric/<groupid>
<artifactid>spring-boot-admin-starter-client/<artifactid>
<version>2.2.1/<version>
/<dependency>

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
<version>2.2.2.RELEASE/<version>
/<dependency>/<code>

在配置文件裡配置服務端地址,並暴露所有監控接口

<code>spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS/<code>

創建啟動類

<code>package com.xundh.springboot.client;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}/<code>

Client項目結構:

Springboot2.0學習10 SpringBootAdmin管理分佈式應用

前臺查看 到瀏覽器打開:http://localhost:8769/

Springboot2.0學習10 SpringBootAdmin管理分佈式應用


應用牆

Springboot2.0學習10 SpringBootAdmin管理分佈式應用

2.3 安全配置

Spring Boot Admin需要進入應用程序的重要節點,最好在server和client都加上一些安全配置。

服務端配置

啟用安全配置,給spring boot admin添加登陸接口

maven引用

<code><dependency>
<groupid>de.codecentric/<groupid>
<artifactid>spring-boot-admin-server-ui-login/<artifactid>
<version>1.5.7/<version>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-security/<artifactid>
<version>2.1.8.RELEASE/<version>
/<dependency>/<code>

添加安全配置類

<code>package com.xundh.springboot.server;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

@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);
}
}/<code>

這時候重啟server,再訪問 http://localhost:8769 可以看到有登陸框。

Springboot2.0學習10 SpringBootAdmin管理分佈式應用


默認賬號是 user,密碼在控制檯可以看到:

Springboot2.0學習10 SpringBootAdmin管理分佈式應用

設置賬號與密碼

修改application.yaml如下:

<code>spring:
application:
name: admin-server
security:
user:
name: 'root'
password: 'root'
boot:
admin:

client:
instance:
metadata:
user:
name: ${spring.security.user.name}
password: ${spring.security.user.password}
server:
port: 8769/<code>

注意這時client也註冊不到server上了。

在client端設置登陸server賬號密碼

<code>spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
username: root
password: root
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS/<code>

這時再啟動client,在server端可以看到註冊的應用了。

2.4 通知

當客戶端發生一些事件時,通知用戶,以下是可選的通知方式:

  • Email
  • PagerDuty
  • OpsGenie
  • Hipchat
  • Slack
  • Let's Chat

郵件通知

在server配置依賴:

<code><dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-mail/<artifactid>
<version>2.1.7.RELEASE/<version>
/<dependency>/<code>

配置文件裡配置郵箱:

<code>spring:
mail:
host: smtp.example.com
username: smtp_user
password: smtp_password
boot:
admin:
notify:
mail:
to: [email protected]/<code>

當有客戶端上下線的時候就會發郵件通知。

自定義通知配置

一個通知過濾示例:

<code>@Configuration
public class NotifierConfiguration {
private final InstanceRepository repository;
private final ObjectProvider<list>> otherNotifiers;

public NotifierConfiguration(InstanceRepository repository,

ObjectProvider<list>> 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;
}
}/<list>/<list>/<code>


分享到:


相關文章: