Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Spring Boot Admin 2.1.0 全攻略

Spring Boot Admin 2.1.0 全攻略

作者头像
方志朋
发布于 2019-06-21 03:18:13
发布于 2019-06-21 03:18:13
76200
代码可运行
举报
运行总次数:0
代码可运行

转载请标明出处: http://blog.csdn.net/forezp/article/details/86105850 本文出自方志朋的博客

Spring Boot Admin简介

Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。常见的功能或者监控如下:

  • 显示健康状况
  • 显示详细信息,例如
    • JVM和内存指标
    • micrometer.io指标
    • 数据源指标
    • 缓存指标
  • 显示构建信息编号
  • 关注并下载日志文件
  • 查看jvm系统和环境属性
  • 查看Spring Boot配置属性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 轻松的日志级管理
  • 与JMX-beans交互
  • 查看线程转储
  • 查看http跟踪
  • 查看auditevents
  • 查看http-endpoints
  • 查看计划任务
  • 查看和删除活动会话(使用spring-session)
  • 查看Flyway / Liquibase数据库迁移
  • 下载heapdump
  • 状态变更通知(通过电子邮件,Slack,Hipchat,…)
  • 状态更改的事件日志(非持久性)

快速开始

创建Spring Boot Admin Server

本文的所有工程的Spring Boot版本为2.1.0 、Spring Cloud版本为Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依赖(完整的依赖见源码):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>
    
    
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <spring-cloud.version>Finchley.SR2</spring-cloud.version>

在工程admin-server引入admin-server的起来依赖和web的起步依赖,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

然后在工程的启动类AdminServerApplication加上@EnableAdminServer注解,开启AdminServer的功能,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

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

}

在工程的配置文件application.yml中配置程序名和程序的端口,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  application:
    name: admin-server
server:
  port: 8769

这样Admin Server就创建好了。

创建Spring Boot Admin Client

在admin-client工程的pom文件引入admin-client的起步依赖和web的起步依赖,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

在工程的配置文件application.yml中配置应用名和端口信息,以及向admin-server注册的地址为http://localhost:8769,最后暴露自己的actuator的所有端口信息,具体配置如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

在工程的启动文件如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
public class AdminClientApplication {

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

一次启动两个工程,在浏览器上输入localhost:8769 ,浏览器显示的界面如下:

查看wallboard:

点击wallboard,可以查看admin-client具体的信息,比如内存状态信息:

也可以查看spring bean的情况:

更多监控信息,自己体验。

Spring boot Admin结合SC注册中心使用

同上一个案例一样,本案例也是使用的是Spring Boot版本为2.1.0 、Spring Cloud版本为Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依赖(完整的依赖见源码),此处省略。

搭建注册中心

注册中心使用Eureka、使用Consul也是可以的,在eureka-server工程中的pom文件中引入:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

配置eureka-server的端口信息,以及defaultZone和防止自注册。最后系统暴露eureka-server的actuator的所有端口。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false
    fetch-registry: false
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

在工程的启动文件EurekaServerApplication加上@EnableEurekaServer注解开启Eureka Server.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

eureka-server搭建完毕。

搭建admin-server

在admin-server工程的pom文件引入admin-server的起步依赖、web的起步依赖、eureka-client的起步依赖,如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
        
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

然后配置admin-server,应用名、端口信息。并向注册中心注册,注册地址为http://localhost:8761,最后将actuator的所有端口暴露出来,配置如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  application:
    name: admin-server
server:
  port: 8769
eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

在工程的启动类AdminServerApplication加上@EnableAdminServer注解,开启admin server的功能,加上@EnableDiscoveryClient注解开启eurke client的功能。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {

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

}

搭建admin-client

在admin-client的pom文件引入以下的依赖,由于2.1.0采用webflux,引入webflux的起步依赖,引入eureka-client的起步依赖,并引用actuator的起步依赖如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

在工程的配置文件配置应用名、端口、向注册中心注册的地址,以及暴露actuator的所有端口。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  application:
    name: admin-client
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
server:
  port: 8762

在启动类加上@EnableDiscoveryClie注解,开启DiscoveryClient的功能。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {

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

一次启动三个工程,在浏览器上访问localhost:8769,浏览器会显示和上一小节一样的界面。

集成spring security

在2.1.0版本中去掉了hystrix dashboard,登录界面默认集成到了spring security模块,只要加上spring security就集成了登录模块。

只需要改变下admin-server工程,需要在admin-server工程的pom文件引入以下的依赖:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

在admin-server工的配置文件application.yml中配置spring security的用户名和密码,这时需要在服务注册时带上metadata-map的信息,如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  security:
    user:
      name: "admin"
      password: "admin"
      
eureka:
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

写一个配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter,配置如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter( "redirectTo" );

        http.authorizeRequests()
                .antMatchers( adminContextPath + "/assets/**" ).permitAll()
                .antMatchers( adminContextPath + "/login" ).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
                .logout().logoutUrl( adminContextPath + "/logout" ).and()
                .httpBasic().and()
                .csrf().disable();
        // @formatter:on
    }
}

