什么是Spring Boot Admin?
Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者使用 Eureka注册到admin server中进行展示,Spring Boot Admin UI部分使用AngularJs将数据展示在前端。
Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以:在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。
Spring Boot Admin主要功能
spring boot admin为spring boot应用提供了整合的视图,应用的详情视图提供了应用本身及运行时环境(OS和JVM)运维比较关心的数据,应用的运行时信息,log输出,metrics统计,environment和logging level实时调整,thread线程运行时状态,trace,audit和Hystrix。 同时提供了turbine扩展插件,用于整合展示整个集群的熔断器信息。 在Journal模块,可以提供整个集群所有节点的状态变化历程。
Spring Boot Admin服务端搭建
一、服务端注册到Eureka
1、引入maven依赖
<properties>
<admin.version>1.5.7</admin.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
复制
2、创建启动类
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class Monitor {
private static Logger logger = LoggerFactory.getLogger(Monitor.class);
public static void main(String[] args) {
SpringApplication.run(Monitor.class, args);
}
复制
3、服务端注册到Eureka配置信息
server:
port: 7500
spring:
application:
name: ${deploy.servicename}
eureka:
instance:
ipAddress: ${eurekaInstanceIpAddress}
preferIpAddress: true
name: ${deploy.servicename}
metadataMap:
zone: ${eurekaZone}
instanceId: ${deploy.servicename}_${spring.cloud.client.ipAddress}
client:
serviceUrl:
defaultZone: ${eurekaClientServiceUrlDefaultZone}
healthcheck:
enabled: true
复制
二、服务端配置登录
1、引入maven依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>${admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制
2、创建http请求权限认证
@Configuration
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
// Requests for the login page and the static assets are allowed
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
http.authorizeRequests().antMatchers("/**").authenticated();
// Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
复制
3、配置登录用户名和密码
security:
basic:
enabled: false
user:
password: admin
name: admin
复制
4、登录页面效果图
三、服务端集成hystrix UI展示
1、引入maven依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
<version>${admin.version}</version>
</dependency>
复制
2、配置文件中添加endpoints节点
spring:
boot:
admin:
routes:
endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream
复制
3、效果图展示
注:如果页面出现loading,则可能client没有开启hystrix,或者client端没有被调用
四、服务端集成turbine展示
1、引入maven依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
<version>${admin.version}</version>
</dependency>
复制
2、添加turbine相关配置信息以及turbine endpoints节点
spring:
boot:
admin:
turbine:
clusters: muse
location: Muse-Turbine
routes:
endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream
复制
3、添加turbine配置信息参数介绍
spring.boot.admin.turbine.clusters # clusters配置的内容必须和turbine服务中配置的clusters信息一致,形如turbine服务端的配置为 turbine: aggregator: clusterConfig: muse
则spring.boot.admin.turbine.clusters填入muse
spring.boot.admin.turbine.location #注册到Eureka中的turbine的serviceId
4、效果图
五、邮件通知
1、添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
复制
2、在配置文件中配置邮件
spring:
mail:
host: xx.xx.xx
port: xx
username: xxxx
password: xxxxx
复制
3、配置邮件通知
boot:
admin:
notify:
mail:
to: [email protected]
from: [email protected]
enabled: true
复制
配置文件正确后,当监控的服务启动或者停止时,都会收到邮件通知
监控的服务客户端配置
一、在spring boot admin上展示客户端的版本和INFO信息
1、在客户端的配置文件添加如下信息
info:
name: ${deploy.servicename}
description: ${service.description}
version: "@project.version@"
复制
注:其中${deploy.servicename},这个可以在pom.xml的配置,型如
<properties>
<deploy.servicename>abc</deploy.servicename>
</properties>
复制
2、效果图
二、在spring boot admin上展示客户端的日志信息
1、配置日志输出路径
logging:
path: ${logback.dir}
复制
2、在logback.xml中,添加如下内容
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
</configuration>
复制
注:这两个配置缺一不可
3、效果图
三、在spring boot admin上动态修改客户端的日志级别
1、在logback.xml文件中,配置如下内容
<configuration>
<jmxConfigurator />
</configuration>
复制
2、为了避免因jmxConfigurator产生内存泄漏,则添加如下代码
@WebListener
public class LogContextListener implements ServletContextListener {
private static Logger logger = LoggerFactory.getLogger(LogContextListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("LoggerContext 销毁");
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
lc.stop();
}
}
复制
注:客户端的启动类上记得加上@ServletComponentScan 注解
3、效果图
四、在spring boot admin上展示客户端的JMX信息
1、在客户端的pom.xml引入Jolokia包即可
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
复制
2、效果图
附录完整的spring boot admin配置文件信息
server:
port: 7500
spring:
application:
name: ${deploy.servicename}
boot:
admin:
turbine:
clusters: muse
location: Muse-Turbine
routes:
endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,activiti,hystrix.stream,turbine.stream
eureka:
instance:
ipAddress: ${eurekaInstanceIpAddress}
preferIpAddress: true
name: ${deploy.servicename}
metadataMap:
zone: ${eurekaZone}
instanceId: ${deploy.servicename}_${spring.cloud.client.ipAddress}
client:
serviceUrl:
defaultZone: ${eurekaClientServiceUrlDefaultZone}
healthcheck:
enabled: true
logging:
config: classpath:${logback_xml_file}
management:
security:
enabled: false
security:
basic:
enabled: false
user:
password: xxx
name: xxxx
info:
name: ${deploy.servicename}
description: ${service.description}
version: "@project.version@"
复制