Spring Boot Admin(监控工具)的使用

 更新时间:2020年02月13日 10:48:35   作者:FLYDEAN  
今天我们将会讲解一个优秀的监控工具Spring Boot Admin。 它采用图形化的界面,让我们的Spring Boot管理更加简单,需要的朋友可以参考下

前面的文章我们讲了Spring Boot的Actuator。但是Spring Boot Actuator只是提供了一个个的接口,需要我们自行集成到监控程序中。今天我们将会讲解一个优秀的监控工具Spring Boot Admin。 它采用图形化的界面,让我们的Spring Boot管理更加简单。

先上图给大家看一下Spring Boot Admin的界面:

 

从界面上面我们可以看到Spring Boot Admin提供了众多强大的监控功能。那么开始我们的学习吧。

配置Admin Server

既然是管理程序,肯定有一个server,配置server很简单,我们添加这个依赖即可:

<dependency>
 <groupId>de.codecentric</groupId>
 <artifactId>spring-boot-admin-starter-server</artifactId>
 <version>2.2.2</version>
</dependency>

同时我们需要在main程序中添加@EnableAdminServer来启动admin server。

@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminServerApplication {

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

配置admin client

有了server,我们接下来配置需要监控的client应用程序,在本文中,我们自己监控自己,添加client依赖如下:

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

我们需要为client指定要注册到的admin server:

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

因为Spring Boot Admin依赖于 Spring Boot Actuator, 从Spring Boot2 之后,我们需要主动开启暴露的主键,如下:

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

配置安全主键

通常来说,我们需要一个登陆界面,以防止未经授权的人访问。spring boot admin提供了一个UI供我们使用,同时我们添加Spring Security依赖:

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

添加了Spring 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);
 }
}

接下来,我们在配置文件中指定服务器的用户名和密码:

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

作为一个客户端,连接服务器的时候,我们也需要提供相应的认证信息如下:

spring.boot.admin.client.instance.metadata.user.name=admin
spring.boot.admin.client.instance.metadata.user.password=admin
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

好了,登录页面和权限认证也完成了。

Hazelcast集群

Spring Boot Admin 支持Hazelcast的集群,我们先添加依赖如下:

<dependency>
 <groupId>com.hazelcast</groupId>
 <artifactId>hazelcast</artifactId>
 <version>3.12.2</version>
</dependency>

然后添加Hazelcast的配置:

@Configuration
public class HazelcastConfig {
 @Bean
 public Config hazelcast() {
  MapConfig eventStoreMap = new MapConfig("spring-boot-admin-event-store")
   .setInMemoryFormat(InMemoryFormat.OBJECT)
   .setBackupCount(1)
   .setEvictionPolicy(EvictionPolicy.NONE)
   .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
  MapConfig sentNotificationsMap = new MapConfig("spring-boot-admin-application-store")
   .setInMemoryFormat(InMemoryFormat.OBJECT)
   .setBackupCount(1)
   .setEvictionPolicy(EvictionPolicy.LRU)
   .setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));
  Config config = new Config();
  config.addMapConfig(eventStoreMap);
  config.addMapConfig(sentNotificationsMap);
  config.setProperty("hazelcast.jmx", "true");
  config.getNetworkConfig()
   .getJoin()
   .getMulticastConfig()
   .setEnabled(false);
  TcpIpConfig tcpIpConfig = config.getNetworkConfig()
   .getJoin()
   .getTcpIpConfig();
  tcpIpConfig.setEnabled(true);
  tcpIpConfig.setMembers(Collections.singletonList("127.0.0.1"));
  return config;
 }
}

本文的例子可以参考 https://github.com/ddean2009/learn-springboot2/tree/master/springboot-admin

总结

以上所述是小编给大家介绍的Spring Boot Admin(监控工具)的使用,希望对大家有所帮助!

相关文章

  • Mybatis中BindingException异常的产生原因及解决过程

    Mybatis中BindingException异常的产生原因及解决过程

    BindingException异常是MyBatis框架中自定义的异常,顾名思义指的是绑定出现问题,下面这篇文章主要给大家介绍了关于MyBatis报错BindingException异常的产生原因及解决过程,需要的朋友可以参考下
    2023-06-06
  • SpringBoot的自动配置原理解析

    SpringBoot的自动配置原理解析

    这篇文章主要介绍了SpringBoot的自动配置原理解析,SpringBoot的自动配置要从它的启动类@SpringBootApplication说起,点进注解,@Target设置当前注解可以标记在哪,(ElementType.type)表示标注在类上面,需要的朋友可以参考下
    2023-08-08
  • Java日常练习题,每天进步一点点(50)

    Java日常练习题,每天进步一点点(50)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-08-08
  • JPA使用乐观锁应对高并发方式

    JPA使用乐观锁应对高并发方式

    这篇文章主要介绍了JPA使用乐观锁应对高并发方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 2020.2 IntelliJ IDEA激活与IDEA2020.2破解详细教程

    2020.2 IntelliJ IDEA激活与IDEA2020.2破解详细教程

    这篇文章主要介绍了2020.2 IntelliJ IDEA激活与IDEA2020.2破解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • java 删除数组元素与删除重复数组元素的代码

    java 删除数组元素与删除重复数组元素的代码

    在java中删除数组元素与过滤重复数组元素我们都会需要去遍历数组然后根据我们设置的值或方法进行去除数组
    2013-10-10
  • Java数据结构中图的进阶详解

    Java数据结构中图的进阶详解

    在Java学习与应用中,数据结构无疑是每个人都要接触的难点,为了更好的学习数据结构这一块内容,用图来理解便是最好的方式,让我们一起来了解本篇内容图的进阶
    2022-01-01
  • SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

    SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤

    这篇文章主要介绍了SpringBoot中使用JeecgBoot的Autopoi导出Excel的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • MyBatis ofType和javaType的区别说明

    MyBatis ofType和javaType的区别说明

    这篇文章主要介绍了MyBatis ofType和javaType的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java操作redis设置第二天凌晨过期的解决方案

    Java操作redis设置第二天凌晨过期的解决方案

    这篇文章主要介绍了Java操作redis设置第二天凌晨过期的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论