原文: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. SQL Server 迁移数据库 (二)分离和附加

    分离和附加其实比导入和导出,步骤要少一些,但是数据量大的话,跨服务器拷贝数据文件可能要慢一些 1. 分离数据库 这里最好选择断开链接,断开之前要确保你记得数据库的路径,一般默认都是C:\Program ...

  2. [LeetCode] 890. Find and Replace Pattern 查找和替换模式

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  3. [LeetCode] 486. Predict the Winner 预测赢家

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  4. shell之startup

    #!/bin/sh # # # # PROJECT=$ APPWORK_DIR=~/apps/$PROJECT LOGPATH=~/logs/$ LOGFILE=~/logs/$PROJECT/${P ...

  5. 2019 SDN上机第2次作业

    1.利用mininet创建如下拓扑,要求拓扑支持OpenFlow 1.3协议,主机名.交换机名以及端口对应正确,请给出拓扑Mininet执行结果,展示端口连接情况 1.1拓扑 1.2 代码 #!/us ...

  6. 使用Docker构建Jekyll框架网站

    使用Docker构建Jekyll框架网站 使用dockerfile构建apache + jekyll 目录 Jekyll基础镜像 构建Jekyll基础镜像 Apache镜像 构建Jekyll Apac ...

  7. linux kernel debug

    1. sysrq http://www.chinaunix.net/old_jh/4/902287.html 常用的SysRq命令(序列)  重启机器的SysRq命令序列是 k(SAK) s(sync ...

  8. java跳出循环break;return;continue使用

    for(int i=0;i<5;i++){ if(i==2){ System.out.println("i==2时忽略了"); continue;//忽略i==2时的循环 } ...

  9. redux-thunk形式

    onClick(e) { e.preventDefault(); const { user, pass } = this.refs; this.props.dispatch(login(user.va ...

  10. 【ELK】elasticsearch使用bulk 导入批量的数据集文件报错:Validation Failed: 1: no requests added

    执行命令如下: curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty --data-binary @/cjf/es/e ...