SBA可以通过客户端上报,也可以通过配置中心自动获取上报信息。

目前统一采用nacos注册中心方式获取服务地址并获取上报信息。

SBA环境搭建

客户端

客户端指需要使用arthas的微服务。具体操作如下:

1.添加监控maven依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. <version>xx.xx.xx</version>
  5. </dependency>

2.配置监控信息

  1. management.metrics.tags.application=${spring.application.name}
  2. management.auditevents.enabled=true
  3. management.endpoint.auditevents.enabled=true
  4. management.endpoint.auditevents.cache.time-to-live=5
  5. management.endpoints.web.exposure.include=*
  6. management.endpoint.health.enabled=true
  7. management.endpoint.health.show-details=always
  8. management.endpoint.beans.enabled=true
  9. management.endpoint.beans.cache.time-to-live=0ms
  10. management.endpoint.shutdown.enabled=true

其中有其他很多属性和配置方式,请参照官方文档

3.即时日志配置

如果要在SBA监控中输出实时日志,添加一下配置

  1. ###springbootAdmin 开启日志打印
  2. management.endpoint.logfile.enabled=true
  3. management.endpoint.logfile.external-file=/opt/logs/xy-rbac/xy-rbac.log
  4. management.endpoint.logfile.cache.time-to-live=0ms

服务端

服务端指SBA的监控平台。具体操作如下:

1.创建一个标准的springboot项目并添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>de.codecentric</groupId>
  7. <artifactId>spring-boot-admin-starter-server</artifactId>
  8. <version>${spring-boot-admin-starter-server.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-security</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.alibaba.cloud</groupId>
  20. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  21. </dependency>

2.开启服务端监控功能

  1. @EnableWebSecurity
  2. @EnableAdminServer
  3. @SpringBootApplication
  4. @EnableDiscoveryClient
  5. public class ApmAdminApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ApmAdminApplication.class, args);
  8. }
  9. }

3.配置spring security安全框架

  1. /**
  2. * @author jack.li
  3. */
  4. @Configuration
  5. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  6. private final String adminContextPath;
  7. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  8. this.adminContextPath = adminServerProperties.getContextPath();
  9. }
  10. @Override
  11. protected void configure(HttpSecurity http) throws Exception {
  12. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  13. successHandler.setTargetUrlParameter("redirectTo");
  14. // successHandler.setDefaultTargetUrl(adminContextPath + "/");
  15. http.authorizeRequests()
  16. .antMatchers(adminContextPath + "/assets/**").permitAll()
  17. .antMatchers(adminContextPath + "/login").permitAll()
  18. .antMatchers(adminContextPath + "/actuator/**").permitAll()
  19. // .antMatchers(adminContextPath + "/instances/**").permitAll()
  20. .anyRequest().authenticated()
  21. .and()
  22. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  23. .logout().logoutUrl(adminContextPath + "/logout").and()
  24. .httpBasic().and()
  25. .csrf().disable();
  26. }
  27. }

效果如下:

SBA - 图1SBA - 图2

SBA - 图3

SBA集成Arthas 控制台

客户端

客户端指需要使用arthas的微服务。具体操作如下:

1.添加maven依赖

  1. <dependency>
  2. <groupId>com.taobao.arthas</groupId>
  3. <artifactId>arthas-spring-boot-starter</artifactId>
  4. <version>3.5.4</version>
  5. </dependency>

版本号根据各自项目需要做调整

2.配置arthas console

  1. ## 客户端连接的唯一标识
  2. arthas.agent-id=xy-rbac
  3. ## 使用tunnel-server所在服务器ip
  4. arthas.tunnel-server=ws://127.0.0.1:7777/ws

服务端

服务端指SBA的监控平台。具体操作如下:

1.下载启动arthas console

在官网下载arthas-tunnel-server-3.5.4-fatjar.jar

通过如下命令启动即可:

  1. java -jar arthas-tunnel-server-3.5.4-fatjar.jar

其启动参数根据需要自行解决。

