注意本文:springboot2.1.6版本亲测没问题,如果版本不匹配会出错
spring-boot-actuator-client 用于向 actuator 的server 暴露自己的基础信息和运行,包括线程数,堆栈,等等。。。spring-boot-admin 用于界面展示这些信息。
一个server 多个client
1.Spring Boot Admin 简介
官方原话:
codecentric’s Spring Boot Admin is a community project to manage and monitor your Spring Boot ® applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul). The UI is just a Vue.js application on top of the Spring Boot Actuator endpoints.
大概意思:
Spring Boot Admin是一个社区项目,用于管理和监视基于SpringBoot的应用程序。客户端应用可以通过Spring Boot Admin Client或者注册中心就可以注册到Spring Boot Admin服务端进行监控。Spring Boot Admin 是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。2.X版本使用Vue.js重写了UI界面,简洁。
下面进入实战,搭建服务端与客户端:
2.Spring Boot Admin Server 服务端搭建
2.1 创建一个Springboot项目,基于2.1.6版本,添加相关依赖,pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stwen</groupId>
<artifactId>springboot-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 启动类添加相关注解,@EnableAdminServer:
package com.stwen.admin.server;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class SpringbootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
}
2.3 修改application.yml配置文件,端口可以自己修改:
server:
port: 8888
spring:
application:
name: SpringBootAdmin
boot:
admin:
ui:
title: SpringBootAdmin-Server
2.4 启动服务端应用:
启动成功后,访问localhost:8888 ,就可以看到简洁的界面,现在还没有客户端注册上来,所以显示0:
3. Spring Boot Admin Client 客户端搭建
3.1 同样创建一个Springboot 应用,基于2.1.6版本,pom依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stwen</groupId>
<artifactId>springboot-admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-admin-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 监控:Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 启动类不用修改,修改配置文件application.yml:
server:
port: 9999
spring:
boot:
admin:
client:
# server地址
url: http://localhost:8888
instance:
#client地址
service-base-url: http://localhost:${server.port}
application:
name: SpringBootAdmin-Client
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
# info信息会显示到SpringBootAdmin的server端,这里取的是pom文件中的数据
info:
version: @project.version@
groupId: @project.groupId@
artifactId: @project.artifactId@
3.3 启动客户端应用,显示已注册上,选项内容挺丰富的,具体自己运行去看下:
这里可以看到具体实例的JVM内存情况:
4. 集成SpringCloud Discovery
如果你的应用程序使用了注册中心Spring Cloud Discovery,则不需要SBA Client客户端依赖。只需将SpringBootAdmin Server 服务端注册到注册中心即可,其余的自动配置完成。
4.1 修改上面 Spring Boot Admin Server 的pom,增加注册中心依赖,根据自己需要选择Eureka、Zookeeper、Consul等:
<!-- eureka注册中心-->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>-->
<!--ZK注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<version>2.1.1.RELEASE</version>
<!-- 2.1.1依赖zk为3.5.3,不兼容3.4.x,注册不上-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
<!-- Consul注册中心 -->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>-->
4.2 在启动类中增加多服务注册的注解 @EnableDiscoveryClient:
package com.stwen.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminClient2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminClient2Application.class, args);
}
}
4.3 修改application.yml ,增加多注册信息:
server:
port: 8888
spring:
application:
name: SpringBootAdmin
boot:
admin:
ui:
title: SpringBootAdmin-Server
cloud:
zookeeper:
#zk 注册中心
connectString: localhost:2181
# eureka注册中心
#eureka:
# client:
# serviceUrl:
# defaultZone: http://localhost:1001/eureka/
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
4.4 启动该Server 应用:
可以看到,该Server应用已经把自己注册上zk注册中心,同时也监控了自己。同理,可以将上面的Client 改成类似现在的Server,可以注册监控上。