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

前面的文章我们讲了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(监控工具)的使用,希望对大家有所帮助!

(0)

相关推荐

  • 详解使用spring boot admin监控spring cloud应用程序

    Spring Boot提供的监控接口,例如:/health./info等等,实际上除了之前提到的信息,还有其他信息业需要监控:当前处于活跃状态的会话数量.当前应用的并发数.延迟以及其他度量信息. 最近在找一个spring cloud的监控组件,要求粒度要到每一个接口的,hystrix dashboard显然不适合,也不是这个应用场景.后来发现了spring boot admin这个神器,可以注册到Eureka和spring cloud无缝整合,页面AngularJS写的还算凑合,里面包含有许多功

  • 使用spring-boot-admin对spring-boot服务进行监控的实现方法

    spring-boot-admin,简称SBA,是一个针对spring-boot的actuator接口进行UI美化封装的监控工具.他可以:在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息.内存信息.JVM信息.垃圾回收信息.各种配置信息(比如数据源.缓存列表和命中率)等,还可以直接修改logger的level. 官网:https://github.com/codecentric/spring-boot-admin 使用指南:http://codecentric.

  • 详解Spring boot Admin 使用eureka监控服务

    前言 最近刚好有空,来学习一下如何搭建spring boot admin环境.其中遇到很多的坑. 网上大多都是使用admin-url的方式直接来监控的,感觉一点也不灵活,这不是我想要的结果,所以本篇介绍借助eureka服务注册和发现功能来灵活监控程序. 本文主要记录spring boot admin的搭建过程,希望能有所帮助.其实非常的简单,不要被使用常规方式的误导! 环境介绍 IDE:intellij idea jdk: java8 maven:3.3.9 spring boot:1.5.6

  • SpringBoot Admin 使用指南(推荐)

    Spring Boot Admin 是一个管理和监控你的 Spring Boot 应用程序的应用程序. 这些应用程序通过 Spring Boot Admin Client(通过 HTTP)注册或者使用 Spring Cloud(例如 Eureka)发现. UI只是 Spring Boot Actuator 端点上的一个 AngularJs 应用程序. 快速开始 首先在 IDEA 创建一个 SpringBoot 项目,把它当作 server 端,工程如下: 然后在 pom.xml 中引入依赖: <

  • Spring Boot Admin 的使用详解

    一.前言 Spring Boot Admin 用于监控基于 Spring Boot 的应用.官方文档在这里(v1.3.4):<Spring Boot Admin Reference Guide> 实践的过程中,感觉这个 User Guide 结构上还是说的不太明白.所以我就大概写一遍我的实践过程与理解. 阅读此文前提条件是: 使用过 Maven. 你跑过基于 Spring Boot 的 hello world 程序. 第三节需要你会点 Spring Cloud 的 Eureka Server

  • 使用Spirng Boot Admin监控Spring Cloud应用项目

    一. 介绍 GitHub: https://github.com/codecentric/spring-boot-admin 官方文档: http://codecentric.github.io/spring-boot-admin/1.5.7/ (此文档为1.5.7版本的文档) The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ®

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

    前面的文章我们讲了Spring Boot的Actuator.但是Spring Boot Actuator只是提供了一个个的接口,需要我们自行集成到监控程序中.今天我们将会讲解一个优秀的监控工具Spring Boot Admin. 它采用图形化的界面,让我们的Spring Boot管理更加简单. 先上图给大家看一下Spring Boot Admin的界面: 从界面上面我们可以看到Spring Boot Admin提供了众多强大的监控功能.那么开始我们的学习吧. 配置Admin Server 既然是

  • 详解Spring Boot Admin监控服务上下线邮件通知

    本文介绍了Spring Boot Admin监控服务上下线邮件通知,分享给大家,具体如下: 微服务架构下,服务的数量少则几十,多则上百,对服务的监控必不可少. 如果是以前的单体项目,启动了几个项目是固定的,可以通过第三方的监控工具对其进行监控,然后实时告警. 在微服务下,服务数量太多,并且可以随时扩展,这个时候第三方的监控功能就不适用了,我们可以通过Spring Boot Admin连接注册中心来查看服务状态,这个只能在页面查看. 很多时候更希望能够自动监控,通过邮件告警,某某服务下线了这样的功

  • Spring Boot Admin的使用详解(Actuator监控接口)

    第一部分 Spring Boot Admin 简介 Spring Boot Admin用来管理和监控Spring Boot应用程序. 应用程序向我们的Spring Boot Admin Client注册(通过HTTP)或使用SpringCloud®(例如Eureka,Consul)发现. UI是Spring Boot Actuator端点上的Vue.js应用程序. Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件.每个应用都认为是一个客户端,通过HT

  • spring boot admin 搭建详解

    1. Spring Boot Admin 定义 Spring Boot Admin 是针对 Spring Boot 的actuator接口进行UI美化的接口,可以方便的浏览被监控的Spring Boot项目的基本信息.详细的Health信息.JVM信息.垃圾回收信息.各种配置信息(数据源.缓存.命中率等) 2 系统搭建 server 端搭建 1.gradle中引入一下依赖 compile('de.codecentric:spring-boot-admin-server:1.5.6') compi

  • Spring Boot Admin管理监控数据的方法

    spring boot actuator 可以监控应用的各种信息, 唯一的缺点就是返回的监控信息是JSON格式的数据,还有一点就是在微服务架构下,服务的实例会很多,一个个去看监控信息这似乎有点不太可能,而且这么多地址信息也只能去Eureka中去找,有没有一个功能能够集中的管理Eureka中的服务信息,并且可以通过界面的方式查看actuator 提供的监控信息,它就是Spring Boot Admin. Spring Boot  Admin简介 spring boot admin github开源

  • Spring Boot Admin微服务应用监控的实现

    Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍. Spring Boot Admin 简介 SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来.Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应

  • Spring Boot Admin实践详解

    在Spring Boot Actuator中提供很多像health.metrics等实时监控接口,可以方便我们随时跟踪服务的性能指标.Spring Boot默认是开放这些接口提供调用的,那么就问题来了,如果这些接口公开在外网中,很容易被不法分子所利用,这肯定不是我们想要的结果.在这里我们提供一种比较好的解决方案. 被监控的服务配置 为被保护的http请求添加请求前缀 management: context-path: /example-context eureka: instance: stat

随机推荐