SpringBoot整合Actuator 和 Admin实现服务监控功能

SpringBoot整合Actuator 和 Admin

首先分为两个端 一个是 Service端,和多个 Client端。

手册:

Spring Boot Actuator :Actuator 手册

Spring Boot Admin:Admin手册

1、Service 端

1、pom文件

<!--  SpringBootAdminServer -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.6.7</version>
</dependency>

需要注意版本需要和SpringBoot版本一致。

2、yml文件:

#端口
server:
  port: 6067
spring:
  application:
    name: detection
  profiles:
    active: dev #默认为开发环境
# springbootAdmin
  boot:
    admin:
      monitor:
        default-timeout: 50000
management:
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true

3、启动类加上@EnableAdminServer:

@EnableAdminServer
@SpringBootApplication
public class DetectionApplication {

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

2、Client 端

1、pom文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--SpringBoot-AdminClient-->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.7.0</version>
</dependency>

2、yml文件:

# springbootAdmin
spring: 
 boot:
    admin:
      client:
        url: http://127.0.0.1:6067
# springbootAdmin
management:
  endpoints:
    web:
      exposure:
        include: '*'
        exclude: configprops
        # 排除对配置信息的监控,每次浏览这个节点的时候,
        # 数据库的链接就一直释放不掉, 最后导致超时,因为配置信息的监控也不重要,
    #enabled-by-default: true
  info:
    env:
      enabled: true
  endpoint:
    health:
      show-details: always

3、访问http://localhost:6067/wallboard

在这里插入图片描述

3、整合SpringSecurity

1、Service 端引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、service端配置文件修改为:

#端口
server:
  port: 6067

spring:
  application:
    name: detection
  profiles:
    active: dev #默认为开发环境
  security:
    user:
      password: admin
      name: admin
# springbootAdmin
  boot:
    admin:
      monitor:
        default-timeout: 50000
management:
  endpoint:
    health:
      show-details: always
  info:
    env:
      enabled: true

3、修改client端yml文件:

# springbootAdmin
spring: 
 boot:
    admin:
      client:
        url: http://127.0.0.1:6067
        username: admin
        password: admin
# springbootAdmin
management:
  endpoints:
    web:
      exposure:
        include: '*'
        exclude: configprops
        # 排除对配置信息的监控,每次浏览这个节点的时候,
        # 数据库的链接就一直释放不掉, 最后导致超时,因为配置信息的监控也不重要,
    #enabled-by-default: true
  info:
    env:
      enabled: true
  endpoint:
    health:
      show-details: always

访问:http://localhost:6067/login
在这里插入图片描述

4、SpringBootAdmin整合钉钉

需求:服务宕机时钉钉机器人发送消息至群。

1、yml文件:

# 钉钉发送群消息
dingding:
  robotName: wallet-服务助手
  testUrl: xxxx
  testSecret: xxxxx

2、Service端添加类:

@Component
public class DingDingNotifier extends AbstractStatusChangeNotifier {

    @Value("${dingding.robotName}")
    private String robotName;

    public DingDingNotifier(InstanceRepository repository) {
        super(repository);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        String serviceName = instance.getRegistration().getName();
        String serviceUrl = instance.getRegistration().getServiceUrl();
        String status = instance.getStatusInfo().getStatus();
        StringBuilder str = new StringBuilder();
        str.append("服务宕机 : 【" + serviceName + "】");
        str.append("【服务地址】" + serviceUrl);
        str.append("【状态】" + status);
        return Mono.fromRunnable(() -> {
            // 状态不是UP的时候通知钉钉群
            if(!status.equals("UP")) {
                if (DateUtils.isWorkTime()) {
                    // 是 9:00~18:00时间段,@所有人
                    RobotUtils.sendTextMsg(robotName, str.toString(), null);
                } else {
                    // 不是 9:00~18:00时间段,只发消息不@所有人
                    RobotUtils.sendTextMsg(robotName, str.toString());
                }
            }
        });
    }
}

5、监控client端的http请求

发现整合后没有:Web模块,即下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SZb2gx4B-1675671541465)(C:\Users\86183\AppData\Roaming\Typora\typora-user-images\image-20230206155529840.png)]

这个是因为 spring-boot 2.2以上版本默认不启用 HttpTraceRepository,需要使用者在工程中显示声明才可以正常使用。

官方解释: 链接
由于Spring Boot Actuator使用HttpTrace消耗资源并不支持集群,在Spring Boot 2.2 Release Notes开始已经默认禁用了,要启用HTTP跟踪,实现HttpTraceRepository或AuditEventRepository重新打开这些功能。

解决办法:Client端添加如下类即可:

Client端添加类:

@Configuration
public class ActuatorConfig {
    @Bean
    public HttpTraceRepository buildHttpTraceRepository() {
        return new InMemoryHttpTraceRepository();
    }
}

观察页面:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xGuO2QpC-1675671541465)(C:\Users\86183\AppData\Roaming\Typora\typora-user-images\image-20230206160014997.png)]

系统调用接口了才会在HTTP跟踪显示出来 请求路径、状态等信息。

看到上图的页面,然后在性能菜单里面可以添加每条请求指标。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-77rLSHbs-1675671541465)(C:\Users\86183\AppData\Roaming\Typora\typora-user-images\image-20230206160301204.png)]

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值