Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)。
本文是简单搭建步骤不对其原理进行介绍,官网https://codecentric.github.io/spring-boot-admin/current/
本文配合eureka对服务进行监控,所以不需要admin 客户端,只需要将admin服务端注册到eureka,admin会从eureka自动拉取服务进行监控,其他服务不用变即可开启监控。
需要注意的是,其他被监控的服务yml文件也需要加上下面代码,否则服务监控页面只会默认info信息:
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
1:新建maven聚合项目 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.xxx.xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>admin-server</artifactId>
<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>
<!-- admin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.6</version>
</dependency>
<!-- ui 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 邮箱配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${artifactId}</warName>
</configuration>
</plugin>
</plugins>
<finalName>${artifactId}</finalName>
</build>
</project>
2:admin yml配置
server:
port: 8079
spring:
application:
name: server-admin
# mail:
# host: smtp.qq.com
# username: [email protected]
# password: paudschxdxijbcja
# properties:
# mail.debug: false
# mail.smtp.auth: true #安全认证(默认是true)
# mail.smtp.port: 465
# mail.smtp.ssl.enable: true #开启ssl加密 否则项目启动时报530error
# mail.smtp.ssl.socketFactory: sf
# mail.smtp.starttls.enable: true
# mail.smtp.starttls.required: true
boot:
admin:
ui:
title: admin-Server
# notify:
# mail:
# to: [email protected] #收件人邮箱
# from: [email protected] #发件人邮箱
security:
user:
name: "admin"
password: "admin"
#eureka注册中心地址
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
prefer-ip-address: true
client:
service-url:
defaultZone: http://127.0.0.1:8060/eureka/
register-with-eureka: true
fetch-registry: true
#Actuator配置:暴露敏感路径,默认情况下,敏感路径并不暴露
management:
endpoints:
web:
exposure:
# 暴露xxx端点,如需暴露多个,用,分隔;如需暴露所有端点,用'*'
include: "*"
endpoint:
health:
# 是否展示健康检查详情
show-details: ALWAYS
3:入口类,多了2注解 @EnableDiscoveryClient配合eureka使用 @EnableAdminServer代表是admin服务端
package com.fencer.admin.server;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 类描述:
*
* @author :carry
* @version: 1.0 CreatedDate in 2019年12月02日
* <p>
* 修订历史: 日期 修订者 修订描述
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminServerApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("######################监控服务启动完成!######################");
}
}
4:security登陆配置
package com.fencer.admin.server.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
/**
* 类描述:
*
* @author :carry
* @version: 1.0 CreatedDate in 2019年12月02日
* <p>
* 修订历史: 日期 修订者 修订描述
*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter( "redirectTo" );
http.authorizeRequests()
.antMatchers( adminContextPath + "/assets/**" ).permitAll()
.antMatchers( adminContextPath + "/login" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
.logout().logoutUrl( adminContextPath + "/logout" ).and()
.httpBasic().and()
.csrf().disable();
// @formatter:on
}
}
以上配置完成后,启动项目,浏览器输入localhost:8079进入登录页面,输入账号密码admin,即可进入监控页面
点击一个服务可以在里面看到服务的状态、其他信息比如bean仓库,请求的接口等,可自行百度解释