文章目录
前言
与大部分应用和系统一样,SpringBoot 微服务的开发、发布与部署只占其生命周期的一小部分,应用和系统运维才是重中之重。而运维过程中,监控工作更是占据重要位置。
运维的目的之一是为了保证系统的平稳运行,进而保障公司业务能持续对外服务,为了达到这一目的,我们需要对系统的状态进行持续地观测,以期望一有风吹草动就能发现并作出应对,监控作为一种手段,就是以此为生。
一、监控方案
- spring-boot-admin 轻量级
在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot应用程序的一个简单的界面
提供如下功能:
显示 name/id 和版本号
显示在线状态
Logging日志级别管理
JMX beans管理
Threads会话和线程管理
Trace应用请求跟踪
应用运行参数信息,如: Java 系统属性、 Java 环境变量属性、 内存信息、Spring 环境属性
- Prometheus+Grafana 相对较重,配置较为麻烦,但灵活性好
二、spring-boot-admin集成
1.引入actuator监控
在每一个需要监控的服务pom中添加监控依赖包
<!-- 引入Actuator监控依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--这个包是作图形化服务端监控客户端:如果不用图形化客户端就不要导这个包-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.0</version>
</dependency>
2. 配置yml
在每一个需要监控的服务yml中添加检查
# 服务端点检查
management:
trace:
http:
enabled: true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
3. 创建监控服务
3.1 pom引入
security 引入用于admin的登录模块,mail引入实现预警发邮件
<dependencies>
<!--Spring Boot Admin Server监控服务端-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--需要与boot版本对应-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--邮件模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
3.2 yml配置
server:
port: 9111
spring:
boot:
admin:
ui:
title: Cloud监控中心
client:
instance:
metadata:
tags:
environment: local
notify:
mail:
from: 991446772@qq.com
to: 991446772@qq.com
security:
user:
name: "admin"
password: "admin"
application:
name: cloud-ali-monitor
cloud:
nacos:
discovery:
server-addr: @config.server-addr@
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
namespace: @config.namespace@
mail:
host: smtp.qq.com
username: 991446772@qq.com
password: ??
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
# 服务端点检查
management:
trace:
http:
enabled: true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
3.3 SecuritySecureConfig
@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 {
// 登录成功处理类
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
//静态文件允许访问
.antMatchers(adminContextPath + "/assets/**").permitAll()
//登录页面允许访问
.antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
//其他所有请求需要登录
.anyRequest().authenticated()
.and()
//登录页面配置,用于替换security默认页面
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
//登出页面配置,用于替换security默认页面
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/instances",
"/actuator/**"
);
}
}
3.4 启动类
添加注解@EnableAdminServer
@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class);
}
}