Spring Boot Admin是一个管理和监视Spring Boot应用程序的项目。应用程序通过Spring Boot Admin客户端(通过HTTP)注册,或者使用Spring Cloud服务发现注册。

1、spring-boot-admin-server服务

server端只要加入spring-boot-admin-starter-server依赖即可,默认的情况下是不需要用户名及密码即可登入查看服务详情,所以可以加入spring-boot-starter-security增加安全策略。

  • 关键依赖如下
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>${project.parent.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 代码及配置如下

SpringBootAdminApplication.java

@EnableAdminServer
@SpringBootApplication
public class SpringBootAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }

}

SecurityConfig.java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //1.配置所有静态资源和登录页可以公开访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2.配置登录和登出路径
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3.开启http basic支持,admin-client注册时需要使用
                .httpBasic().and()
                .csrf()
                //4.开启基于cookie的csrf保护
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5.忽略这些路径的csrf保护以便admin-client注册
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }

}

application.yml

server:
  port: 8081

spring:
  security:
    user:
      name: root
      password: root123
  • 界面演示

使用用户名root密码root123登录

image.png

image.png

2、spring-boot-admin-client客户端服务
  • 关键依赖
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
  • 代码及配置如下,使用一个schedulecache演示每秒输出日志并获取cache数据

SpringBootAdminClientApplication .java

@EnableCaching
@EnableScheduling
@SpringBootApplication
public class SpringBootAdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminClientApplication.class, args);
    }

}

application.yml,注意需要配置logging.file.name才可在日志中查看到实时日志

server:
  port: 8082

spring:
  application:
    name: spring-boot-admin-client
  boot:
    admin:
      client:
        url: http://localhost:8081
        username: root
        password: root123

logging:
  level:
    root: info
  file:
    name: logs/client.log
management:
  endpoints:
    web:
      exposure:
        include: "*"

CacheService.java

@Slf4j
@Service
@CacheConfig(cacheNames = "cacheService")
public class CacheService {

    @Cacheable(key = "#name")
    public String  get(String name) {
        log.info("{}", name);
        return "name:" + name;
    }

}

AdminSchedule.java 模拟每秒获取缓存数据,可以spring-boot-admin控制台中清除此缓存

@Slf4j
@Component
public class AdminSchedule {

    @Autowired
    private CacheService cacheService;

    @Scheduled(cron = "0/1 * * * * ?")
    public void process() {
        cacheService.get("name" + System.currentTimeMillis() / 10000);
        log.info("schedule success");
    }

}
  • 查看spring-boot-admin-client日志

image.png

  • 查看spring-boot-admin-client缓存定义,可清除缓存

image.png

  • 查看基本详情

image.png

3、使用spring-cloud注册中心注册

如果微服务已经使用eureka、consul、zookeeper等注册中心,可更加方便集成,如eureka,增加以下依赖即可

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

示例代码:https://gitee.com/viturefree/spring-boot-admin.git


赞赏(Donation)
微信(Wechat Pay)

donation-wechatpay