SBA介绍及SBA+nacos集成

前面的文章介绍过了spring家强大的服务监控管理系统spring boot actuator,但是好学的朋友会发现通过http restful api的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,当服务过多的时候查看起来就过于操蛋了,每个服务都需要调用不同的接口来查看监控信息,各种困扰因素,这时spring-boot-admin就闪亮登场了。

SBA

SBA 全称 Spring Boot Admin是一个管理和监控Spring Boot应用程序的开源项目。分为admin-serveradmin-client两个组件,admin-server通过采集actuator端点数据,显示在spring-boot-admin-ui上,已知的端点几乎都有进行采集,通过spring-boot-admin可以动态切换日志级别、导出日志、导出heapdump、监控各项指标 等等….

Spring Boot Admin在对单一应用服务监控的同时也提供了集群监控方案,支持通过eurekaconsulzookeeper、nacos等注册中心的方式实现多服务监控与管理…

spring boot admin+nacos集成:

服务端整合:

新建boot项目引入下面依赖:

<!-- admin服务端依赖 https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-starter-server</artifactId>
   <version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
   <version>2.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
   <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置中心-->
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--注册中心-->
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

属性配置:(安全起见引入security)

spring:
  #spring boot admin的登陆账号和密码配置
  security:
    user:
      name: admin
      password: 123456

management:
  endpoints:
    web:
      exposure:
        include: '*'
#日志相关配置
logging:
  level:
    org.springframework.security: DEBUG
  path: logs/
  file:
    max-size: 1GB

bootstrap.yml配置

server:
  port: ${SERVER_PORT:8022}
spring:
  application:
    name: admin
  cloud:
    nacos:
      discovery:
        server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
      config:
        server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
        file-extension: yml

主函数:

添加上@EnableAdminServer注解即代表是Server端。

配置文件:

package com.geostar.cloud.monitor.admin;

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;

@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 {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");

        http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/actuator/**").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
    }
}

至此服务端搭建完毕。

客户端:

只需要在你的cloud子项目模块引入actuator,并且注册到nacos即可,admin服务端就会去nacos服务中心拉取服务列表。

低版本可能还需要spring-boot-admin-starter-client

<!--自省和监控的集成功能-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置:

management:
  endpoints:
    web:
      exposure:
        include: '*'

logging:
  level:
    com.geostar.cloud: debug
    java.sql.PreparedStatement: debug
  path: logs/
  file:
    max-size: 1GB

然后把你的cloud项目全都启动访问admin服务中心:localhost:8022输入配置好的security用户名密码就可以愉快的玩耍了。👇

然后点击某一个应用进去就可以看到详细的监控信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值