【SpringBoot】51、Spring-Boot-Admin搭建服务监控系统
一、写在前面
Spring Boot Actuator 是 spring-boot 自带监控功能 ,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean 加载情况、环境变量、日志信息、线程信息等。
Spring Boot Admin是一个针对 spring-boot 的 actuator 接口进行 UI 美化封装的监控工具。他可以:在列表中浏览所有被监控 spring-boot 项目的基本信息,详细的Health信息、内存信息、JVM 信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改 logger 的 level。
二、搭建监控服务中心
1、引入 maven 依赖
<!-- SpringBoot Admin -->
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.5.3</version>
</dependency>
<!-- web支持 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring security -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
- spring-boot-admin-starter-server,是监控中心所需要的依赖
- spring-boot-starter-security,用于开启登录认证,即开启用户名密码登录监控中心
2、启动类
package com.asurplus.cloud;import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableAdminServer
public class MonitorServer {public static void main(String[] args) {SpringApplication.run(MonitorServer7001.class, args);}
}
- @EnableAdminServer 用户开启监控服务中心,表明自己是一个服务注册中心
3、YML 配置文件
server:port: 7001spring:# 安全认证security:user:# 账号name: admin# 密码password: 123456boot:admin:ui:# 网页标题title: Asurplus服务监控
4、监控权限配置
package com.asurplus.cloud.config;import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;/*** 监控权限配置**/
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {/*** 上下文路径*/private final String contextPath;public WebSecurityConfigurer(AdminServerProperties adminServerProperties) {this.contextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(contextPath + "/");http.headers().frameOptions().disable().and().authorizeRequests().antMatchers(contextPath + "/assets/**", contextPath + "/login", contextPath + "/actuator/**", contextPath + "/instances/**").permitAll().anyRequest().authenticated().and().formLogin().loginPage(contextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(contextPath + "/logout").and().httpBasic().and().csrf().disable();}
}
至此,监控中心配置完成,启动该服务,访问:http://localhost:7001
http://localhost:7001
就会看到如下界面:
使用我们配置的账户密码登录
注册进我们的监控服务中心的实例,我们就能在这儿看到,当然现在还没有实例注册
三、注册进入监控服务中心
1、新建一个工程,引入 maven 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Admin Client -->
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>${spring-boot-admin.version}</version>
</dependency>
<!-- SpringBoot Actuator -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- spring-boot-admin-starter-client,是客户端需要的依赖
- spring-boot-starter-actuator,用来获取项目的监控信息
2、启动类
package com.asurplus.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
public class MonitorClient {public static void main(String[] args) {SpringApplication.run(MonitorClient.class, args);}
}
启动类,没有特殊配置
3、YML 配置文件
server:port: 9001spring:boot:admin:client:# 注册中心地址url:- http://localhost:7001application:name: monitor-client# 暴露我们的所有监控信息
management:endpoints:web:exposure:include: '*'
我们的端口为 9001,注册进入我们的监控服务中心,http://localhost:7001,可以看出这是一个数组,表示可以同时注册进入多个监控服务中心
接下来,我们启动我们的 9001 项目,访问 http://localhost:7001
可以看到,有一台实例注册进入了监控服务中心,我们点击它,就能看到关于这台实例的监控信息
四、实时日志
1、在客户端工程中配置日志文件名称
logging:file:name: logs/${spring.application.name}/info.log
2、编写访问接口
package com.asurplus.cloud.controller;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {private static final Logger log = LoggerFactory.getLogger(TestController.class);@Value("${server.port}")private String port;@GetMapping("init")public String init() {log.info("这是实时日志");return port;}
}
重新启动我们的项目,访问:http://localhost:9001/init
http://localhost:9001/init
我们就能看到了实时日志
Spring-Boot-Admin 还支持动态修改日志级别,其他的自己琢磨琢磨吧!!!
如您在阅读中发现不足,欢迎留言!!!
【SpringBoot】51、Spring-Boot-Admin搭建服务监控系统相关推荐
- 如何做自己的服务监控?spring boot 2.x服务监控揭秘
Actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...
- MOSS 代替Spring Boot Admin 的服务治理工具
1.1 什么是服务治理 服务治理,我也称之为微服务治理,是指用来管理微服务的整个生命周期.包括应用的创建,服务名的规范,服务的上下线,服务的迁移,整个服务的生老病死等方方面面的治理. 1.2 Moss ...
- Spring Boot + ELK搭建日志监控框架
Spring Boot + ELK搭建日志监控框架 准备ELK三件套 Elasticsearch+Logstash+Kibana 下载地址:https://www.elastic.co/cn/ ...
- 如何做自己的服务监控?spring boot 1.x服务监控揭秘
1.准备 下载可运行程序:http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/ 2.添加服务监控依赖 <d ...
- Spring boot + Jsoup 搭建高清视频解析系统接口只需1分钟
点击上方"码农突围",马上关注 这里是码农充电第一站,回复"666",获取一份专属大礼包 真爱,请设置"星标"或点个"在看&quo ...
- Spring boot admin 监控配置
1.项目背景 项目开发完成并部署上线,系统正式进入试运行:在试运行期间由于客户服务问题导致部分服务不可用,幸亏系统采用集群架构没有造成系统正常使用,但该问题存在系统风险,问题出现后没被第一时间发现和处 ...
- Spring Boot Admin:微服务应用监控
摘要 Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍. Spring Boot Admin 简介 ...
- 高级版的 jvisualvm :Spring Boot Admin 监控 Spring Boot 微服务项目
前奏:先说一下 Java VisualVM Java VisualVM 是一个能够监控 JVM 的 jdk 自带的图形化工具: 在 $JAVA_HOME/bin 目录下,可直接运行它. 要想监控远程服 ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
点赞再看,动力无限.Hello world : ) 微信搜「 程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 1. ...
最新文章
- linux kprobe rootkit 简介
- 如何在Mac中卸载openjdk15
- VS中查看子类对象内存分布的方法
- Oracle PCTfree assm,Oracle 段空间管理方式与PCTFREE和PCTUSED的概念
- shell脚本if中判断大于、小于、等于、不等于的符号
- mysql hive 安装 配置_hive 安装配置部署与测试
- 初步认识泊松重建(比较全的综合教程)
- EVE-NG之Cisco FirePower 系统
- C#开发的3D图表控件,适用于winform项目
- IDEA API文档的导出方式
- Ext.grid.EditorGridPanel使用方法
- 【单片机竞赛:共阳数码管静态控制】
- python实现图片嗅探工具——自编driftnet
- 网络七层及四层协议通俗详解
- 遥感数据集的下载记录——MODIS产品为主
- 在线图片转文字怎么操作?
- APA系统中超声波雷达的安装调试使用说明
- 很齐全的怀孕须知.不认真看不配做妈妈.
- 老虎证券赴美IPO:3年交易破万亿,5年9轮融资“众星捧月”...
- 强制旋转iPhone界面
热门文章
- 用FPGA实现尼康绝对值编码器的数据读取
- CVE-2014-4113:飓风熊猫(HURRICANE PANDA)Win64bit提起权0day破绽
- 吴金贵有望二次助教国足 成顶替刘春明热门人选
- 解决win7电脑无法打开此计算机组策略对象的问题
- P2P DHT sp
- 单目标跟踪通过CAM绘制heatmap图像(以SiamCAR为例)
- 柴达木光伏+农业跨界融合新态势
- 三、IDEA更换主题皮肤
- springboot+vue3物资管理系统实战
- python获取网站代码_python爬虫1——获取网站源代码(豆瓣图书top250信息)