Spring Boot Admin:微服务应用监控
摘要
Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。
Spring Boot Admin 简介
SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。
Spring Boot Admin 可以提供应用的以下监控信息:
监控应用运行过程中的概览信息;
度量指标信息,比如JVM、Tomcat及进程信息;
环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
查看所有创建的Bean信息;
查看应用中的所有配置信息;
查看应用运行日志信息;
查看JVM信息;
查看可以访问的Web端点;
查看HTTP跟踪信息。
创建admin-server模块
这里我们创建一个admin-server模块来作为监控中心演示其功能。
- 在pom.xml中添加相关依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
- 在application.yml中进行配置:
spring:application:name: admin-server
server:port: 9301
- 在启动类上添加@EnableAdminServer来启用admin-server功能:
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}
创建admin-client模块
这里我们创建一个admin-client模块作为客户端注册到admin-server。
- 在pom.xml中添加相关依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
- 在application.yml中进行配置:
spring:application:name: admin-clientboot:admin:client:url: http://localhost:9301 #配置admin-server地址
server:port: 9305
management:endpoints:web:exposure:include: '*'endpoint:health:show-details: always
logging:file: admin-client.log #添加开启admin的日志监控
监控信息演示
- 访问如下地址打开Spring Boot Admin的主页:http://localhost:9301
- 点击wallboard按钮,选择admin-client查看监控信息;
- 监控信息概览;
- 度量指标信息,比如JVM、Tomcat及进程信息;
- 环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
- 查看所有创建的Bean信息;
- 查看应用中的所有配置信息;
- 查看日志信息,需要添加以下配置才能开启;
logging:file: admin-client.log #添加开启admin的日志监控
查看JVM信息;
查看可以访问的Web端点;
查看HTTP跟踪信息;
结合注册中心使用
Spring Boot Admin结合Spring Cloud 注册中心使用,只需将admin-server和注册中心整合即可,admin-server 会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。
修改admin-server
- 在pom.xml中添加相关依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在application-eureka.yml中进行配置,只需添加注册中心配置即可:
spring:application:name: admin-server
server:port: 9301
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
- 在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {public static void main(String[] args) {SpringApplication.run(AdminServerApplication.class, args);}}
修改admin-client
- 在pom.xml中添加相关依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在application-eureka.yml中进行配置,删除原来的admin-server地址配置,添加注册中心配置即可:
spring:application:name: admin-client
server:port: 9305
management:endpoints:web:exposure:include: '*'endpoint:health:show-details: always
logging:file: admin-client.log #添加开启admin的日志监控
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
- 在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {public static void main(String[] args) {SpringApplication.run(AdminClientApplication.class, args);}}
功能演示
- 启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;
- 查看注册中心发现服务均已注册:http://localhost:8001/
- 查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
添加登录认证
我们可以通过给admin-server添加Spring Security支持来获得登录认证功能。
创建admin-security-server模块
- 在pom.xml中添加相关依赖:
<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.1.5</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 在application.yml中进行配置,配置登录用户名和密码,忽略admin-security-server的监控信息:
spring:application:name: admin-security-serversecurity: # 配置登录用户名和密码user:name: macropassword: 123456boot: # 不显示admin-security-server的监控信息admin:discovery:ignored-services: ${spring.application.name}
server:port: 9301
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
- 对SpringSecurity进行配置,以便admin-client可以注册:
/*** Created by macro on 2019/9/30.*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(adminContextPath + "/");http.authorizeRequests()//1.配置所有静态资源和登录页可以公开访问.antMatchers(adminContextPath + "/assets/**").permitAll().antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and()//2.配置登录和登出路径.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath + "/logout").and()//3.开启http basic支持,admin-client注册时需要使用.httpBasic().and().csrf()//4.开启基于cookie的csrf保护.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())//5.忽略这些路径的csrf保护以便admin-client注册.ignoringAntMatchers(adminContextPath + "/instances",adminContextPath + "/actuator/**");}
}
- 修改启动类,开启AdminServer及注册发现功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {public static void main(String[] args) {SpringApplication.run(AdminSecurityServerApplication.class, args);}}
- 启动eureka-server,admin-security-server,访问Spring Boot Admin 主页发现需要登录才能访问:http://localhost:9301
使用到的模块
springcloud-learning
├── eureka-server -- eureka注册中心
├── admin-server -- admin监控中心服务
├── admin-client -- admin监控中心监控的应用服务
└── admin-security-server -- 带登录认证的admin监控中心服务
项目源码地址
https://github.com/macrozheng/springcloud-learning
Spring Boot Admin:微服务应用监控相关推荐
- MOSS 代替Spring Boot Admin 的服务治理工具
1.1 什么是服务治理 服务治理,我也称之为微服务治理,是指用来管理微服务的整个生命周期.包括应用的创建,服务名的规范,服务的上下线,服务的迁移,整个服务的生老病死等方方面面的治理. 1.2 Moss ...
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
- 一文透析 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
- Spring Cloud与微服务学习总结(8)——Spring Boot、微服务架构和大数据治理三者之间的故事
前言 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况下,如 ...
- Spring微服务实战第2章 使用Spring Boot构建微服务
第2章 使用Spring Boot构建微服务 基于微服务的架构具有以下特点. 有约束的--微服务具有范围有限的单一职责集.微服务遵循UNIX的理念,即应用程序是服务的集合,每个服务只做一件事,并只做好 ...
- 使用Spring Boot构建微服务(文末福利)
本文主要内容 学习微服务的关键特征 了解微服务是如何适应云架构的 将业务领域分解成一组微服务 使用Spring Boot实现简单的微服务 掌握基于微服务架构构建应用程序的视角 学习什么时候不应该使用微 ...
- Spring boot、微服务、OAuth、OpenID的爱恨情仇!
在本文中,我们学习如何使用Spring boot轻松配置和部署微服务,然后使用OAuth和OpenID保护它们. 在微服务体系架构中,其中较大的应用程序由多个较小的服务组成,每个服务都有自己的目标,它 ...
- 一文读懂spring boot 和微服务的关系
欢迎访问网易云社区,了解更多网易技术产品运营经验. Spring Boot 和微服务没关系, Java 微服务治理框架普遍用的是 Spring Cloud. Spring Boot 产生的背景,是开发 ...
- Spring Boot学习总结(12)——Spring Boot Admin 2.0应用监控示例
Spring Boot Admin 2.0新特性 Spring Boot Admin 2.0 变化还是挺多的,具体参考 官网说明,这里列几条主要的:使用Vue.js重写了UI界面,漂亮得不像实力派 : ...
- Spring Boot Admin 基于security 认证监控
首先介绍一下Spring Boot Admin是用来干嘛的,它用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI. ...
最新文章
- 独家 | 一文盘点AutoML 库(附PPT等链接)
- 教你用Python表达母亲节的祝福
- C#多线程学习(二) 如何操纵一个线程
- php 值是否在数组里面,php怎么判断某值在不在数组中
- 「offer来了」快来关注这些性能优化问题
- 瀑布流插件|jquery.masonry|使用demo
- JSP毕业设计源码带论文和答辩、大作业、实例程序源码下载合集【10套】
- 解决There is no getter for property named ‘organization_id‘ in的报错
- ctf比赛/学习资源整理,记得收藏!
- alert的确定和取消
- android 半透明裁剪框 截取图片 头像
- What are 20 questions to detect fake data scientists?
- 红豆薏米的祛湿效果到底好不好?
- 微信小程序接入微信支付流程
- 解决-Dmaven.multiModuleProjectDirectory system property is not set
- 【conda解决】安装Torchvision后使用PIL报错,ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'
- 应用白名单:方法与挑战
- Exception: D:\Unity\Editor\Data\il2cpp/build/il2cpp.exe did not run properly!
- pandas 讲清楚pivot_table和margins
- powerpoint预览_如何禁用或删除PowerPoint动画
热门文章
- webservice中jaxws:server 和jaxws:endpoint的区别
- 在 C# 中,new 关键字可用作运算符、修饰符或约束。
- 解决KMP看高清电影背景声大说话声小问题
- ios图片放大之后如何不模糊_图片怎样放大后不模糊 图片放大不失真的方法步骤...
- docker 安装oracle_rancher安装oracle 11g
- mysql illegal mix of_mysql字符集问题:Illegal mix of collations
- 二分类最优阈值确定_分类模型评价标准,AUC还是Macro F1?
- Linux部署动态网页,Nginx发布支持动态配置的开源Web服务器
- pb 哪里找到系统图标_win10电脑桌面上“回收站”和“此电脑”图标不见了怎么办...
- IDEA2021创建Java Web项目