重启启动工程,在浏览器上访问:http://localhost:8769/,会被重定向到登录界面,登录的用户名和密码为配置文件中配置的,分别为admin和admin,界面显示如下:

集成邮箱报警功能

在spring boot admin中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。集成非常简单,只需要改造下admin-server即可:

在admin-server工程Pom文件,加上mail的起步依赖,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

在配置文件application.yml文件中,需要配置邮件相关的配置,如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: 124746406@qq.com

做完以上配置后,当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。

源码下载

快速开始: https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin

和spring cloud结合:https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin-cloud

参考资料

http://codecentric.github.io/spring-boot-admin/2.1.0/

https://github.com/codecentric/spring-boot-admin

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年01月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Spring Boot Admin:微服务应用监控
SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。
macrozheng
2019/10/31
8300
SpringBoot Admin监控Spring程序
SpringBoot Admin是开源社区孵化的项目,用于对SpringBoot应用的管理和监控。
鱼找水需要时间
2023/02/16
7080
SpringBoot Admin监控Spring程序
利用 Spring Boot Admin 进行项目监控管理
Spring Boot Admin (SBA) 是一个社区开源项目,用于管理和监视 Spring Boot 应用程序。应用程序通过 http 的方式注册到 Spring Boot 管理客户端,或者通过 Spring Cloud 的服务发现机制,然后针对 actuator 接口将数据通过 Vue.js 进行可视化管理。
烂猪皮
2020/11/25
1.2K0
利用 Spring Boot Admin 进行项目监控管理
SpringCloud2.0入门4-springboot-admin监控
上一节为springboot项目添加springboot-admin监控 学习了基于springboot1.5自己注册到admin的方法。接下来学习结合Eureka使用以及2.0的改变。
Ryan-Miao
2018/07/27
1.3K0
SpringCloud2.0入门4-springboot-admin监控
Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
上一篇文章《Spring Boot(十九):使用 Spring Boot Actuator 监控应用》介绍了 Spring Boot Actuator 的使用,Spring Boot Actuator 提供了对单个 Spring Boot 的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了 Spring Boot 应用的整个生命周期。
纯洁的微笑
2019/10/24
1.1K0
一起来学SpringBoot | 第十五篇:actuator与spring-boot-admin 可以说的秘密
一起来学SpringBoot | 第十四篇:强大的 actuator 服务监控与管理 中介绍了 actuator 的作用,细心的朋友可能会发现通过 http restful api的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,当服务过多的时候查看起来就过于操蛋了,每个服务都需要调用不同的接口来查看监控信息,备受各种困扰因素的我默默翻了下 全球最大男性交友平台找到了 spring-boot-admin
battcn
2018/08/03
9900
一起来学SpringBoot | 第十五篇:actuator与spring-boot-admin 可以说的秘密
springboot(二十):使用spring-boot-admin对spring-boot服务进行监控
上一篇文章《springboot(十九):使用Spring Boot Actuator监控应用》介绍了Spring Boot Actuator的使用,Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了Spring Boot应用的整个生命周期。 但是这样监控也有一些问题:第一,所有的监控都需要调用固定的接口来查看,如果全面查看应用状态需要调用很多接口,并且接口返回的Json信息不方便运营人员理解;第二,如果Spring
纯洁的微笑
2018/04/18
1.5K0
springboot(二十):使用spring-boot-admin对spring-boot服务进行监控
【Spring Boot实战与进阶】如何搭建Spring Boot Admin应用监控台
  Spring Boot Admin(SBA)是一款基于Actuator开发的开源项目,用于管理和监控Spring Boot应用程序。应用程序注册到我们的Spring启动管理客户端(通过HTTP)或使用Spring Cloud(例如Eureka)发现。UI只是一个AngularJs应用程序,位于Spring启动Actuator endpoints之上。以图形化界面的方式展示Spring Boot应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。   上一篇文章讲了《如何使用Actuator监控Spring Boot应用》,它提供了许多REST接口来查看应用的信息,但是它返回的是大量的JSON格式数据,信息看上去不直观也不易于理解。所以我们采用Spring Boot Admin这种监控方式。
