前言
最近做项目,需要对现有的spring boot项目做一个类似console界面,管理与监控boot应用,干脆就直接在spring boot admin定制了。先了解spring boot admin吧。对于spring boot admin,每个应用都是客户端,通过 HTTP 或者使用 Eureka 等注册中心注册到 admin server 中,前端展示。定制化一般而言,主要是权限定制,界面定制,业务新增管理定制。
Spring Boot Admin 是基于 Spring Boot 的 Actuator 接口做的UI监控工具,主要数据:基本信息、Health 、内存、JVM 、垃圾回收、配置等,可修改 logger 的 level。
1. Spring boot admin server
这里使用目前最新的2.2.1,为模板。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
根据依赖,可以看到spring boot admin非官方的项目,但社区活跃度很高,质量还是可以保证的。其本身还是一个spring boot应用。
1.1 main方法
@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminMain.class, args);
}
}
注解@EnableAdminServer,即可开启
1.2 application配置
server.port=8082
spring.application.name=BootAdminServer
笔者同一台机器,需要配置端口,服务器可根据实际情况,建议对每个spring boot配置spring.application.name,方便服务注册与发现,集成注册中心。
启动后注意日志
根据源码,意思就是解析
resolve links to the given
/**
* Creates a new {@code EndpointLinksResolver} that will resolve links to the given
* {@code endpoints} that are exposed beneath the given {@code basePath}.
* @param endpoints the endpoints
* @param basePath the basePath
*/
public EndpointLinksResolver(Collection<? extends ExposableEndpoint<?>> endpoints, String basePath) {
this.endpoints = endpoints;
if (logger.isInfoEnabled()) {
logger.info("Exposing " + endpoints.size() + " endpoint(s) beneath base path '" + basePath + "'");
}
}
1.3 UI
访问http://localhost:8082/
2. spring boot admin的client,通过HTTP注册
依赖client的pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
2.1 main方法
@SpringBootApplication
public class SpringBootClientMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootClientMain.class, args);
}
}
不需要其他注解支持
2.2 application配置
server.port=8180
spring.application.name=BootClient
spring.boot.admin.client.url=http://127.0.0.1:8082
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.boot.admin.client.url
通过HTTP注册方式,配置server端URL。
management.endpoints.web.exposure.include=* #暴露所有
management.endpoint.health.show-details=always #一直显示健康详情
测试环境建议都开启,生产根据实际,提高性能
2.3 启动查看UI
看日志暴露了14个/actuator接口,spring boot admin其实就是展示这些接口的数据
可以看到实例已经在线了,点击可以看到进程,GC,堆信息
配置要注意一下,各种配置其实就可以在这里看,这个功能异常适用,以我们暴露spring boot admin client为例
比如:management.endpoints.web.basePath,在同一个Tomcat多个spring boot的时候就可以定制URL达到共存的目的
日志可以直接调节级别,即改即生效
映射可以看到暴露的requestmapping,接口开发时很有用
3. eureka注册中心
上面的demo是通过HTTP注册server端的,微服务本身提供注册中心,比如eureka console zookeeper。其实就是中心化注册。
搭建eureka-server,其他注册中心同理,pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
main方法,注解上@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMain {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain.class, args);
}
}
配置
spring.application.name=eureka-server
server.port=8088
eureka.client.service-url.defaultZone=http://127.0.0.1:8088/eureka
eureka.client.allow-redirects=false
eureka.client.fetch-registry=false
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
3.1 admin server改造
admin的pom增加eureka的client支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
这里也可以使用webflux的依赖同样可行
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
main方法增加注解@EnableDiscoveryClient
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class SpringBootAdminMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminMain.class, args);
}
}
配置
server.port=8082
spring.application.name=BootAdminServer
eureka.client.service-url.defaultZone=http://127.0.0.1:8088/eureka
eureka.client.registry-fetch-interval-seconds=30
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.health-check-url-path=/actuator/health
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
增加注册中心的心跳时长,可调节,注册中心地址
3.2 admin client改造
pom增加与admin端一样
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
main方法同理注解上@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class SpringBootClientMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootClientMain.class, args);
}
}
配置
server.port=8180
spring.application.name=BootClient
#spring.boot.admin.client.url=http://127.0.0.1:8082
eureka.client.service-url.defaultZone=http://127.0.0.1:8088/eureka
eureka.client.registry-fetch-interval-seconds=30
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.health-check-url-path=/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
不再配置admin的HTTP地址,注册中心去拿,顺便也可以让admin管理注册中心下所有应用,包括注册中心
3.3 测试
访问注册中心http://localhost:8088/
3个应用都注册了
访问admin server,http://localhost:8082/
3个应用都在线,3个实例;这里说一下,1个应用可以包含多个实例,比如一个Tomcat启动多个spring boot应用,这时就需要配置区分
/actuator
的前缀避免冲突。
各个应用管理与监控正常
这里需要注意,点击时不能点击URL,要点击URL的背景位置,不然不能访问
因为URL是
总结
spring boot admin虽然不是spring boot官方的,但是 Apache License 2.0,不用担心版权问题,社区活跃度高,管理方便定制,但缺点也明显,上面的示例没有权限管理,邮件通知。其实是支持的,只是很简陋,不能满足复杂一点的业务的需要,需要定制化开发;另外spring boot admin管理的页面也需要权限控制,内容也需要根据实际增删。下一章增加权限,邮件通知,试试一个应用多个实例。