1、介绍
SpringBootAdmin可以监控SpringBoot 单机或集群项目。通过结合actuator,能够提供一些可视化的细节信息,比如:进程信息、内存信息、垃圾回收信息,配置属性、类信息、系统环境信息、日志设置和查看、定时任务查看、缓存查看和管理等功能。(抄的)
说下理解,spring boot admin 简称sba。它分为服务端和客户端。客户端就是我们需要监控的服务,服务端是一个独立的服务,运行之后就可以访问页面。如图:http://127.0.0.1:8188/applications
当前是没有客户端注册的情况,所以看不到实列。步入正题。
2、正题
服务端和客户端的spring boot都是用的 2.7.14
这里先创建sba的服务端。使用idea创建一个spring boot 项目。
这里选择spring initializr 输入项目名称和选择jdk版本
然后选择spring boot 的版本为2.7.14。其他都不选。
pom.xml 这里我用了security,一个安全管理框架,进入sba的服务端不需要校验,加上security就需要账号密码才能进入。
<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>
<version>2.7.4</version>
</dependency>
<!-- security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<!-- <version>2.7.14</version>-->
</dependency>
在启动类上加上@EnableAdminServer注解。
@SpringBootApplication
@EnableAdminServer
public class SbaApplication {
public static void main(String[] args) {
SpringApplication.run(SbaApplication.class, args);
System.out.println("启动成功!");
}
}
添加一个简单的Security配置文件SecurityConfig。作用都有注释。
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity // 开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 定制请求的授权规则
// 首页所有人可以访问
http.authorizeRequests().antMatchers("/instances").permitAll() // instances 客户端请求时放行
.anyRequest() // 任何其它请求
.authenticated(); // 都需要身份认证
http.formLogin()
.loginProcessingUrl("/login"); // 登陆表单提交请求;
http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
//开启自动配置的注销的功能
// /logout 注销请求
http.logout().logoutSuccessUrl("/");
}
}
最后就是application.properties配置文件
server.port=8188
#设置security默认登陆的账号密码
spring.security.user.name=admin
spring.security.user.password=123456.
#加上这个防止启动时的警告 没有禁告可以不加
spring.thymeleaf.check-template-location=false
到这里sba的服务端就建好了,直接运行。然后访问http://127.0.0.1:8188就会自动跳转到登陆页面
输入application.properties里面配置的账号密码。就会进入到sba的管理页面了。
http://127.0.0.1:8188/applications
如果没有使用Security,访问http://127.0.0.1:8188就会直接进入到sba的管理页面。
到这里服务端就完成了,可以直接打包部署到服务器上了。附上项目目录:
sba的服务端搭建好了,就可以搭客户端了。
客户端就简单了,你可以使用你原有的项目,也可以新建一个spring boot项目。
项目准备好后,直接引入maven包。我spring boot 版本用的2.7.14。
<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>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.14</version>
</dependency>
之后修改application.yml,添加配置
server:
port: 8080
management:
endpoints:
web:
exposure:
include: '*' #暴露所有端点 可以取消自己看效果
spring:
application:
name: client #sba服务器上显示的客户端名称
boot:
admin:
client:
url: http://127.0.0.1:8188 #sba监控需要
instance:
service-url: http://127.0.0.1:8080 #告诉服务器客户端地址
客户端就添加成功了,可以添加多个客户端。启动客户端后,就可以在sba的管理页面看见客户端的信息了。
最后要注意,如果客户端添加了过滤器比如登陆校验这些,记得放开接口:
"/actuator","/actuator/**"
到这里就sba的完成了。