‘
Spring Boot Admin
Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序,它在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。
为什么需要Spring Boot Admin
在Spring Boot Admin之前,我们知道可以使用 Spring Boot Actuator 对项目的健康状况进行监控,但都只是以一些文字表述性的形式展示,可以说信息并不美观也并不便捷,而Spring Boot Admin可以说是Spring Boot Actuator的可视化版本,可以结合Spring Boot Actuator向用户展示更多可视化监控信息(如系统压力、QPS、CPU、内存、日活等)
Spring Boot Actuator传统监控信息展示:
整合Spring Boot Admin
Spring Boot Admin将系统分成两种角色:
- Spring Boot Admin Server: 监控服务端,用于展示监控数据
- Spring Boot Admin Client: 被监控的客户端
被监控的客户端通过向监控服务端注册(HTTP),即可让服务端展示相关监控信息但是,如果系统内集成了SC即注册中心,则只需要向注册中心注册即可。由于项目内整合了Eureka注册中心,我们选择第二种模式。
构建监控服务端
第一步:引依赖。新建一个工程:system-admin-server,引入Spring Boot Admin Server依赖
<?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">
<parent>
<artifactId>spring-cloud-system</artifactId>
<groupId>com.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>system-admin-server</artifactId>
<description>系统可视化监控服务</description>
<dependencies>
<!--admin服务-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<!--admin服务可视化界面-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
</dependency>
<!--eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--集成security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!-- 添加spring-boot的maven插件,不能少,打jar包时得用 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:加配置;在system-admin-server的项目配置文件bootstrap.yml中添加如下配置:
server:
port: 5000 #服务端口
spring:
profiles:
active: dev #当前生效环境
application:
name: springcloud-admin-server #指定应用的唯一标识/服务名
security:
user:
name: "admin"
password: "admin"
# tag::configuration-eureka[]
eureka:
client:
registryFetchIntervalSeconds: 5 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ #服务注册中心地址
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率
health-check-url-path: /actuator/health #健康检查页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
metadata-map: #携带登录用户及密码
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
#暴露所有监控端点
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS #总是展示详情信息
# end::configuration-eureka[]
---
第三步:加注解;使用@EnableAdminServer开启Admin监控服务
package com.springcloud.demo;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Configuration;
/**
* @description 系统监控服务
* @auther: xukang
* @date: 2021-06-17 10:06
*/
@EnableEurekaClient
@EnableAdminServer
@SpringBootApplication
public class SpringCloudAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudAdminApplication.class, args);
}
}
构建监控客户端
第四步:引依赖;监控客户端必须引入Spring Boot Actuator 依赖
<!--eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--实时健康监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
第五步:加配置;在springcloud-client工程的bootstrap.yml文件配置暴露监控端点,以便被监控服务端监控发现
#暴露所有监控端点
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS #总是展示详情信息
# end::configuration-eureka[]
第六步:加注解;由于使用SC注册中心模式,此处只需要在服务启动类添加注册发现注解@EnableEurekaClient即可
第七步:启动服务,访问上面配置的http://localhost:5000查看效果
需要启动以下服务,SpringCloudTMAppliction可以不用启动
扩展-Spring Boot Admin 整合Spring Security
第一步:引依赖;system-admin-server服务模块引入Spring Security依赖
<!--集成security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
第二步:加配置;在system-admin-server的服务配置文件bootstrap.yml中添加如下配置
同时,新建Security配置类,配置登录页信息
package com.springcloud.demo.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;
/**
* @description
* @auther: xukang
* @date: 2021-06-17 14:47
*/
@Configuration
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public AdminSecurityConfig(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
}
}
第三步:启动服务,访问5000端口,重定向到登录页,使用上述配置的用户名及密码(admin/admin)进行登录
总结
Spring Boot Admin是一个用于管理和监控SpringBoot应用程序, Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI的开源项目。相当于Spring Boot Actuator 的升级版.
资源地址
‘
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/4492.html