参考:Spring Boot Admin 2.0 上手

Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI,提供如下功能:

  • 显示 name/id 和版本号
  • 显示在线状态
  • Logging 日志级别管理
  • JMX beans 管理
  • Threads 会话和线程管理
  • Trace 应用请求跟踪
  • 应用运行参数信息,如:
    • Java 系统属性
    • Java 环境变量属性
    • 内存信息
    • Spring 环境属性

使用Spring Boot Admin监控Spring Cloud微服务

首先需要一个服务注册中心Eureka Server,让Spring Boot Admin(以下简称 SBA)服务端向它注册服务。

服务端

在主Maven工程下新建Moeule工程admin-server,pom文件引入依赖。其中SBA的依赖必需在dependency中指定version版本号(包括server和client),用spring boot initializer自动建工程,引入依赖时,版本号会写在properties中,<spring-boot-admin.version>2.0.1</spring-boot-admin.version>,而在我的测试中,这样并不能起作用,不能引入依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.cralor</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>admin-server</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.cralor</groupId>
<artifactId>chap12-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类加上注解@EnableAdminServer

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

配置文件配置端口号、程序名、服务注册中心地址。

server:
port: 5000
spring:
application:
name: admin-server
eureka:
client:
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

客户端

在建立了我们的服务端之后,我们可以将一个 Spring Boot 应用程序注册为客户端。注册客户端有两种方式,一种就是通过引入 SBA Client,另外一种是基于 Spring Cloud Discovery。我们这里先介绍通过引入 SBA Client 的方式。

SBA Client

新建工程admin-client。pom文件引入依赖,客户端需要引入actuator依赖,服务端不需要。

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置文件:SBA服务端http://localhost:5000,actuator监控暴露全部端口,显示所有信息。

server:
port: 8762
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:5000
eureka:
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率,默认为30秒
health-check-url-path: /actuator/health #健康检查的地址(依赖spring-boot-starter-actuator)
client:
registryFetchIntervalSeconds: 5 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS

依次启动eureka-server、admin-server和admin-client(两个客户端8762、8763)。访问SBA服务端http://localhost:5000

Wallboard:

Applications:

Journal:

Instance details:

Spring Cloud Discovery

如果我们的项目中使用了 Spring Cloud,那么我们其实并不用通过 SBA Client 来向 SBA 注册,而是让 SBA 通过注册中心(Eureka、Consul 等)来发现服务。这里以 Eureka 作为注册中心来说明。

在admin-client中只需要改变引入的依赖,其他不需要改变。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <!--<dependency>-->
<!--<groupId>de.codecentric</groupId>-->
<!--<artifactId>spring-boot-admin-starter-client</artifactId>-->
<!--<version>2.0.1</version>-->
<!--</dependency>--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

重启 SBA 服务端和客户端,就能看的和使用 SBA Client 一样的效果了。

在Spring Boot Admin中集成Turbine

因为SBA最新版本为2.0.1,而spring-boot-admin-server-ui-turbine的版本为1.5.7,整合会出现问题,暂时不做测试。。。

安全配置,在Spring Boot Admin中添加安全登陆界面

SBA 服务端可以访问客户端的敏感端点,因此手册上 建议我们应该为服务端和客户端添加一些安全配置。

服务端的安全配置

向服务端添加 Spring Security 依赖

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

配置文件设置账号密码,并添加一些配置

server:
port: 5000
spring:
application:
name: admin-server
security:
user:
name: 'admin'
password: 'admin'
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password} management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS

新增一个安全配置类。

@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:5000 就会发现需要认证了。

如果这时你的客户端是使用的 SBA Client 的方式,你会注意到客户端这时已无法再注册到服务端了(Spring Cloud Discovery 的话不受影响)。为了能将客户端注册到服务端,我们还必须在客户端的配置文件中添加以下内容:

spring.boot.admin.client:
username: "admin"
password: "admin"

因为 SBA 客户端的注册方式有两种,所以在客户端的安全配置上也是分为了两种。下面我们来为客户端增加安全配置。

客户端的安全配置

SBA Client

客户端的配置文件

server:
port: 8762
spring:
application:
name: admin-client
security:
user:
name: "client"
password: "client"
boot:
admin:
client:
url: http://localhost:5000
username: "admin"
password: "admin"
instance:
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
eureka:
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率,默认为30秒
health-check-url-path: /actuator/health #健康检查的地址(依赖spring-boot-starter-actuator)
client:
registryFetchIntervalSeconds: 5 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS

重启 SBA Server 和 Clients 可看到效果。

Spring Cloud Discovery

配置文件

server:
port: 8762
spring:
application:
name: admin-client
security:
user:
name: "client"
password: "client" eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS

重启 SBA Server 和 Clients 。

(ps:在我的测试中,SBA client和Spring Cloud Discovery,不管用这两个配置文件的哪一个都可以...)