2.配置扩展UI

  1. spring.boot.admin.ui.external-views[0].label=Arthas Console
  2. spring.boot.admin.ui.external-views[0].url=http://localhost:8080/
  3. spring.boot.admin.ui.external-views[0].order=1800
  4. spring.boot.admin.ui.external-views[0].iframe=true

external-views以List的方式配置。

效果如下:

SBA - 图4

自定义报警

SBA提供了基于mail的报警,但是mail的延迟太高,不易及时处理,所以需要自定义一个报警。直接贴代码:

  1. /**
  2. * <p>自定义报警</p >
  3. *
  4. * @author jack.li
  5. * @version 1.0
  6. * @date 2021/10/27 12:39 下午
  7. */
  8. @Slf4j
  9. public class XyStatusChangeNotifier extends AbstractStatusChangeNotifier {
  10. private final MessageComponent messageComponent;
  11. public XyStatusChangeNotifier(InstanceRepository repository, final MessageComponent messageComponent) {
  12. super(repository);
  13. this.messageComponent = messageComponent;
  14. }
  15. @Override
  16. protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
  17. if (event instanceof InstanceStatusChangedEvent) {
  18. InstanceStatusChangedEvent statusChangedEvent = (InstanceStatusChangedEvent) event;
  19. boolean down = statusChangedEvent.getStatusInfo().isDown();
  20. boolean offline = statusChangedEvent.getStatusInfo().isOffline();
  21. boolean up = statusChangedEvent.getStatusInfo().isUp();
  22. boolean unknown = statusChangedEvent.getStatusInfo().isUnknown();
  23. //TODO
  24. }
  25. String serviceName = instance.getRegistration().getName();
  26. String serviceUrl = instance.getRegistration().getServiceUrl();
  27. String status = instance.getStatusInfo().getStatus();
  28. Map<String, Object> details = instance.getStatusInfo().getDetails();
  29. StringBuilder str = new StringBuilder();
  30. str.append("监控报警 : 【" + serviceName + "】");
  31. str.append("【服务地址】" + serviceUrl);
  32. str.append("【状态】" + status);
  33. str.append("【详情】" + JSONObject.toJSONString(details));
  34. log.info(">>>>>>>>sent message:" + str.toString());
  35. return Mono.fromRunnable(() -> messageComponent.send(MessageType.Alarm_Message, str.toString()));
  36. }
  37. }
  1. /**
  2. * <p>description</p >
  3. *
  4. * @author jack.li
  5. * @version 1.0
  6. * @since 2021/10/27 1:31 下午
  7. */
  8. @Configuration
  9. @RequiredArgsConstructor(onConstructor = @__(@Autowired))
  10. public class AlarmConfig {
  11. private final MessageComponent messageComponent;
  12. @Bean
  13. public XyStatusChangeNotifier statusChangeNotifierToLark(InstanceRepository repository) {
  14. return new XyStatusChangeNotifier(repository, messageComponent);
  15. }
  16. /**
  17. * 当服务不可用时,需要持续发告警消息。以下仅定义服务状态为{"DOWN", "OFFLINE"}时持续告警
  18. @Primary
  19. @Bean(initMethod = "start", destroyMethod = "stop")
  20. public RemindingNotifier remindingNotifier(InstanceRepository repository) {
  21. RemindingNotifier notifier = new RemindingNotifier(statusChangeNotifierToLark(repository), repository);
  22. notifier.setReminderStatuses(new String[]{"DOWN", "OFFLINE"});
  23. notifier.setEnabled(true);
  24. notifier.setReminderPeriod(Duration.ofSeconds(60));
  25. notifier.setCheckReminderInverval(Duration.ofSeconds(60));
  26. return notifier;
  27. }
  28. /**
  29. * 开启web http trace跟踪
  30. *
  31. * @return
  32. */
  33. @Bean
  34. public InMemoryHttpTraceRepository getInMemoryHttpTrace() {
  35. return new InMemoryHttpTraceRepository();
  36. }
  37. }

可暴露端点列表

SBA - 图5