携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第25天,点击查看活动详情
一、Spring Boot Admin
可视化监控平台, 是一个基于 Spring Boot Actuator 端点之上的 Vue.js 应用程序。
绿色:健康
灰色:连接客户端健康信息超时(超过10s)
红色:就能看到具体异常信息
GitHub官方地址:github.com/codecentric…
1-1、使用
1-1-1、查看官方文档,如何使用
-
1、点击 a quick guide跳转到指引界面,可以看到相关使用指南
-
2、创建服务器并引入依赖,如一个springboot项目
版本建议: Spring Boot 2.x=Spring Boot Admin 2.x (比如Spring Boot 2.3.x 可以用Spring Boot Admin 2.3.x)
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring‐boot‐admin‐starter‐server</artifactId>
<version>
2.3.1
</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
- 3、通过添加@EnableAdminServer到配置中来引入Spring Boot Admin Server配置:
@SpringBootApplication
@EnableAdminServer
public class Boot05AdminserverApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05AdminserverApplication.class, args);
}
}
- 4、注册客户端应用程序并引入依赖,如一个springboot项目
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring‐boot‐admin‐starter‐client</artifactId>
<version>2.3.1</version>
</dependency>
- 5、配置Spring Boot Admin Server的URL,客户端连接Spring Boot Admin Server的地址
spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
-
6、启动admin查看服务(客户端还未启动,只能看到空页面)
-
7、启动客户端服务再次查看spring boot admin服务 可以看到服务已经被监听到了
1-1-2、springboot admin介绍
-
1、应用墙 可以查看所有注册到springboot Admin的服务
-
2、应用 和应用墙类似,都是显示注册的相关服务
-
3、进入应用就可以看到相关指标信息
1-1-3、连接异常
若连接不上,可能是地址使用计算机名称作为地址,可以改变使用ip注册
spring:
boot:
admin:
client:
url: http://localhost:8080
instance:
prefer-ip: true # 使用ip注册进来
application:
name: springboot admin # 客户端名称
访问 服务器的根目录如http://localhost:8080/ 即可浏览
1-2、安全防护
通过上面可以查看服务的监控情况了,但是如果被其他人知道访问地址,那就可以成功访问了,因此需要加一下安全防护。
1-2-1、SBA服务端安全
1-2-1-1、服务端处理
spring-boot-admin-server-ui提供了一个登录页面和一个注销按钮、可以结合SpringSecurity解决身份验证和授权。可以看下官网提供的帮助,
在github搜索spring boot admin,然后点击quick guide
然后找到Security就可以看到帮助文档中的示例代码
下面开始再项目中进行配置
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐security</artifactId>
</dependency>
配置类
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;
private final SecurityProperties security;
public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
this.adminServer = adminServer;
this.security = security;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
http.authorizeRequests(
(authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
.antMatchers(this.adminServer.path("/actuator/info")).permitAll()
.antMatchers(this.adminServer.path("/actuator/health")).permitAll()
.antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
).formLogin(
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.path("/instances"),
HttpMethod.POST.toString()),
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
HttpMethod.DELETE.toString()),
new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
))
.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
}
// Required to provide UserDetailsService for "remember functionality"
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(security.getUser().getName())
.password("{noop}" + security.getUser().getPassword()).roles("USER");
}
}
application.yml配置信息
spring:
security:
user:
name: jony
password: jony
重启服务,就可以访问需要用户名密码了
1-2-1-2、客户端配置
登录成功之后,刚刚注册进来spring actuator看不到了,此时需要给spring actuator加上访问spring admin的用户名密码就可以了,看下官网的说明
下面去我这边spring actuator加上如上配置
重启spring actuactor服务,再次刷新spring boot admin就可以看到注册进来的服务了
1-3、通过注册中心集成客户端
通过上面我们就可以将客户端,注入到spring boot admin 中了,但是如果你有成百上千个微服务, 这样配置未免太麻烦。如果您已经为您的应用程序使用了 Spring Cloud (Alibaba-nacos) Discovery,那么您就不需要 SBA 客户端。只需在 Spring Boot Admin Server 中添加一个
DiscoveryClient,剩下的工作由我们的 AutoConfiguration 完成。
下面的步骤使用 Nacos,但是也支持其他 Spring Discovery实现。
1-3-1、SBA服务端
- 添加spring cloud依赖、版本管理器以及nacos注册中心,最终配置如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jony</groupId>
<artifactId>actuator_admin</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.jony</groupId>
<artifactId>admin_02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin_02</name>
<description>admin_02</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-version>Hoxton.SR12</spring-cloud-version>
<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<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.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--nacos 注册-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<!--spring cloud 版本管理器-->
<dependencyManagement>
<dependencies>
<!--spring cloud alibaba 的版本管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 配置Nacos,最终spring-boot-admin的配置如下
server:
port: 8082
spring:
security:
user:
name: jony
password: jony
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: 192.168.253.131:8851
application:
name: spring-boot-admin-server
3、进入nacos客户端就可以看到spring-boot-admin-server服务已经注册进去了,同时还有其他已经注册进去的服务
4、进入spring-admin控制台,可以看到服务,但是由于两个服务为添加Actuator依赖,所以是异常状态。
# 也会将SBA服务配置为客户端, 所以也可以配置自己的endpoint规则(可选)
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
1-3-2、SBA客户端
在不使用nacos的时候,因为spring-admin配置了Security,Actuator客户端就也需要配置账号密码才能注入到spring-admin。使用了Nacos就不再需要配置Actuator用户名密码,可以直接注入到spring-admin中
- 给stock服务假如Actuator依赖
只需添加actuator即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 重启stock服务,打开spring-admin
- 进入stock服务,只能看health和info端点,无法看到更多端点信息
- 配置stock服务针对Actuator要暴露的端点,如需公开更多端点:(不配置该选项则只显示(health和info端点)
management:
endpoints:
web:
exposure:
include: '*'
- 重启stock服务,再次打开spring-admin服务,所有端点即可查看
1-4、邮件通知
服务通过spring-admin进行管理,当服务下线的时候,就需要通过一些方式告诉运维人员。看下官网,查看的版本为:2.5.1已经提供如下通知
下面以邮件通知为例来进行配置
- 引入邮件的pom依赖
<!--邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置application邮件相关信息之后的全部配置如下
server:
port: 8082
spring:
security:
user:
name: jony
password: jony
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: 192.168.253.131:8851
application:
name: spring-boot-admin-server
#配置邮件客户端
mail:
# 发件人使用的qq邮箱服务
host: smtp.qq.com
username: xxxxx@qq.com
# 授权码,不是密码,在qq邮箱设置-账号里面有生成授权码
password: xxxx
boot:
admin:
notify:
mail:
# 收件人,多个中间用,分隔
to: xxxxx@qq.com
# 发件人
from: xxxxx@qq.com
- 开启qq邮箱授权码密码
- 重启服务,关闭某个服务,坐等邮件通知
服务下线提醒
- 邮件模板也可以进行自定义