Eureka 的 metadataMap

Eureka 中的 metadataMap 是专门用来存放一些自定义的数据,当注册中心或者其他服务需要此服务的某些配置时可以在 metadataMap 里取。实际上,每个 instance 都有各自的 metadataMap,map 中存放着需要用到的属性。例如,上面配置中的 eureka.instance.metadata-map.user.name,当这个服务成功注册到 Eureka 上,Spring Boot Admin 就会取拿到这个 instance,进而拿到 metadataMap 里的属性,然后放入请求头,向此服务发送请求,访问此服务的 Actuator 开放的端点。

关于 SBA 的更多认证方式可以参见 joshiste/spring-boot-admin-samples

案例代码地址:https://github.com/cralor7/springcloud

spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin的更多相关文章

  1. SpringCloud(8)微服务监控Spring Boot Admin

    1.简介 Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件.Spring Boot Admin 分为 Server 端和 Client 端,Spring ...

  2. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  3. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  4. 《Spring Cloud与Docker微服务架构实战》配套代码

    不才写了本使用Spring Cloud玩转微服务架构的书,书名是<Spring Cloud与Docker微服务架构实战> - 周立,已于2017-01-12交稿.不少朋友想先看看源码,现将 ...

  5. Java微服务之Spring Boot on Docker

    本文学习前提:Java, Spring Boot, Docker, Spring Cloud 一.准备工作 1.1 安装Docker环境 这一部分请参考我的另一篇文章<ASP.NET Core ...

  6. SpringCloud(9)使用Spring Cloud OAuth2保护微服务系统

    一.简介 OAth2是一个标准的授权协议. 在认证与授权的过程中,主要包含以下3种角色. 服务提供方 Authorization Server. 资源持有者 Resource Server. 客户端 ...

  7. 微服务与Spring Cloud概述

    微服务与Spring Cloud随着互联网的快速发展, 云计算近十年也得到蓬勃发展, 企业的IT环境和IT架构也逐渐在发生变革,从过去的单体应用架构发展为至今广泛流行的微服务架构. 微服务是一种架构风 ...

  8. 构建微服务:Spring boot

    构建微服务:Spring boot 在上篇文章构建微服务:Spring boot 提高篇中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jp ...

  9. 微服务下 Spring Boot Maven 工程依赖关系管理

    单体 Spring Boot Maven 工程 最基本的 pom.xml 包含工程信息.Spring Boot 父工程.属性配置.依赖包.构建插件 <?xml version="1.0 ...

随机推荐

  1. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  2. 2018-2019-20172329 《Java软件结构与数据结构》第四周学习总结

    2018-2019-20172329 <Java软件结构与数据结构>第四周学习总结 经过这样一个国庆节的假期,心中只有一个想法,这个国庆假期放的,不如不放呢!! 教材学习内容总结 < ...

  3. python struct详解

    转载:https://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 有的时候需要用python处理二进制数据,比如,存取文件,socket操 ...

  4. 让程序运行更加面向用户——电梯V2.1

    电梯V2.1 GitHub仓库地址 Problem 为程序添加命令行参数(自行利用搜索引擎进行学习). 写成 .cpp .h 文件分离的形式(大多数同学已经达到). 继续完善函数分离.模块化思想. 要 ...

  5. iOS-封装UIPickerView

    创建类WJPickerView继承与UIView ProvinceModel是省市的model,包含属性 @property (nonatomic, strong) NSString *provinc ...

  6. Mac10.11.2 Apache 服务配置

    系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它.它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它 ...

  7. mysql(五)查询缓存

    mysql的逻辑架构图如下: 当开启查询缓存时,mysql会将查询结果缓存到查询缓存区域,结果对应的key是使用查询语句,数据库名称,客户端协议的版本等因素算出的一个hash值. 在下次查询时,根据一 ...

  8. 第111天:Ajax之jQuery实现方法

    由于jQuery中的Ajax方法是用了内置的deferred模块,是Promise模式的一种实现,而我们这里没有讲过,所以我们就不使用这一模式啦. 我们只定义一个Ajax方法,他可以简单的get,po ...

  9. 后缀树的线性在线构建-Ukkonen算法

    Ukkonen算法是一个非常直观的算法,其思想精妙之处在于不断加字符的过程中,用字符串上的一段区间来表示一条边,并且自动扩展,在需要的时候把边分裂.使用这个算法的好处在于它非常好写,代码很短,并且它是 ...

  10. AtCoder Grand Contest 019 B: Reverse and Compare

    题意: 给出一个字符串,你可以选择一个长度大于等于1的子串进行翻转,也可以什么都不做.只能翻转最多一次. 问所有不同的操作方式得到的字符串中有多少个是本质不同的. 分析 tourist的题妙妙啊. 首 ...