原文: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. Day01 确定选题

    一起来选题 一.谁想个选题? 今天是第一节大软课,大家需要进行分组和确定选题.分组固然是快乐的,但是确定选题是让人费脑筋的.要新颖!要有需求!要我们能实现(笑)......大家面面相觑.面对这种情况, ...

  2. Codeforces Global Round 2 D 差分 + 前缀和 + 二分

    https://codeforces.com/contest/1119/problem/D 题意 有n个数组,每个数组大小为\(10^{18}+1\)且为等差数列,给出n个数组的\(s[i]\),q次 ...

  3. 2016年蓝桥别A组模拟训练

    1. 网友年龄 某君新认识一网友. 当问及年龄时,他的网友说: “我的年龄是个2位数,我比儿子大27岁, 如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄” 请你计算:网友的年龄一共有多少种可能 ...

  4. Asp.Net Core 减少Controller获取重复注入对象

    原文:Asp.Net Core 减少Controller获取重复注入对象 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012770274/art ...

  5. 2019 SDN上机第1次作业

    一.安装轻量级网络仿真工具Mininet 克隆github上的Mininet源 git clone https://github.com/mininet/mininet 选择默认全部安装 cd min ...

  6. C++ 中文乱码的问题

    乱码的根本原因就是字符串编码的方式也字符串解码方式不一致导致的, 而在我们平常用的编码编码方式一般都是utf-8以gbk之间的相互转换, 下面给出编码方式的转换代码 string UtfToStrin ...

  7. EPPlus.Core 处理 Excel 报错之天坑 WPS

    最近工作中常常有有数据处理的需求,一个Excel动不动就是上十万的数据量,在用 EPPlus.Core 导入数据入库的时候遇到了一个莫名其妙的问题 The given key 'rId2' was n ...

  8. [数据库] SQL 语法之进阶篇

    一.创建计算字段 下面介绍什么是计算字段,如何创建计算字段,以及如何从应用程序中使用别名引用它们. 1.1 计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式,下面举几个例子. 需要显示公司 ...

  9. Mysql 错误 ERROR 1 (HY000) at line 1: Can't create/write to file '/home/kaizenly/cfg_dict.csv' (Errcode: 13 - Permission denied)

    [1]问题描述 (1)执行SQL语句: use billing; select * from cfg_dict into outfile '/home/kaizenly/cfg_dict.csv' f ...

  10. 关于优秀的视频播放器 - PotPlayer

    播放器设置 直接截图: 其他重要功能 1. 切换语音:Alt + A 谢谢浏览!