1. 描述
一个spring boot 集成 spring-boot-admin, 用于管理和监控 springboot 应用程序的 demo示例, 分为server端和client端。
环境:
IDE(idea):2021.3
JDK:1.8
maven:3.8.4
spring boot:2.5.6
spring-boot-admin-dependencies:2.5.5
2. 结果
- server端监控页面
3. demo
3.0 项目结构
3.1 demo-admin
- pom.xml,引入spring-boot-admin-dependencies 依赖,管理版本
<modules>
<module>demo-admin-client</module>
<module>demo-admin-server</module>
</modules>
<properties>
<spring.boot.admin.version>2.5.5</spring.boot.admin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring.boot.admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.2 demo-admin-server
集成spring-boot-admin, 作为server端接收client端信息, 并可视化展示客户端项目的运行状态。
- pom.xml,引入spring-boot-admin-starter-server 依赖
<dependencies>
<!--提供全栈的 web 开发特性,包括 Spring MVC 依赖和 Tomcat 容器-->
<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>
</dependency>
<!-- 提供通用单元测试依赖,包括 JUnit, Hamcrest , Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- SpringBootApplication, 实现启动类,启用AdminServer
@EnableAdminServer
@SpringBootApplication
public class DemoAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoAdminServerApplication.class, args);
System.out.println("----- http://localhost:8880/demo/ -----");
}
}
- application.yml,配置端口和应用路径
server:
port: 8880
servlet:
context-path: /demo
3.3 demo-admin-client
集成spring-boot-admin, 并作为client,如何注册到server端并把自己的运行状态发送过去。
- pom.xml,引入spring-boot-admin-starter-client 依赖
<dependencies>
<!--提供全栈的 web 开发特性,包括 Spring MVC 依赖和 Tomcat 容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 提供通用单元测试依赖,包括 JUnit, Hamcrest , Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- SpringBootApplication, 实现启动类
@SpringBootApplication
public class DemoAdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(DemoAdminClientApplication.class, args);
System.out.println("----- http://localhost:8880/demo/ -----");
}
}
- application.yml, 配置端口、应用路径、actuator、security等
actuator监控配置(server端通过actuator获取运行状态信息)
server:
port: 8881
servlet:
context-path: /demo
spring:
application:
# Spring Boot Admin展示的客户端项目名,不设置,会使用自动生成的随机id
name: demo-admin-client1
boot:
admin:
client:
# Spring Boot Admin 服务端地址
url: http://localhost:8880/demo/
instance:
name: client1
service-url: http://localhost:8881/demo/
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
security:
user:
name: byrc
password: 123456
management:
endpoint:
health:
# 端点健康情况,默认值"never",设置为"always"可以显示硬盘使用情况和线程情况
show-details: always
endpoints:
web:
exposure:
# 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
include: "*"
info:
desc: this is a client
phone: 1351746666
qq: 35174
- 其他代码,eg: TestController .java
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping(value = "/hello")
public String hello(
@RequestParam(value = "name", required = false) String name) {
return String.format("Hello %s! ", name == null ? "World" : name);
}
@GetMapping("/test1")
public String test1(@RequestParam(value = "name") String name) {
return String.format("Are you ready? %s! ", name == null ? "World" : name);
}
}
4. 资料
- spring-boot-admin 文档:https://codecentric.github.io/spring-boot-admin/2.5.5/
- spring-boot-admin github:https://github.com/codecentric/spring-boot-admin/tree/
5. 注
- spirng boot 已在父项目引入(父项目pom.xml配置);
- 部分jar包版本已在父项目管理(如果对应不上,一定、肯定、决定是某些修改,没同步更新文档)。