程序员云帆哥
2022/05/12
6210
【Spring Boot实战与进阶】如何搭建Spring Boot Admin应用监控台
每天20分钟之spring boot admin
对于springboot应用的监控我们有很多选择,因为actuator提供了一组数据
李子健
2022/08/27
6130
Spring Boot Admin 2.0监控配置
在实际开发中,需要监控项目的异常情况,如掉线,内存开销等信息。Spring Boot Admin通过简单的配置即可实现。
geekfly
2022/04/24
3630
Spring Boot Admin 2.0监控配置
spring-boot-admin 2.0小试牛刀
新版前端改用vue.js进行了重构,后端的话,使用event sourcing的原则进行了重构,支持spring5,移除了spring-cloud-starter依赖,另外使用WebClient替代了zuul等等,具体详见spring-boot-admin-changes-with-2-x。
code4it
2018/09/17
4600
spring-boot-admin 2.0小试牛刀
Spring Boot Admin 服务监控利器 !
Spring Boot Admin 用于对 Spring Boot 应用的管理和监控。可以用来监控服务是否健康、是否在线、以及一些jvm数据等等。
一行Java
2023/02/23
1.1K0
Spring Boot Admin 服务监控利器 !
springboot之spring-boot-admin对springboot服务进行监控
Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过 HTTP 或者使用 Eureka 注册到 admin server 中进行展示,Spring Boot Admin UI 部分使用 VueJs 将数据展示在前端。
Vincent-yuan
2020/11/26
5700
springboot之spring-boot-admin对springboot服务进行监控
Spring Boot 2 实战:使用 Spring Boot Admin 监控平台
生产上对 Web 应用 的监控是十分必要的。我们可以近乎实时来对应用的健康、性能等其他指标进行监控来及时应对一些突发情况。避免一些故障的发生。对于 Spring Boot 应用来说我们可以通过一个轻量级的监控工具 Spring Boot Admin (SBA) 来进行监控。
码农小胖哥
2019/12/10
3.8K0
Spring Boot 2 实战:使用 Spring Boot Admin 监控平台
利用 Spring Boot Admin 对 Spring Boot 应用监控以及配置认证
Spring Boot Admin 是一个优秀的 Spring Boot 应用监控,可以查看应用的各项性能指标,修改日志级别(生产环境利器,不用动不动就上 Arthas),dump 线程等功能。如果是微服务可以使用 Eureka 来做服务的注册与发现,单体应用的话直接往 Spring Boot Admin 的 Server 端注册就行。
Yuyy
2022/09/21
1.4K0
Spring Boot Application 监控管理利器: Spring Boot Admin
上篇文章了解了 Spring Boot Actuator,引入后即可通过访问不同的端点,来获得相应的监控信息。
happyJared
2019/04/18
6650
SpringBootAdmin2.0实现微服务应用监控
Spring Boot Admin Server 可以监控的功能很多,使用起来没有难度,下面描述下可以监测的部分内容:
黎明大大
2020/09/08
1.9K0
SpringBootAdmin2.0实现微服务应用监控
Spring Boot 2.X(十七):应用监控之 Spring Boot Admin 使用及配置
Spring Boot Admin 是 Spring Boot 应用程序运行状态监控和管理的后台界面。最新UI使用vue.js重写。
朝雾轻寒
2019/11/14
2.4K0
聊聊如何玩转spring-boot-admin
Spring Boot Admin 是一个监控工具,旨在以良好且易于访问的方式可视化 Spring Boot Actuators 提供的信息
lyb-geek
2023/09/20
1K0
聊聊如何玩转spring-boot-admin
SpringBoot集成SpringBootAdmin实现监控
code2roc
2023/07/19
2030
SpringBoot集成SpringBootAdmin实现监控
推荐阅读
相关推荐
Spring Boot Admin:微服务应用监控
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档