Spring Boot Admin
是一个开源社区项目,用于管理和监控
SpringBoot
应用程序。
应用程序作为
Spring Boot Admin Client
向为
Spring Boot Admin Server
注册(通过
HTTP
)或使用
SpringCloud
注册中心(例如
Eureka
,
Consul
)发现。
UI
是的
AngularJs
应用程序,展示
Spring Boot Admin Client
的
Actuator
端点上的一些监控。常见的功能或者监控如下:
§
显示健康状况
§
显示详细信息,例如
o
JVM
和内存指标
o
micrometer.io
指标
o
数据源指标
o
缓存指标
§
显示构建信息编号
§
关注并下载日志文件
§
查看
jvm
系统和环境属性
§
查看
Spring Boot
配置属性
§
支持
Spring Cloud
的
postable / env-
和
/ refresh-endpoint
§
轻松的日志级管理
§
与
JMX-beans
交互
§
查看线程转储
§
查看
http
跟踪
§
查看
auditevents
§
查看
http-endpoints
§
查看计划任务
§
查看和删除活动会话(使用
spring-session
)
§
查看
Flyway / Liquibase
数据库迁移
§
下载
heapdump
§
状态变更通知(通过电子邮件,
Slack
,
Hipchat
,
......
)
§
状态更改的事件日志(非持久性)
快速开始
创建Spring Boot Admin Server
本文的所有工程的
Spring Boot
版本为
2.1.0
、
Spring Cloud
版本为
Finchley.SR2
。案例采用
Maven
多
module
形式,父
pom
文件引入以下的依赖(完整的依赖见源码):
1.
<parent>
2.
<groupId>
org.springframework.boot
</groupId>
3.
<artifactId>
spring-boot-starter-parent
</artifactId>
4.
<version>
2.1.0.RELEASE
</version>
5.
<relativePath/>
6.
</parent>
7.
8.
9.
<dependencyManagement>
10.
<dependencies>
11.
<dependency>
12.
<groupId>
org.springframework.cloud
</groupId>
13.
<artifactId>
spring-cloud-dependencies
</artifactId>
14.
<version>
${spring-cloud.version}
</version>
15.
<type>
pom
</type>
16.
<scope>
import
</scope>
17.
</dependency>
18.
</dependencies>
19.
</dependencyManagement>
20.
21.
22.
<spring-cloud.version>
Finchley.SR2
</spring-cloud.version>
在工程
admin-server
引入
admin-server
的起来依赖和
web
的起步依赖,代码如下:
1.
<dependency>
2.
<groupId>
de.codecentric
</groupId>
3.
<artifactId>
spring-boot-admin-starter-server
</artifactId>
4.
<version>
2.1.0
</version>
5.
</dependency>
6.
<dependency>
7.
<groupId>
org.springframework.boot
</groupId>
8.
<artifactId>
spring-boot-starter-web
</artifactId>
9.
</dependency>
然后在工程的启动类
AdminServerApplication
加上
@EnableAdminServer
注解,开启
AdminServer
的功能,代码如下:
1.
@SpringBootApplication
2.
@EnableAdminServer
3.
public
class
AdminServerApplication
{
4.
5.
public
static
void
main(
String
[] args) {
6.
SpringApplication
.run(
AdminServerApplication
.
class
, args );
7.
}
8.
9.
}
在工程的配置文件
application.yml
中配置程序名和程序的端口,代码如下:
1.
spring:
2.
application:
3.
name: admin-server
4.
server:
5.
port:
8769
这样
Admin Server
就创建好了。
创建Spring Boot Admin Client
在
admin-client
工程的
pom
文件引入
admin-client
的起步依赖和
web
的起步依赖,代码如下:
1.
<dependency>
2.
<groupId>
de.codecentric
</groupId>
3.
<artifactId>
spring-boot-admin-starter-client
</artifactId>
4.
<version>
2.1.0
</version>
5.
</dependency>
6.
7.
<dependency>
8.
<groupId>
org.springframework.boot
</groupId>
9.
<artifactId>
spring-boot-starter-web
</artifactId>
10.
</dependency>
在工程的配置文件
application.yml
中配置应用名和端口信息,以及向
admin-server
注册的地址为
http://localhost:8769
,最后暴露自己的
actuator
的所有端口信息,具体配置如下:
1.
spring:
2.
application:
3.
name: admin-client
4.
boot:
5.
admin:
6.
client:
7.
url: http:
//localhost:8769
8.
server:
9.
port:
8768
10.
11.
management:
12.
endpoints:
13.
web:
14.
exposure:
15.
include:
'*'
16.
endpoint:
17.
health:
18.
show-details: ALWAYS
在工程的启动文件如下:
1.
@SpringBootApplication
2.
public
class
AdminClientApplication
{
3.
4.
public
static
void
main(
String
[] args) {
5.
SpringApplication
.run(
AdminClientApplication
.
class
, args );
6.
}
一次启动两个工程,在浏览器上输入
localhost:8769
,浏览器显示的界面如下:
查看
wallboard
:
点击
wallboard
,可以查看
admin-client
具体的信息,比如内存状态信息:
也可以查看
spring bean
的情况:
更多监控信息,自己体验。
Spring boot Admin
结合SC注册中心使用
同上一个案例一样,本案例也是使用的是
Spring Boot
版本为
2.1.0
、
Spring Cloud
版本为
Finchley.SR2
。案例采用
Maven
多
module
形式,父
pom
文件引入以下的依赖(完整的依赖见源码),此处省略。
搭建注册中心
注册中心使用
Eureka
、使用
Consul
也是可以的,在
eureka-server
工程中的
pom
文件中引入:
1.
<dependency>
2.
<groupId>
org.springframework.cloud
</groupId>
3.
<artifactId>
spring-cloud-starter-netflix-eureka-server
</artifactId>
4.
</dependency>
配置
eureka-server
的端口信息,以及
defaultZone
和防止自注册。最后系统暴露
eureka-server
的
actuator
的所有端口。
1.
spring:
2.
application:
3.
name: eureka-server
4.
server:
5.
port:
8761
6.
eureka:
7.
client:
8.
service-url:
9.
defaultZone: http:
//localhost:8761/eureka
10.
register
-
with
-eureka:
false
11.
fetch-registry:
false
12.
management:
13.
endpoints:
14.
web:
15.
exposure:
16.
include:
"*"
17.
endpoint:
18.
health:
19.
show-details: ALWAYS
在工程的启动文件
EurekaServerApplication
加上
@EnableEurekaServer
注解开启
Eureka Server.
1.
@SpringBootApplication
2.
@EnableEurekaServer
3.
public
class
EurekaServerApplication
{
4.
5.
public
static
void
main(
String
[] args) {
6.
SpringApplication
.run(
EurekaServerApplication
.
class
, args );
7.
}
8.
}
eureka-server
搭建完毕。
搭建admin-server
在
admin-server
工程的
pom
文件引入
admin-server
的起步依赖、
web
的起步依赖、
eureka-client
的起步依赖,如下:
1.
<dependency>
2.
<groupId>
de.codecentric
</groupId>
3.
<artifactId>
spring-boot-admin-starter-server
</artifactId>
4.
<version>
2.1.0
</version>
5.
</dependency>
6.
7.
<dependency>
8.
<groupId>
org.springframework.boot
</groupId>
9.
<artifactId>
spring-boot-starter-web
</artifactId>
10.
</dependency>
11.
12.
<dependency>
13.
<groupId>
org.springframework.cloud
</groupId>
14.
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
15.
</dependency>
然后配置
admin-server
,应用名、端口信息。并向注册中心注册,注册地址为
http://localhost:8761
,最后将
actuator
的所有端***露出来,配置如下:
1.
spring:
2.
application:
3.
name: admin-server
4.
server:
5.
port:
8769
6.
eureka:
7.
client:
8.
registryFetchIntervalSeconds:
5
9.
service-url:
10.
defaultZone: ${EUREKA_SERVICE_URL:http:
//localhost:8761}/eureka/
11.
instance:
12.
leaseRenewalIntervalInSeconds:
10
13.
health-check-url-path:
/actuator/
health
14.
15.
management:
16.
endpoints:
17.
web:
18.
exposure:
19.
include:
"*"
20.
endpoint:
21.
health:
22.
show-details: ALWAYS
在工程的启动类
AdminServerApplication
加上
@EnableAdminServer
注解,开启
admin server
的功能,加上
@EnableDiscoveryClient
注解开启
eurke client
的功能。
1.
@SpringBootApplication
2.
@EnableAdminServer
3.
@EnableDiscoveryClient
4.
public
class
AdminServerApplication
{
5.
6.
public
static
void
main(
String
[] args) {
7.
SpringApplication
.run(
AdminServerApplication
.
class
, args );
8.
}
9.
10.
}
搭建admin-client
在
admin-client
的
pom
文件引入以下的依赖,由于
2.1.0
采用
webflux
,引入
webflux
的起步依赖,引入
eureka-client
的起步依赖,并引用
actuator
的起步依赖如下:
1.
<dependency>
2.
<groupId>
org.springframework.boot
</groupId>
3.
<artifactId>
spring-boot-starter-webflux
</artifactId>
4.
</dependency>
5.
6.
<dependency>
7.
<groupId>
org.springframework.cloud
</groupId>
8.
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
9.
</dependency>
10.
11.
<dependency>
12.
<groupId>
org.springframework.boot
</groupId>
13.
<artifactId>
spring-boot-starter-actuator
</artifactId>
14.
</dependency>
在工程的配置文件配置应用名、端口、向注册中心注册的地址,以及暴露
actuator
的所有端口。
1.
spring:
2.
application:
3.
name: admin-client
4.
eureka:
5.
instance:
6.
leaseRenewalIntervalInSeconds:
10
7.
health-check-url-path: /actuator/health
8.
9.
client:
10.
registryFetchIntervalSeconds:
5
11.
service-url:
12.
defaultZone: ${EUREKA_SERVICE_URL:http:
//localhost:8761}/eureka/
13.
management:
14.
endpoints:
15.
web:
16.
exposure:
17.
include:
"*"
18.
endpoint:
19.
health:
20.
show-details: ALWAYS
21.
server:
22.
port:
8762
在启动类加上
@EnableDiscoveryClie
注解,开启
DiscoveryClient
的功能。
1.
@SpringBootApplication
2.
@EnableDiscoveryClient
3.
public
class
AdminClientApplication
{
4.
5.
public
static
void
main(
String
[] args) {
6.
SpringApplication
.run(
AdminClientApplication
.
class
, args );
7.
}
8.
}
一次启动三个工程,在浏览器***问
localhost:8769
,浏览器会显示和上一小节一样的界面。
集成spring security
在
2.1.0
版本中去掉了
hystrix dashboard
,登录界面默认集成到了
spring security
模块,只要加上
spring security
就集成了登录模块。
只需要改变下
admin-server
工程,需要在
admin-server
工程的
pom
文件引入以下的依赖:
1.
<dependency>
2.
<groupId>
org.springframework.boot
</groupId>
3.
<artifactId>
spring-boot-starter-security
</artifactId>
4.
</dependency>
在
admin-server
工的配置文件
application.yml
中配置
spring security
的用户名和密码,这时需要在服务注册时带上
metadata-map
的信息,如下:
1.
spring:
2.
security:
3.
user:
4.
name:
"admin"
5.
password:
"admin"
6.
7.
eureka:
8.
instance:
9.
metadata-map:
10.
user.name: ${spring.security.user.name}
11.
user.password: ${spring.security.user.password}
写一个配置类
SecuritySecureConfig
继承
WebSecurityConfigurerAdapter
,配置如下:
1.
@Configuration
2.
public
class
SecuritySecureConfig
extends
WebSecurityConfigurerAdapter
{
3.
4.
private
final
String
adminContextPath;
5.
6.
public
SecuritySecureConfig
(
AdminServerProperties
adminServerProperties) {
7.
this
.adminContextPath = adminServerProperties.getContextPath();
8.
}
9.
10.
@Override
11.
protected
void
configure(
HttpSecurity
http)
throws
Exception
{
12.
// @formatter:off
13.
SavedRequestAwareAuthenticationSuccessHandler
successHandler =
new
SavedRequestAwareAuthenticationSuccessHandler
();
14.
successHandler.setTargetUrlParameter(
"redirectTo"
);
15.
16.
http.authorizeRequests()
17.
.antMatchers( adminContextPath +
"/assets/**"
).permitAll()
18.
.antMatchers( adminContextPath +
"/login"
).permitAll()
19.
.anyRequest().authenticated()
20.
.and()
21.
.formLogin().loginPage( adminContextPath +
"/login"
).successHandler( successHandler ).and()
22.
.logout().logoutUrl( adminContextPath +
"/logout"
).and()
23.
.httpBasic().and()
24.
.csrf().disable();
25.
// @formatter:on
26.
}
27.
}
重启启动工程,在浏览器***问:
http://localhost:8769/
,会被重定向到登录界面,登录的用户名和密码为配置文件中配置的,分别为
admin
和
admin
,界面显示如下:
集成邮箱报警功能
在
spring boot admin
中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。集成非常简单,只需要改造下
admin-server
即可:
在
admin-server
工程
Pom
文件,加上
mail
的起步依赖,代码如下:
1.
<dependency>
2.
<groupId>
org.springframework.boot
</groupId>
3.
<artifactId>
spring-boot-starter-mail
</artifactId>
4.
</dependency>
在配置文件
application.yml
文件中,需要配置邮件相关的配置,如下:
1.
spring.mail.host: smtp.
163.com
2.
spring.mail.username: miles02
3.
spring.mail.password:
4.
spring.boot.admin.notify.mail.to:
124746406@qq
.com
做完以上配置后,当我们已注册的客户端的状态从
UP
变为
OFFLINE
或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。
源码下载
快速开始:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin
和
spring cloud
结合:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-boot-admin-cloud
参考资料
http://codecentric.github.io/spring-boot-admin/2.1.0/
https://github.com/codecentric/spring-boot-admin
原创作者:方志朋
方志朋简介:
SpringCloud
中国社区联合创始人,博客访问量突破一千万,爱好开源,热爱分享,活跃于各大社区,保持着非常强的学习驱动力,终身学习践行者,终身学习受益者。目前就职于国内某家知名互联网保险公司,担任
DEVOPS
工程师,对微服务领域和持续集成领域研究较深,精通微服务框架
SpringCloud