Spring Boot Admin 入门
Spring Boot Admin 简介
简而言之,Spring Boot Admin是用来管理和监视您的Spring Boot应用程序的。应用程序需要注册为客户端,UI由AngularJs开发。对于Spring Admin来说,需要分别配置好服务端和客户端。服务端我们将其理解为一个Servlet应用,客户端即为要监控的Springboot应用。
以下为Spring 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 an AngularJs application on top of the Spring Boot Actuator endpoints.
Spring Boot Admin Server 设置
建立Springboot项目,Springboot Admin Server设置为servlet或webflux应用。
引入相关依赖至pom.xml
将pom.xml插入以下依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置主启动类注解
配置主启动类注解@EnableAdminServer
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
启动项目,在浏览器输入http://localhost:8080/,可得到SpringAdmin监控页面
这里插一个坑,若遇到下图所示项目启动失败
2020-08-30 13:30:17.094 INFO 25568 --- [ main] c.e.a.AdminserverdemoApplication : Starting AdminserverdemoApplication on DESKTOP-K6A15G9 with PID 25568 (D:\北京邮电大学\实验室实习\Iaas升级\SpringbootAdmin\target\classes started by 吴宇轩 in D:\北京邮电大学\实验室实习\Iaas升级\SpringbootAdmin)
2020-08-30 13:30:17.101 INFO 25568 --- [ main] c.e.a.AdminserverdemoApplication : No active profile set, falling back to default profiles: default
2020-08-30 13:30:19.255 INFO 25568 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-30 13:30:19.277 INFO 25568 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-30 13:30:19.277 INFO 25568 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-30 13:30:19.280 INFO 25568 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
2020-08-30 13:30:19.280 INFO 25568 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-08-30 13:30:19.281 INFO 25568 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2020-08-30 13:30:19.285 INFO 25568 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1c 28 May 2019]
2020-08-30 13:30:19.423 INFO 25568 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-30 13:30:19.423 INFO 25568 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2237 ms
2020-08-30 13:30:19.837 INFO 25568 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-30 13:30:19.839 WARN 25568 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'webMvcAsyncTaskExecutor' available
2020-08-30 13:30:19.839 INFO 25568 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-08-30 13:30:19.846 INFO 25568 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-08-30 13:30:19.868 INFO 25568 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-30 13:30:19.987 ERROR 25568 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Method requestMappingHandlerAdapter in org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration required a bean named 'webMvcAsyncTaskExecutor' that could not be found.
Action:
Consider defining a bean named 'webMvcAsyncTaskExecutor' in your configuration.
Process finished with exit code 1
则为SpringBootAdmin版本问题,将版本改为2.2.0即可
配置Spring Boot Admin Client
新建一个Spring Boot项目
添加依赖至pom.xml
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.0</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>
将此应用注册应用到服务端
再application.properties中添加相关参数
spring.boot.admin.client.url=http://localhost:8888
#这里公开了所有的端点
management.endpoints.web.exposure.include=*
server.port=8889
添加配置类SecurityPermitAllConfig使执行器端点可访问
package com.example.adminclientdemo;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
展现客户端版本信息
现在启动应用就可以在管理页面看到该客户端实例信息,为了展示客户端版本信息(其它信息请查阅Spring Boot Reference Guide),要在build的时候列出build-info,修改pom.xml*。
<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>
启动Server端,将client端打包成jar包并运行,可以看到如下网页进行监控
显示了build-info信息