Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。常见的功能或者监控如下:
状态更改的事件日志(非持久性)
快速开始: 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
本文的所有工程的Spring Boot版本为2.1.0 、Spring Cloud版本为Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依赖(完整的依赖见源码):
org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE
org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Finchley.SR2在工程admin-server引入admin-server的起来依赖和web的起步依赖,代码如下:
de.codecentric spring-boot-admin-starter-server 2.1.0 org.springframework.boot spring-boot-starter-web
然后在工程的启动类AdminServerApplication加上@EnableAdminServer注解,开启AdminServer的功能,代码如下:
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}在工程的配置文件application.yml中配置程序名和程序的端口,代码如下:
spring: application: name: admin-server server: port: 8769
这样Admin Server就创建好了。
在admin-client工程的pom文件引入admin-client的起步依赖和web的起步依赖,代码如下:
de.codecentric spring-boot-admin-starter-client 2.1.0 org.springframework.boot spring-boot-starter-web
在工程的配置文件application.yml中配置应用名和端口信息,以及向admin-server注册的地址为http://localhost:8769,最后暴露自己的actuator的所有端口信息,具体配置如下:
spring: application: name: admin-client boot: admin: client: url: http://localhost:8769 server: port: 8768 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS
在工程的启动文件如下:
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}一次启动两个工程,在浏览器上输入localhost:8769 ,浏览器显示的界面如下:

查看wallboard:

点击wallboard,可以查看admin-client具体的信息,比如内存状态信息:

也可以查看spring bean的情况:

更多监控信息,自己体验。
同上一个案例一样,本案例也是使用的是Spring Boot版本为2.1.0 、Spring Cloud版本为Finchley.SR2。案例采用Maven多module形式,父pom文件引入以下的依赖(完整的依赖见源码),此处省略。
注册中心使用Eureka、使用Consul也是可以的,在eureka-server工程中的pom文件中引入:
org.springframework.cloud spring-cloud-starter-netflix-eureka-server
配置eureka-server的端口信息,以及defaultZone和防止自注册。最后系统暴露eureka-server的actuator的所有端口。
spring: application: name: eureka-server server: port: 8761 eureka: client: service-url: defaultZone: http://localhost:8761/eureka register-with-eureka: false fetch-registry: false management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
在工程的启动文件EurekaServerApplication加上@EnableEurekaServer注解开启Eureka Server.
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}eureka-server搭建完毕。
在admin-server工程的pom文件引入admin-server的起步依赖、web的起步依赖、eureka-client的起步依赖,如下:
de.codecentric spring-boot-admin-starter-server 2.1.0 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client
然后配置admin-server,应用名、端口信息。并向注册中心注册,注册地址为http://localhost:8761,最后将actuator的所有端口暴露出来,配置如下:
spring:
application:
name: admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS在工程的启动类AdminServerApplication加上@EnableAdminServer注解,开启admin server的功能,加上@EnableDiscoveryClient注解开启eurke client的功能。
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}在admin-client的pom文件引入以下的依赖,由于2.1.0采用webflux,引入webflux的起步依赖,引入eureka-client的起步依赖,并引用actuator的起步依赖如下:
org.springframework.boot spring-boot-starter-webflux org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator
在工程的配置文件配置应用名、端口、向注册中心注册的地址,以及暴露actuator的所有端口。
spring:
application:
name: admin-client
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
server:
port: 8762在启动类加上@EnableDiscoveryClie注解,开启DiscoveryClient的功能。
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
}一次启动三个工程,在浏览器上访问localhost:8769,浏览器会显示和上一小节一样的界面。

在2.1.0版本中去掉了hystrix dashboard,登录界面默认集成到了spring security模块,只要加上spring security就集成了登录模块。
只需要改变下admin-server工程,需要在admin-server工程的pom文件引入以下的依赖:
org.springframework.boot spring-boot-starter-security
在admin-server工的配置文件application.yml中配置spring security的用户名和密码,这时需要在服务注册时带上metadata-map的信息,如下:
spring:
security:
user:
name: "admin"
password: "admin"
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}写一个配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter,配置如下:
本文系作者在时代Java发表,未经许可,不得转载。
如有侵权,请联系nowjava@qq.com删除。