什么是SpringBootAdmin?
用于管理和监控项目一种应用,说白了就是 对 spring boot actuator 的封装,提供了界面以及监控。
创建Spring Boot Admin Server
首先创建一个Spring Boot 项目,可以使用idea 去创建或者使用 spring官方的快速工具来创建项目 start.spring.io/
这里注意了,一定要按照我的pom文件来,这里作者是试过的,其他的依赖有可能会出现版本冲突的问题!
pom.xml 依赖:
<?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 https://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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>admindemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admindemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<admin.version>2.3.0</admin.version>
</properties>
<dependencies>
<!-- spring-boot-admin 服务端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<exclusions>
<exclusion>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</exclusion>
</exclusions>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.10.RELEASE</version>
</dependency>
<!-- spring-boot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
spring.application.name=admin-service
server.port=8080
启动文件上面加入@EnableAdminServer 注解
@SpringBootApplication
@EnableAdminServer
public class AdmindemoApplication {
public static void main(String[] args) {
SpringApplication.run(AdmindemoApplication.class, args);
}
}
服务端创建好之后,我们来接下来就创建客户端实例。
创建Spring Boot Admin客户端
与上面的步骤一致,创建好spring boot 项目 加入依赖。
pom.xml
<?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 https://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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>adminclien</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>adminclien</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<admin.version>2.3.0</admin.version>
</properties>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件: 连接服务端口地址,注册进服务端当中。
spring.application.name=admin-client
spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
server.port=8082
与Spring Boot 2一样,默认情况下,大多数端点都不通过http公开,我们公开了所有端点。对于生产,您应该仔细选择要公开的端点。
使执行器端点可访问
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
启动服务端: 启动成功后,访问出现界面说明成功了
启动客户端,注册进admin 服务端当中,说明注册成功了
点击应用墙,查看实例的详细信息
出现以下信息说明 SpringBootAdmin 已经搭建成功了。
这里提以下作者搭建出现的错误吧:
java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 10000ms in 'map' (and no fallback has been configured)
at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.handleTimeout(FluxTimeout.java:289) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ Request to GET health [DefaultWebClient]
Stack trace:
这个错误很恶心,其实就是版本的问题,作者测试过很多版本最后,在 2.3.0这个版本测试成功了,所以大家如果出现这个错误的话,就看看版本的问题吧
<!-- spring-boot-admin 服务端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<exclusions>
<exclusion>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</exclusion>
</exclusions>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.10.RELEASE</version>
</dependency>
移除掉默认版本的reactor-netty 新引入reactor-netty 依赖问题解决。