原文:https://blog.csdn.net/hubo_88/article/details/80671192

Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。

(一)简介
Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。

(二)Spring Boot Admin 是由服务端和客户端组成
在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端,基于这种的配置如下步骤:

2.1 Server
2.1.1 添加相关依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.1.2 启动类添加注解,开启监控
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
2.1.3 配置文件
server:
port: 8788
2.2 Client
2.2.1 添加相关依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
2.2.2 配置文件
spring.boot.admin.client.url: "http://localhost:8788"
management.endpoints.web.exposure.include: "*"
以上的配置,就可以实现 Spring Boot 项目中 Spring Boot Admin 监控其他应用了,但是这不是我们的重点,详细信息请参考官网文档:http://codecentric.github.io/spring-boot-admin/2.0.0/,我们的重点是在 Spring Cloud 中使用 Spring Boot Admin 监控 Spring Cloud 的服务,下面我们就详细的讲解在 Spring Cloud 中搭建 Spring Boot Admin

(三)在 Spring Cloud 中基于 Eureka 的 Spring Boot Admin 的搭建
3.1 启动之前的项目 eureka server,端口8761
3.2 新建 module(springboot-admin),创建步骤参考上篇
3.2.1 添加相关依赖,pom文件:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
由于项目中使用的是 spring boot 2.0 版本,所以这里要使用 spring boot admin 的最新版本(还未正式发布),本人测试使用 spring boot admin 2.0.0 版本会有问题。

3.2.2 启动类添加注解
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminApplication {

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

@Profile("insecure")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()//
.and().csrf().disable();
}
}

@Profile("secure")
@Configuration
public static 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
}
}
}
SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方给的配置,是对 url 进行安全认证等配置,照着配置即可。@EnableEurekaClient 注解是把 Spring Boot Admin 注册到 Eureka 里,这样 Spring Boot Admin 就可以发现注册到 Eureka 里的其他服务实例,@EnableAdminServer 注解是开启监控功能。

3.2.3 配置文件
官方有给出示例,主要是配置 eureka 的地址,这里要强调说明的一点,由于 Spring Boot 2.0 的 Actuator 只暴露了 /health、/info 两个端口(为了安全考虑),所以要配置 management.endpoints.web.exposure.include 的属性,下面的配置文件中暴力了一点,配置暴露了所有的端点,由于 Spring Boot Admin 有 web UI 管理界面,配置了登录的用户名密码如下,使用了 security.user 的属性,其他的详细配置,可以查看官方文档。

spring:
application:
name: spring-boot-admin
profiles:
active:
- secure
server:
port: 8788

# tag::configuration-eureka[]
eureka: #<1>
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management:
endpoints:
web:
exposure:
include: "*" #<2>
endpoint:
health:
show-details: ALWAYS
# end::configuration-eureka[]

---
spring:
profiles: insecure

---
spring:
profiles: secure
security:
user:
name: "user"
password: "password"
eureka:
instance:
metadata-map:
user.name: "user" #These two are needed so that the server
user.password: "password" #can access the protected client endpoints
3.2.4 启动 spring boot admin 服务,界面如下:

用户名密码即上面的配置,user/password登录成功后

此时由于 Eureka 里只有 Spring Boot Admin 自身已注册,所以其监控列表里只有自己,下面我们启动其他的服务,让其注册到 Eureka 里。

3.2.5 启动 spring-demo-service 服务
(之前文章里现有的服务,可以查找此前的文章,或者查看文末的源码下载),在启动前,还有一处要配置,就是在 3.2.3 里说的,Actuator 在 spring boot 2.0 版本后,只暴露了两个端点,所以此时启动,监控不到所需的信息,下面修改配置文件如下:

server:
port: 8281

eureka:
client:
serviceUrl:
# 向每个注册中心注册
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
spring:
application:
name: spring-demo-service

management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
此时启动 spring-demo-service,发现监控列表里 spring-demo-service 已经有了

点击 SPRING-DEMO-SERVICE 进入,页面如下,可以看到有我们之前介绍的一些功能。

3.2.6 其他的配置
如果想要显示版本信息,配置文件中加入 info.version=1.0.0 可以配置版本信息

源码下载:https://github.com/shmilyah/spring-cloud-componets

Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)的更多相关文章

  1. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  2. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  3. spring事务管理(详解和实例)

    原文地址: 参考地址:https://blog.csdn.net/yuanlaishini2010/article/details/45792069 写这篇博客之前我首先读了<Spring in ...

  4. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client

  5. Spring Boot异常处理详解

    在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...

  6. 02Spring Boot配置文件详解

    02Spring Boot配置文件详解 文章指导 学习笔记 学习代码 自定义属性 在src/main/java/resources目录下创建一个application.properties或appli ...

  7. Spring Security Filter详解

    Spring Security Filter详解 汇总 Filter 作用 DelegatingFilterProxy Spring Security基于这个Filter建立拦截机制 Abstract ...

  8. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  9. Spring jar包详解

    Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...

随机推荐

  1. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. 本地手动一步步搭建WNMP环境(nginx+php+mysql) Windows平台

    环境:Windows 10 x64 参考文章: WNMP完整教程      windows下PHP环境的搭建 我自定义安装后的目录结构: +WNMP ++MySQL_Server-8.0.13 ++n ...

  3. ZJOI 2009 多米诺骨牌(状态压缩+轮廓线+容斥)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1435 思路 一道很好的状压/容斥题,涵盖了很多比较重要的知识点. 我们称每两行间均有纵跨.每 ...

  4. [翻译 EF Core in Action 2.4] 加载相关数据

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  5. Knative Serving 进阶: Knative Serving SDK 开发实践

    作者 | 阿里云智能事业群技术专家 牛秋霖(冬岛) 导读:通过前面的一系列文章你已经知道如何基于 kubectl 来操作 Knative 的各种资源.但是如果想要在项目中集成 Knative 仅仅使用 ...

  6. Java连载15-boolean类型&类型转换&++运算符

    一.boolean类型 1.说明: (1)在java语言中,boolean类型只有两个值:true.false,没有其他的值.在C语言中,是有0代表false和1代表true的 (2)在底层存储的时候 ...

  7. Docker底层原理介绍

    1.docker介绍 1.1什么是docker Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻 ...

  8. Rabbit MQ 学习参考

    网上的教程虽然多,但是提供demo的比较少,或者没有详细的说明,因此,本人就照着网上的教程做了几个demo,并把代码托管在码云,供有需要的参考. 项目地址:https://gitee.com/dhcl ...

  9. 基于串口的SD_card系统

    概述 基于串口的SD_card系统1, 扫描文件:2, 新建文件:3, 删除文件:4, 写入文件:5, 读取文件. 整个文件系统的串口通信方式都是ASC通信方式. 文件系统分为简单实用方式和专业使用方 ...

  10. el-upload进度条无效,on-progress无效问题解决方案

    事先声明,本人系.net后端老菜鸟,vue接触没有多长时间,如果存在技术分享错误,切莫见怪,第一次写博,还请大佬们多多担待,转载请注明出处谢谢! 最近项目用到饿了么上传,于是参照官网接入el-uplo ...