源码:https://github.com/GiraffePeng/spring-cloud-scaffolding
Spring Boot Admin 是一个 管理 和 监控 Spring Boot 应用程序 的一款开源软件。Spring Boot Admin 分为 Server 端和 Client 端,Spring Boot Admin UI 部分使用 AngularJS 将数据展示在前端。
官方地址:https://github.com/codecentric/spring-boot-admin/
大体功能如下:
基于之前项目,创建一个新的子级项目(spring-cloud-admin-server),其pom.xml依赖如下:
<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>
<artifactId>spring-cloud-scaffoldingartifactId>
<groupId>me.penggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>spring-cloud-admin-serverartifactId>
<dependencies>
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-serverartifactId>
<version>2.0.3version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webfluxartifactId>
dependency>
dependencies>
project>
其中spring-boot-admin-starter-server为引入Spring Boot Admin的核心依赖包。
配置属性application.yml
server:
port: 8868
spring:
application:
name: adminserver
zipkin:
base-url: http://localhost:9411
sleuth:
feign:
enabled: true
sampler:
probability: 1.0
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8865/eureka/
registry-fetch-interval-seconds: 5
management:
endpoints:
web:
exposure:
include: "*"
exclude: env
endpoint:
health:
show-details: always
启动类加上注解@EnableAdminServer
package com.peng.admin.server;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableAdminServer //启动类加上该注解标明开启Spring Boot Admin
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
至此Admin Server 创建完成。
因为使用的为Spring Cloud环境,那么其实并不用通过 Spring Boot Admin Client 来向 Spring Boot Admin 注册,而是让 Spring Boot Admin 通过注册中心(Eureka、Consul 等)来发现服务.
在admin-client中只需要引入spring-boot-starter-actuator包即可,以userservice为例
.....
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
.....
分别启动
启动后访问:http://localhost:8868/ 进入Spring Boot Admin页面,可以看到有两个服务正常运行,UserServer为Down状态,是因为没有启动MQ,导致UserServer无法连接发出报警信息。
当我们把UserServer服务停止后,SBA(Spring Boot Admin)检测到UserServer服务不可用,会将其至为OFFLINE脱机状态
当我们将MQ启动后,重新启动UserServer服务,可以看到UserServer变成了UP,同时右下角还有服务状态变更的弹框提示。
点击Journal,可以看到各个Client客户端的服务事件变更的时间点以及变更内容
点击Wallboard节点,可以看到有哪些客户端被抓取到客户端信息。
单击其中一个服务,比如USERSERVICE服务,进入到监控详情页,
左侧导航栏介绍:
Spring Boot Admin 提供了登录界面的组件,并且和 Spring Boot Security 结合使用,需要用户登录才能访问。在 Admin Server 的 pom.xml 文件中引入以下依赖:
.....
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
.....
在 admin-server 模块的 application.yml 中完成如下配置,创建一个security的user用户,它的用户名为 admin,密码为admin。通过 eureka.instance.metadate-map 配置属性带上该security的user用户信息。
server:
port: 8868
spring:
application:
name: adminserver
security:
user:
name: 'admin'
password: 'admin'
zipkin:
base-url: http://localhost:9411
sleuth:
feign:
enabled: true
sampler:
probability: 1.0
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8865/eureka/
registry-fetch-interval-seconds: 5
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: "*"
exclude: env
endpoint:
health:
show-details: always
然后在应用程序中配置 Spring Boot Security,创建一个 SecurityConfig的配置类,给静态资源加上 permitAll() 权限,其他的资源访问则需要权限认证,另外这些资源不支持 CSFR(跨站请求伪造),所以禁用掉 CSFR,最后需要开启 HTTP 的基本认证,即 httpBasic() 方法。
package com.peng.admin.server;
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;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
@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 {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**","/actuator/**")
.permitAll()
.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();
}
}
重新启动SBA服务端项目,可以看到有登陆界面需要进行登录才能进行访问,输入账号密码 admin admin 即可登陆。
集成登陆可以通过如下三种方法:
详情请查看 https://github.com/joshiste/spring-boot-admin-samples