springbootadmin的使用(非微服务版)
Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序 同样,也可通过springcloud注册中心(如nacos)使用,使用简单功能强大
本文着重测试非微服务的使用
SpringBoot Admin分为server和client两个,server端引入ui操作,而client端则向server注册和提供信息
server端搭建
<!-- admin server 依赖 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.2</version>
</dependency>
启动类添加注解@EnableAdminServer
server:
port: 8080
spring:
application:
name: xueya-server
boot:
admin:
ui:
# ui页面的自定义内容信息
title: 我的服务监控中心
brand: <span>Service Center</span>
client端搭建
注意:client端可以和server端在同一个服务中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client
</artifactId>
</dependency>
spring:
application:
name: xueya-server
boot:
admin:
client:
# spring boot admin server的注册地址
#多个以逗号隔开
#并把localhost换成ip
url: http://localhost:8080
#需要暴露监控端口给spring boot admin server访问
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
访问测试
配置完成后,访问http://localhost:8080
实时日志输出
这个功能确实让人眼前一亮,配置也不复杂,如下:
#yaml里面新增
logging:
#spring admin boot日志输出配置
#需要跟logback-spring.xml配置中日志路径一致
file:
name: logs/suntae-oauth/error.log
引入security授权访问
下面是一个例子: 在server端引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
加入密码访问
spring:
security:
user:
name: coding-farmer
password: 123456
添加security配置
package com.springcloudlearn;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
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 org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
@Configuration
public static 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");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
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()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
// @formatter:on
}
}
}
由于服务端配置了密码,客户端访问的时候需要密码,这是基于SBA访问模式,也就是所谓的直接连接springboot admin服务端模式,在application.yml文件中添加username,password
spring:
application:
name: xueya-server
boot:
admin:
client:
url: http://localhost:8080
username: coding-farmer
password: 123456
总结
以上都是最基础的使用,我们可以引入javaMail发送邮件等等 若想显示更加详细的信息,请在clent端引入pom依赖:
<!-- 系统健康检测模块依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>