Spring Boot Admin

1.Spring Boot Admin是什么?

它是用于监控springboot应用程序的监控系统,应用程序通过Apring Boot Admin Client进行注册(通过HTTP的方式),或者使用springcloud来发现(比如:eureka),UI只是在Spring Boot Actuator端点上的一个AngularJs应用程序

这里写图片描述


2、开始

2.1、设置spring Boot Admin Server

1.pom.xml

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>1.5.4</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>1.5.4</version>
</dependency>

2.通过在配置中添加@enableadminserver,来导入Spring Boot Admin Server配置:

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }
}
2.2、注册客户端应用程序
2.2.1、spring-boot-admin-starter-client

想要注册的每个应用程序都必须包含Spring Boot Admin Client。
1.在依赖项添加spring-boo-admin-starclient-client:
pom.xml

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>1.5.4</version>
</dependency>

2.通过配置Spring Boot Admin Server URL来启用SBA客户端
application.yml | application.properties

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

Spring Boot Admin Server的URL
所有端点在缺省情况下都是安全的。为了简便起见,禁用安全性


3.安全

1.spring-boot-admin-server-ui-login和spring-boot-starter-security添加到依赖项中

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui-login</artifactId>
    <version>1.5.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.Spring的安全配置可能如下:

## Securing Spring Boot Admin Server
 @Configuration
  public static class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      // Page with login form is served as /login.html and does a POST on /login
      http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
      // The UI does a POST on /logout on logout
      http.logout().logoutUrl("/logout");
      // The ui currently doesn't support csrf
      http.csrf().disable();

      // Requests for the login page and the static assets are allowed
      http.authorizeRequests()
          .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
          .permitAll();
      // ... and any other request needs to be authorized
      http.authorizeRequests().antMatchers("/**").authenticated();

      // Enable so that the clients can authenticate via HTTP basic for registering
      http.httpBasic();
    }
  }

3.当actuator endpoints使用HTTP基本身份验证时,SBA服务器需要凭证来访问它们,在注册应用程序时,可以在元数据中提交凭据,BasicAuthHttpHeaderProvider使用该元数据添加授权头来访问应用程序的actuator endpoints,可以提供自己的httpheadprovider来改变行为(例如添加一些解密)或添加额外的头。

## Securing Client Actuator Endpoints
spring.boot.admin.client.metadata.user.name=${security.user.name}
spring.boot.admin.client.metadata.user.password=${security.user.password}
spring.boot.admin.url=http://localhost:9090/dlc-admin

DEMO:Spring-Boot-admin-example


4.发送邮件配置

1.配置一个JavaMailSender,使用spring-startmail-mail,并设置一个接收方。
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application.properties

spring.mail.host=smtp.example.com
spring.boot.admin.notify.mail.to=admin@example.com
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